์•Œ๊ณ ๋ฆฌ์ฆ˜

๋ฐฑ์ค€ 10816 ์นด๋“œ๋„˜๋ฒ„2 ๋ฌธ์ œํ’€์ด

ghtis1798 2021. 3. 21. 12:18

๐Ÿ“Œ๋ฌธ์ œ์„ค๋ช…

๋ฐฑ์ค€ 10816 ๋ฌธ์ œ ์„ค๋ช…
๋ฐฑ์ค€ 10816 ์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

๐Ÿ“Œ๋ฌธ์ œ์ ‘๊ทผ๋ฐฉ์‹

1์ฐจ์‹œ๋„

3๋‹จ๊ณ„์— ๊ฑธ์ณ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๊ณ  ํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋ฌธ์ œ๋ฅผ ๋ณต์žกํ•˜๊ฒŒ ๋งŒ๋“ค์—ˆ๋‹ค.

1. ์ž…๋ ฅ๋ฐ›์€ ๊ฐ’์„ ์ •๋ ฌํ•œ๋‹ค.

2. (์ž…๋ ฅ๋ฐ›์€ ๊ฐ’, ๊ฐœ์ˆ˜)๋ฅผ ์ €์žฅํ•œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

3. 2๋ฒˆ์˜ ๋ฆฌ์ŠคํŠธ์—์„œ ์ž…๋ ฅ๋ฐ›์€ ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์ด์ง„ํƒ์ƒ‰์„ ์ˆ˜ํ–‰ํ•˜๊ณ , ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์ฒซ ์‹œ๋„๋Š” ์ด์ง„ ํƒ์ƒ‰์„ ์‚ฌ์šฉํ–ˆ๋‹ค.

์ด์œ ๋Š” ์ˆซ์ž ์นด๋“œ ๊ฐœ์ˆ˜๊ฐ€ 50๋งŒ์ด์—ˆ๊ณ , ์ˆซ์ž ์นด๋“œ์˜ ๋ฒ”์œ„๊ฐ€ -์ฒœ๋งŒ~+์ฒœ๋งŒ์ด์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๋”ฐ๋ผ์„œ ์ตœ์•…์˜ ๊ฒฝ์šฐ 50๋งŒ x 7log10์„ ๊ณ„์‚ฐํ•˜๋ฉด ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ•ด์‰ฌ๋กœ ๊ตฌํ˜„ํ•ด์•ผ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.

์†Œ์Šค์ฝ”๋“œ

import sys

n = int(sys.stdin.readline().rstrip())
cards = list(map(int, sys.stdin.readline().rstrip().split()))
m = int(sys.stdin.readline().rstrip())
nums = list(map(int, sys.stdin.readline().rstrip().split()))
cards_set = list(set(cards))

cards.sort()
cards_set.sort()
cards_num = [cards_set[i] for i in range(len(cards_set))]
counts_num = [cards.count(cards_set[i]) for i in range(len(cards_set))]

def test(cards_num, num):
    left, right = 0, len(cards_num)
    while left <= right:
        mid = (left+right)//2
        if cards_num[mid] == num:
            return mid
        elif cards_num[mid] > num:
            right = mid-1
        else:
            left = mid+1
    return -1

def solution(cards_num, counts_num, nums):
    result = []
    for i in range(len(nums)):
        res = test(cards_num, nums[i])
        if res == -1:
            result.append(0)
        else:
            result.append(counts_num[res])
    return result

result = solution(cards_num, counts_num, nums)
for x in result:
    print(x, end=' ')

2์ฐจ์‹œ๋„

๋‘ ๋ฒˆ์งธ๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ด์šฉํ–ˆ๋‹ค.

๋ฆฌ์ŠคํŠธ๊ฐ€ ํŽธํ•˜๋‹ค๋ณด๋‹ˆ ์ž๊พธ ๋ฆฌ์ŠคํŠธ๋กœ ์ ‘๊ทผํ•˜๊ฒŒ ๋˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

๊ทธ๋ž˜์„œ ๋ฌธ์ œ์˜ ๋ณต์žก๋„์™€ ๋‚œ์ด๋„๊ฐ€ ์˜ฌ๋ผ๊ฐ”๋‹ค.

๊ฐ„๋‹จํžˆ ์ž…๋ ฅ๋ฐ›์€ ์ˆ˜๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ๋”•์…”๋„ˆ๋ฆฌ์˜ key๊ฐ’์œผ๋กœ ์ €์žฅํ•˜๊ณ , value๋ฅผ 1์”ฉ ์ฆ๊ฐ€์‹œํ‚ค๋ฉด ๋˜๋Š” ๋ฌธ์ œ์˜€๋‹ค.

 ๊ฒฐ๊ณผ๊ฐ’์œผ๋กœ๋Š” ํ•ด์‰ฌ์— ํ•ด๋‹น ๊ฐ’์ด ์žˆ์œผ๋ฉด value๋ฅผ ์ถœ๋ ฅ, ์—†์œผ๋ฉด 0์„ ์ถœ๋ ฅํ•˜๋ฉด ํ•ด๊ฒฐ๋ฌ๋‹ค.

์†Œ์Šค์ฝ”๋“œ

import sys

n = int(sys.stdin.readline().rstrip())
cards = list(map(int, sys.stdin.readline().rstrip().split()))
m = int(sys.stdin.readline().rstrip())
nums = list(map(int, sys.stdin.readline().rstrip().split()))
cards_set = list(set(cards))
cards.sort()

def solution(cards_set, cards, nums):
    hashmap = {}
    for x in cards:
        if x in hashmap:
            hashmap[x] += 1
        else:
            hashmap[x] = 1
    return hashmap

hashmap = solution(cards_set, cards, nums)s
for x in nums:
    if x in hashmap:
        print(hashmap[x], end=' ')
    else:
        print('0', end=' ')