ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ 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
- java
- AWS
- python3
- ๋งฅ๋ถํ๋ก
- ์๊ฐ๊ต์ฒด
- ๋ฐ๋
- ํด์๋งต
- MongoDB
- ๋งฅ๋ถ
- ๋ชฝ๊ณ ๋๋น
- ์๊ณ ๋ฆฌ์ฆ
- ํ
- ๋ถํ ์ ๋ณต
- baekjoon
- dfs
- BOJ
- ํ๋ก์ด๋์์ฌ
- ballet
- dp
- go
- BFS
- Macbook pro 2012 mid 13
- Algorithm
- ๋ฐฑ์ค
- ์๋ฐ
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- Golang
- ์คํ
- ์ด๋ถํ์
- ๋ธ๋ฃจํธํฌ์ค
- Total
- Today
- Yesterday