ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ 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
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 2428๋ฒ / ํ์ [Go][Python3] (0) | 2021.02.16 |
---|---|
BOJ / 10546๋ฒ / ๋ฐฐ๋ถ๋ฅธ ๋ง๋ผํ ๋ [Go][Python3] (0) | 2021.02.15 |
BOJ / 1822๋ฒ / ์ฐจ์งํฉ [Go][Python3] (0) | 2021.02.13 |
BOJ / 2670๋ฒ / ์ฐ์๋ถ๋ถ์ต๋๊ณฑ [Go][Python3] (0) | 2021.02.12 |
BOJ / 1543๋ฒ / ๋ฌธ์ ๊ฒ์ [Go][Python3] (0) | 2021.02.11 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- Algorithm
- ๋งฅ๋ถ
- baekjoon
- dp
- ํ๋ก์ด๋์์ฌ
- dfs
- ๋ถํ ์ ๋ณต
- go
- ballet
- ์๊ฐ๊ต์ฒด
- ๋ชฝ๊ณ ๋๋น
- Macbook pro 2012 mid 13
- AWS
- ์๋ฐ
- ๋ฐฑ์ค
- ๋งฅ๋ถํ๋ก
- Golang
- ๋ฐ๋
- java
- BOJ
- ํ
- MongoDB
- ํด์๋งต
- ์คํ
- python3
- BFS
- ๋ธ๋ฃจํธํฌ์ค
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- ์๊ณ ๋ฆฌ์ฆ
- ์ด๋ถํ์
- Total
- Today
- Yesterday