ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๋ฌธ์ œ

 

2910๋ฒˆ: ๋นˆ๋„ ์ •๋ ฌ

์ฒซ์งธ ์ค„์— ๋ฉ”์‹œ์ง€์˜ ๊ธธ์ด N๊ณผ C๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N ≤ 1,000, 1 ≤ C ≤ 1,000,000,000) ๋‘˜์งธ ์ค„์— ๋ฉ”์‹œ์ง€ ์ˆ˜์—ด์ด ์ฃผ์–ด์ง„๋‹ค.

www.acmicpc.net


โœ๐Ÿป ํ’€์ด

๐ŸŽจ Go

// https://www.acmicpc.net/problem/2910
package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n, c int
	fmt.Fscanln(reader, &n, &c)
	var frequency = map[int]int{}
	var order = map[int]int{}
	for i := 0; i < n; i++ {
		var number int
		fmt.Fscanf(reader, "%d ", &number)
		frequency[number]++
		if _, ok := order[number]; !ok {
			order[number] = i + 1
		}
	}

	var frequencies = []frequencyFormat{}
	for key, val := range frequency {
		frequencies = append(frequencies, frequencyFormat{key, val, order[key]})
	}

	sort.Slice(frequencies, func(i, j int) bool {
		if frequencies[i].frequency > frequencies[j].frequency {
			return true
		} else if frequencies[i].frequency == frequencies[j].frequency {
			return frequencies[i].order < frequencies[j].order
		}
		return false
	})

	for i := 0; i < len(frequencies); i++ {
		tmp := frequencies[i]
		for j := 0; j < tmp.frequency; j++ {
			fmt.Fprintf(writer, "%d ", tmp.number)
		}
	}
}

type frequencyFormat struct {
	number    int
	frequency int
	order     int
}

๐ŸŽจ Python3

# https://www.acmicpc.net/problem/2910
import sys

if __name__ == "__main__":
    n, c = list(map(int, sys.stdin.readline().split()))
    frequency, order = {}, {}
    for i, num in enumerate(list(map(int, sys.stdin.readline().split()))):
        if frequency.get(num):
            frequency[num] += 1
        else:
            frequency[num] = 1
        if not order.get(num):
            order[num] = i + 1
    
    frequencies = []
    for i in frequency.keys():
        frequencies.append((i, frequency[i], order[i]))

    frequencies.sort(key=lambda a: (-a[1], a[2]))
    for i in frequencies:
        for j in range(i[1]):
            print("{} ".format(i[0]), end='')
728x90
๋Œ“๊ธ€