ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [๋ฐฑ์ค€] 22252 ์ •๋ณด ์ƒ์ธ ํ˜ธ์„ with Python
    PS 2022. 5. 18. 02:26
    728x90
    ๋ฐ˜์‘ํ˜•

    ๐Ÿ“Œ BOJ 22252 ์ •๋ณด ์ƒ์ธ ํ˜ธ์„

    ๐Ÿ’ก ์กฐ๊ฑด

    1. ๊ด€์ฐฐํ•˜๋ฉด์„œ ์–ป์€ ์ •๋ณด๋Š” ์ด $Q$ ๊ฐœ์ด๋‹ค. ๊ฐ ์ •๋ณด๋Š” ๋‹ค์Œ์˜ 2๊ฐ€์ง€ ์ค‘ ํ•˜๋‚˜์ด๋‹ค.
      1. 1 Name k C_1, C_2, ..., C_k : ์ด๋ฆ„์ด [Name]์ธ ๊ณ ๋ฆด๋ผ๊ฐ€ k ๊ฐœ์˜ ์ •๋ณด๋ฅผ ์–ป์—ˆ์œผ๋ฉฐ, ๊ฐ ๊ฐ€์น˜๋Š” C_1 ๋ถ€ํ„ฐ C_k ์ด๋‹ค.
      2. 2 Name b : ํ˜ธ์„์ด๊ฐ€ ์ด๋ฆ„์ด [Name]์ธ ๊ณ ๋ฆด๋ผ์—๊ฒŒ b ๊ฐœ์˜ ์ •๋ณด๋ฅผ ๊ตฌ๋งคํ•œ๋‹ค.
        ์ด๋•Œ ๊ณ ๋ฆด๋ผ๊ฐ€ ๊ฐ€์ง„ ์ •๋ณด๋“ค ์ค‘ ๊ฐ€์žฅ ๋น„์‹ผ b ๊ฐœ๋ฅผ ๊ตฌ๋งคํ•˜๋ฉฐ, ๊ณ ๋ฆด๋ผ๊ฐ€ ๊ฐ€์ง„ ์ •๋ณด๊ฐ€ b๊ฐœ ์ดํ•˜์ด๋ฉด ๊ฐ€์ง„ ๋ชจ๋“  ์ •๋ณด๋ฅผ ๊ตฌ๋งคํ•œ๋‹ค.
    1. ๊ณ ๋ฆด๋ผ๋“ค์ด ์ •๋ณด๋ฅผ ์–ป๋Š” ์‚ฌ๊ฑด๊ณผ ํ˜ธ์„์ด๊ฐ€ ๊ฑฐ๋ž˜ํ•˜๋Š” ์ •๋ณด๊ฐ€ ์‹œ๊ฐ„์ˆœ์œผ๋กœ ์ฃผ์–ด์ง„๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ์ฟผ๋ฆฌ์˜ ๊ฐœ์ˆ˜ Q๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.
    1. Q ๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ์„œ ๊ฐ ์ค„์— ์ฟผ๋ฆฌ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.
    1. ์ฟผ๋ฆฌ๋Š” 1์ด๋‚˜ 2๋กœ ์‹œ์ž‘ํ•œ๋‹ค.
      1๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์ •๋ณด๋ฅผ ์–ป์€ ์ •๋ณด ๊ณ ๋ฆด๋ผ์˜ ์ด๋ฆ„๊ณผ k๊ฐ€ ์ฃผ์–ด์ง€๋ฉฐ ์ด์–ด์„œ
      k๊ฐœ์˜ ์ •๋ณด ๊ฐ€์น˜ C_1, ..., C_k๊ฐ€ ์ž์—ฐ์ˆ˜๋กœ ์ฃผ์–ด์ง„๋‹ค.
    1. ๋ชจ๋“  C_i๋Š” 1 ์ด์ƒ 100,000 ์ดํ•˜์ด๋‹ค.
      2๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ํ˜ธ์„์ด๊ฐ€ ๊ฑฐ๋ž˜ํ•˜๋ ค๋Š” ์ •๋ณด ๊ณ ๋ฆด๋ผ์˜ ์ด๋ฆ„๊ณผ ๊ตฌ๋งคํ•˜๋ ค๋Š” ์ •๋ณด์˜ ๊ฐœ์ˆ˜ b๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.
    1. ์ œํ•œ์‚ฌํ•ญ์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

      1. 1 โ‰ค Q โ‰ค 100,000, Q ๋Š” ์ž์—ฐ์ˆ˜
      2. ๋ชจ๋“  Name์€ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž ํ˜น์€ ๋Œ€๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๊ณ  ๊ณต๋ฐฑ์ด ์—†์œผ๋ฉฐ ๊ธธ์ด๋Š” 1 ์ด์ƒ 15 ์ดํ•˜์ด๋‹ค.
      3. 1 โ‰ค k โ‰ค 100,000, k ๋Š” ์ž์—ฐ์ˆ˜
      4. 1 โ‰ค C โ‰ค 100,000, C ๋Š” ์ž์—ฐ์ˆ˜
      5. 1 โ‰ค b โ‰ค 100,000, b ๋Š” ์ž์—ฐ์ˆ˜
      6. ๋ชจ๋“  ์ฟผ๋ฆฌ์— ๋Œ€ํ•œ k ์˜ ํ•ฉ์€ 1,000,000 ์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค.
    2. ์ž๋ฃŒ๊ตฌ์กฐ, ์šฐ์„ ์ˆœ์œ„ํ, heapq ์œ ํ˜•์˜ ๋ฌธ์ œ

    ๐Ÿ–ฅ ์†Œ์Šค ์ฝ”๋“œ

    from sys import stdin
    import heapq
    
    dictionary, res = {}, 0
    for _ in range(int(stdin.readline())):
        data = list(stdin.readline().split())
    
        info = data[:3]
        if info[0] == '1':
            try:
                for i in range(int(info[2])):
                    heapq.heappush(dictionary[info[1]], (-int(data[3 + i]), int(data[3 + i])))
            except:
                dictionary[info[1]] = []
                for i in range(int(info[2])):
                    heapq.heappush(dictionary[info[1]], (-int(data[3 + i]), int(data[3 + i])))
    
        else:
            try:
                for i in range(int(info[2])):
                    res += heapq.heappop(dictionary[info[1]])[1]
            except:
                pass
    
    print(res)

    ๐Ÿ”– ์˜ˆ์ œ ๋ฐ ์‹คํ–‰๊ฒฐ๊ณผ

    ์˜ˆ์ œ

    7
    1 Cpp 5 10 4 2 8 4
    1 Java 2 8 2
    2 Cpp 2
    1 Cpp 2 10 3
    2 Cpp 3
    2 Java 1
    2 Python 10

    ์‹คํ–‰๊ฒฐ๊ณผ

    44

    โŒจ๏ธ ๋ฌธ์ œ ํ’€์ด

    1. ์šฐ์„ ์ˆœ์œ„ ํ์˜ ์„ฑ์งˆ์„ ์ด์šฉํ•ด ๊ฐ ์ •๋ณด์˜ ๊ฐ€์น˜๋ฅผ ์šฐ์„ ์ˆœ์œ„๋กœ ์ •ํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•˜๋ฉฐ ๊ตฌํ˜„ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

    2. dictionary ๋กœ ์ •๋ณด์˜ ์ด๋ฆ„์„ ๊ด€๋ฆฌํ•˜๋ฉด ํŽธํ•˜๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.