ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [๋ฐฑ์ค€] 13334 ์ฒ ๋กœ with Python
    PS 2021. 10. 25. 23:56
    728x90
    ๋ฐ˜์‘ํ˜•

    ๐Ÿ“Œ BOJ 13334 ์ฒ ๋กœ

    ๐Ÿ’ก ์กฐ๊ฑด

    1. ์‚ฌ๋žŒ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์–‘์˜ ์ •์ˆ˜ n (1 โ‰ค n โ‰ค 100,000)
      n๊ฐœ์˜ ๊ฐ ์ค„์— ์ •์ˆ˜ ์Œ (hi, oi)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.
      โˆ’100,000,000 โ‰ค hi โ‰ค 100,000,000
      โˆ’100,000,000 โ‰ค oi โ‰ค 100,000,000
      oi != hi
      ์ฒ ๋กœ์˜ ๊ธธ์ด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ d (1 โ‰ค d โ‰ค 200,000,000)

    2. ์ง‘๊ณผ ์‚ฌ๋ฌด์‹ค ๋ชจ๋‘๊ฐ€ ์ฒ ๋กœ ๊ธธ์ด ์•ˆ์— ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ.

    3. ์šฐ์„ ์ˆœ์œ„ ํ, ์ฆ‰ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฌธ์ œ.

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

    from sys import stdin
    import heapq
    
    n = int(stdin.readline())
    roads, data = [], []
    
    for _ in range(n):
        data.append(sorted(list(map(int, stdin.readline().split()))))
    train_road_length = int(stdin.readline())
    
    
    for road in data:
        s, e = road
        if (e - s) <= train_road_length:
            roads.append(road)
    
    roads.sort(key=lambda x: x[1])
    
    answer = 0
    q = []
    
    for road in roads:
        if not q:
            heapq.heappush(q, road)
        else:
            while q[0][0] < road[1] - train_road_length:
                heapq.heappop(q)
                if not q:
                    break
    
            heapq.heappush(q, road)
        answer = max(answer, len(q))
    
    print(answer)

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

    ์˜ˆ์ œ

    8
    5 40
    35 25
    10 20
    10 25
    30 50
    50 60
    30 25
    80 100
    30

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

    4

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

    1. ๊ฐ€์žฅ ๋จผ์ € ์ง‘๊ณผ ์‚ฌ๋ฌด์‹ค์˜ ์œ„์น˜๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋ฐ, ๊ทธ ์œ„์น˜๊ฐ€ ์ •๋ ฌ์ด ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์•„๋‹ˆ๊ธฐ์— ๊ณ„์‚ฐ์„ ์šฉ์ดํ•˜๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด
      ๋ฐ์ดํ„ฐ๋ฅผ ์ „์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค€ ํ›„, data ๋ฆฌ์ŠคํŠธ์— ๋„ฃ๋Š”๋‹ค.
    1. ์ฒ ๋กœ์˜ ๊ธธ์ด๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.
    1. data๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ, ์ง‘๊ณผ ์‚ฌ๋ฌด์‹ค์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ์ฒ ๋กœ์˜ ๊ธธ์ด๋ณด๋‹ค ์งง์„ ๊ฒฝ์šฐ์—๋งŒ roads ๋ฆฌ์ŠคํŠธ์— ๋„ฃ์–ด์ค€๋‹ค.
      ๋ชจ๋“  ๊ณณ์—์„œ์˜ ์„ ๋ถ„ d(์ฒ ๋กœ์˜ ๊ธธ์ด) ์•ˆ์— ์ง‘๊ณผ ์‚ฌ๋ฌด์‹ค์ด ๋ชจ๋‘ ์•ˆ์— ๋“ค์–ด๊ฐ€์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
    1. 3๋ฒˆ์„ ์ˆ˜ํ–‰์‹œ์ผœ ์–ป์€ roads ๋ฆฌ์ŠคํŠธ๋ฅผ ์ •๋ ฌ์‹œํ‚จ๋‹ค.
    1. ์ •๋ ฌ์‹œํ‚จ roads ๋ฐฐ์—ด์„ ๋‹ค์‹œ ์ˆœํšŒํ•˜๋ฉด์„œ, heapq ๋ฅผ ๋งŒ๋“ค์–ด ํ๊ฐ€ ๋น„์–ด์žˆ์„ ๊ฒฝ์šฐ ์ •๋ ฌ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ๋Š”๋‹ค.
    1. ํ์˜ ์‹œ์ž‘์  ์œ„์น˜๊ฐ€ (ํ˜„์žฌ ์ง€์ •๋œ road์˜ ๋๋‚˜๋Š” ์ง€์  - ์ฒ ๋กœ ๊ธธ์ด) ๋ณด๋‹ค ์งง์œผ๋ฉด ํ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋บ€๋‹ค.
      ํ์— ๋“ค์–ด๊ฐ€ ์žˆ๋Š” ์ง‘๊ณผ ์‚ฌ๋ฌด์‹ค ์ขŒํ‘œ ์ˆœ์„œ์Œ์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ •๋‹ต์ด๊ธฐ ๋•Œ๋ฌธ์—
      ์ฒ ๋กœ์˜ ๊ธธ์ด ๋ฒ”์œ„ ๋ฐ–์œผ๋กœ ์ขŒํ‘œ๊ฐ€ ๋‚˜๊ฐ€๋Š” ์• ๋“ค์€ ๊ณผํ•จํ•˜๊ฒŒ ์ž˜๋ผ์ค€๋‹ค.

    ๐Ÿ’พ ๋Š๋‚€์ 

    1. ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ‘ธ๋Š” ๋ฌธ์ œ์˜€์ง€๋งŒ, ๋ฌธ์ œ๋ฅผ ์ดํ•ดํ•˜๊ณ  ํ‘ธ๋Š”๋ฐ์— ์• ๋ฅผ ๋จน์—ˆ๋‹ค.
    2. ๋ˆ„์ ํ•ฉ์„ ์‚ฌ์šฉํ•ด์„œ ํ’€์–ด๋ณด๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ ์˜ฌ๋ฐ”๋ฅธ ํ’€์ด๊ฐ€ ์•„๋‹ˆ์—ˆ๋Š”์ง€, ์ฝ”๋”ฉ์„ ํ•˜๋‹ค๊ฐ€ ๋ช‡ ๋ฒˆ ๋’ค์ง‘์—ˆ๋‹ค.
    3. ์ด ๋ฌธ์ œ ๋˜ํ•œ ๋ฐ˜๋“œ์‹œ ํ•œ๋ฒˆ ๋” ํ’€์–ด๋ณด๊ณ  ๊ฐœ๋…์„ ์ตํ˜€์•ผํ•  ๋ฌธ์ œ์ธ ๊ฒƒ ๊ฐ™๋‹ค.
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.