โ 18869 ๋ฉํฐ๋ฒ์ค II โ
import sys
input=sys.stdin.readline
from collections import defaultdict
M,N=map(int,input().split())
cu_list,ans=[],0
def get_compressed_coordinate(l):
sort_l = sorted(set(l))
cd = {sort_l[i]: i for i in range(len(sort_l))}
x = [cd[n] for n in l]
return x
for _ in range(M):
universe=list(map(int,input().split()))
compressed_universe = get_compressed_coordinate(universe)
cu_list.append(tuple(compressed_universe))
freq=defaultdict(int)
for cu in cu_list:
freq[cu] += 1
for val in freq.values():
if val > 1:
ans+=(((val)*(val-1))//2)
print(ans)
๐ ๊ท ๋ฑํ์ง ์๋์ง์ ๊ธฐ์ค์ ๋์๊ด๊ณ๋ก๋ง ํ๋ณํ๋ฏ๋ก ํฌ๊ธฐ ์์ฒด์ ์๋ฏธ์๊ณ ํฌ๊ธฐ ์ฌ์ด์ ๋์๊ด๊ณ๋ง ์ ์๋ฏธํ๊ธฐ์ Coordinate Compression ์งํ
๐ ์ ๋ ฅ๋ฐ์ ๊ฐ ์ฐ์ฃผ๋ฅผ ์ขํ ์์ถ(get_compressed_coordinate)์ ์งํํด์ ๊ฐ ์ฐ์ฃผ์ ์กด์ฌํ๋, ํฌ๊ธฐ์ ๋ฐ๋ฅธ ํ์ฑ์ index ์ ์ฅ
ex) 1 3 2๋ผ๋ฉด ๊ฐ ํ์ฑ์ ์ขํ ์์ถ ๊ฒฐ๊ณผ index๋ 0 2 1 / 12 50 31์ด๋ผ๋ฉด ์ขํ ์์ถ ๊ฒฐ๊ณผ index๋ 0 2 1
๐ ์ขํ ์์ถ ๊ฒฐ๊ณผ index๋ฅผ tuple ํํ๋ก cu_list์ ๋ฃ์ → ๋น๋์ dictionary ์ ์ฅ → ๋น๋์ 2 ์ด์์ผ ๊ฒฝ์ฐ๋ง ์์ ๊ฐ์ ๋์ ํฉ ์ถ๋ ฅ
โ 20440 ๐ต ๋๊ฐ ์ซ์ด ์ซ์ด ๋๋ฌด ์ซ์ด ์ค์ง ๋ง ๋ด๊ฒ ์ฐ์ฉ๋์ง๋ง๐ต - 1 โ
import sys
input=sys.stdin.readline
from collections import defaultdict
N=int(input())
delta_mosquito=defaultdict(int)
for _ in range(N):
e,x=map(int,input().split())
delta_mosquito[e] += 1
delta_mosquito[x] -= 1
time=sorted(delta_mosquito.keys())
cumsum=[]
total=0
for i in range(len(time)):
total+=delta_mosquito[time[i]]
cumsum.append(total)
many=max(cumsum)
found=False
for x in range(len(cumsum)):
if cumsum[x] == many:
if not found:
found=True
s,e=x,x+1
else:
e+=1
else:
if found:
break
print(many)
print(time[s],time[e])
๐ BOJ 9574 <Goldilocks and the N Cows>์ ๋น์ทํ ์ขํ ์์ถ ๋ฌธ์
๐ ๋ชจ๊ธฐ์ ์ ์ฅ / ํด์ฅ ์๊ฐ ์ซ์ ์์ฒด๋ ์ค์์น ์๊ณ ์ฌ๋ฌ ๋ชจ๊ธฐ์ ์ ์ฅ / ํด์ฅ ์๊ฐ ๊ฐ์ ๋์ ๋น๊ต๋ง ์ค์ํ๋ฏ๋ก Coordinate Compression ์ ํ
โ ํต์ฌ์ ๋ชจ๊ธฐ์ ์ ์ฅ / ํด์ฅ ์๊ฐ ์ ๋ฒ์๊ฐ 2์ต์ด ๋์ด๊ฐ๋ค๋ ์ → ๋ชจ๊ธฐ์ ๋ง๋ฆฟ์ N์ ํ์ฉ โ
๐
โ hash map ์์ฑ: ๋ชจ๊ธฐ์ ์ ์ฅ/ํด์ฅ์๊ฐ์ key, ํด๋น ์๊ฐ์ ์ ์ฅ + 1 / ํด์ฅ - 1์ ํ์ฉํ ๋ชจ๊ธฐ ๋ง๋ฆฟ์ ๋ณํ๋์ value
โก hash map์ keys() ๊ฐ์ ธ์์ ์ค๋ฆ์ฐจ์ ์ ๋ ฌ
(์ ์ฅ/ํด์ฅ ์๊ฐ ์ซ์ ์์ฒด๊ฐ ์๋ ๋ณํ๋๋ง ๋ฐ์ง๋ฏ๋ก Coordinate Compression ๊ธฐ๋ฒ ์งํ)
โข ์ ๋ ฌํ keys() for loop ๋๋ฆฌ๋ฉฐ ๋ง๋ฆฟ์ ๋ณํ๋ ๋์ ํฉ list ์์ฑ → ๋์ ํฉ ๊ฒฐ๊ณผ๋ ๋งค ์๊ฐ ๋ชจ๊ธฐ์ ๋ง๋ฆฟ์
โฃ ์ฆ ๋์ ํฉ list์ ์ต๋๊ฐ & ์ต๋๊ฐ์ผ ๋์ start index / end index ๊ตฌํด์ ์ถ๋ ฅ
→ ๊ฐ์ฅ ๋น ๋ฅธ ์๊ฐ ๊ธฐ์ค ์ถ๋ ฅ์ด๋ฏ๋ก flag ๊ฐ ํ์ฉํ์ฌ ํ ๋ฒ ์ฐพ์ ๋ค ์ฐ์ํด์ ์ต๋๊ฐ์ด ์๋ ๊ฒฝ์ฐ๋ง ์ ์ธ ๋ฐ๋ก break
'BOJ > ๐ฅ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
โ DP Upper-Advanced I - 9 Solvedโ (1) | 2024.01.05 |
---|---|
โ Binary Search Advanced I - 2 Solvedโ (0) | 2023.12.29 |
โ Tree Advanced I - 1 Solvedโ (1) | 2023.12.22 |
โ BFS&DFS Advanced I - 13 Solvedโ (0) | 2023.12.15 |
โ Greedy Upper-Advanced I - 1 Solvedโ (1) | 2023.10.18 |
๋๊ธ