https://www.acmicpc.net/problem/10816

 

10816번: 숫자 카드 2

첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10,

www.acmicpc.net

문제: 주어진 숫자 카드에 대해서, 각 수가 적혀진 숫자카드가 몇장 있는지 출력하는 문제입니다.

 

풀이 방법: heapq를 사용했습니다. 

이진 탐색 문제인 것은 알았습니다.

다만, 주어진 값들을 count 하면 되는 문제이므로 heapq.heappop()이 이진 탐색과 같은 log의 시간 복잡도를 갖는다는 것을 활용하였습니다.

 

 

풀이 코드:

import heapq
import sys

MAX = 10000000 * 2 + 2

n = int(sys.stdin.readline().rstrip())
lst = list(map(int,sys.stdin.readline().rstrip().split()))

m = int(sys.stdin.readline().rstrip())
lst2 = list(map(int,sys.stdin.readline().rstrip().split()))

count = [0] * MAX

while lst:
    count[heapq.heappop(lst)] += 1


for i in range(m):
    print(count[lst2[i]],end = ' ')

 

 

+ Recent posts