[BOJ][๐ŸŸก5][๋ฐฑ์ค€#02170] ์„  ๊ธ‹๊ธฐ

์ž‘์„ฑ:    

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #2170


๋ฌธ์ œ

๋งค์šฐ ํฐ ๋„ํ™”์ง€์— ์ž๋ฅผ ๋Œ€๊ณ  ์„ ์„ ๊ทธ์œผ๋ ค๊ณ  ํ•œ๋‹ค. ์„ ์„ ๊ทธ์„ ๋•Œ์—๋Š” ์ž์˜ ํ•œ ์ ์—์„œ ๋‹ค๋ฅธ ํ•œ ์ ๊นŒ์ง€ ๊ธ‹๊ฒŒ ๋œ๋‹ค. ์„ ์„ ๊ทธ์„ ๋•Œ์—๋Š” ์ด๋ฏธ ์„ ์ด ์žˆ๋Š” ์œ„์น˜์— ๊ฒน์ณ์„œ ๊ทธ๋ฆด ์ˆ˜๋„ ์žˆ๋Š”๋ฐ, ์—ฌ๋Ÿฌ ๋ฒˆ ๊ทธ์€ ๊ณณ๊ณผ ํ•œ ๋ฒˆ ๊ทธ์€ ๊ณณ์˜ ์ฐจ์ด๋ฅผ ๊ตฌ๋ณ„ํ•  ์ˆ˜ ์—†๋‹ค๊ณ  ํ•˜์ž. ์ด์™€ ๊ฐ™์€ ์‹์œผ๋กœ ์„ ์„ ๊ทธ์—ˆ์„ ๋•Œ, ๊ทธ๋ ค์ง„ ์„ (๋“ค)์˜ ์ด ๊ธธ์ด๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ์„ ์ด ์—ฌ๋Ÿฌ ๋ฒˆ ๊ทธ๋ ค์ง„ ๊ณณ์€ ํ•œ ๋ฒˆ์”ฉ๋งŒ ๊ณ„์‚ฐํ•œ๋‹ค.


์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์„ ์„ ๊ทธ์€ ํšŸ์ˆ˜ N(1 โ‰ค N โ‰ค 1,000,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ N๊ฐœ์˜ ์ค„์—๋Š” ์„ ์„ ๊ทธ์„ ๋•Œ ์„ ํƒํ•œ ๋‘ ์ ์˜ ์œ„์น˜ x, y(-1,000,000,000 โ‰ค x < y โ‰ค 1,000,000,000)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.


์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ๊ทธ์€ ์„ ์˜ ์ด ๊ธธ์ด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.


์˜ˆ์ œ

์ž…๋ ฅ

4
1 3
2 5
3 5
6 7


์ถœ๋ ฅ

5


My Sol

import sys
input = sys.stdin.readline

N = int(input())
d = {}
for _ in range(N):
    s, e = map(int, input().split())
    v = d.get(s, 1e10)
    if v == 1e10 or v < e:
        d[s] = e

arr = sorted([(k, v) for k, v in d.items()])
ret = 0
tmps, tmpe = arr[0]

for s, e in arr[1:]:
    if tmpe < s:
        ret += tmpe - tmps
        tmps, tmpe = s, e
        continue

    tmpe = max(tmpe, e)

ret += tmpe - tmps
print(ret)

์ž…๋ ฅ ์ฒ˜๋ฆฌ

  1. ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ด์šฉํ•ด ์‹œ์ž‘์ ์„ key๋กœ, ๋„์ฐฉ์ ์„ value๋กœ ๋‘์—ˆ๋‹ค.
  2. ๋งŒ์•ฝ ์ž…๋ ฅ์— ํ•ด๋‹นํ•˜๋Š” ์‹œ์ž‘์ ์ด ์ด๋ฏธ ์žˆ์—ˆ๋‹ค๋ฉด ๋” ๊ธด ๋ฒ”์œ„๋ฅผ ์ €์žฅ
  3. ์ž…๋ ฅ์— ํ•ด๋‹นํ•˜๋Š” ์‹œ์ž‘์ ์ด ์—†์—ˆ๋‹ค๋ฉด ํ•ด๋‹น ๋„์ฐฉ์ ์„ ์ €์žฅํ•ด์ค€๋‹ค.
  4. ๋ชจ๋“  ์ž…๋ ฅ์„ ๋งˆ์น˜๋ฉด key ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด ๊ฐ’๊ณผ ํ•จ๊ป˜ arr ๋ฐฐ์—ด์— ๋‹ด์•„์ค€๋‹ค.


๊ฐ’ ๋”ํ•˜๊ธฐ

  1. ๊ธฐ์ค€ ์‹œ์ž‘์ ๊ณผ ๋„์ฐฉ์ ์„ arr์˜ ์ฒซ ๋ฐฐ์—ด๋กœ ๋‘”๋‹ค.
  2. ๋งŒ์•ฝ ๊ธฐ์ค€ ๋„์ฐฉ์ ์ด ๋‹ค์Œ ์กฐํšŒํ•˜๋Š” ์‹œ์ž‘์ ๋ณด๋‹ค ์ ๋‹ค๋ฉด ์ด์–ด์ง€์ง€ ์•Š๋Š” ๊ฒƒ์ด๋‹ค.
  3. ๊ธฐ์ค€ ์‹œ์ž‘์ ๊ณผ ๋„์ฐฉ์ ์˜ ๊ธธ์ด๋ฅผ ret์— ๋”ํ•ด์ฃผ๊ณ  ์ƒˆ๋กœ ๋งž์ดํ•œ ์‹œ์ž‘์ ๊ณผ ๋„์ฐฉ์ ์„ ๊ธฐ์ค€ ์‹œ์ž‘์ ๊ณผ ๋„์ฐฉ์ ์œผ๋กœ ์žฌ์„ค์ •ํ•œ๋‹ค.
  4. ๋งŒ์•ฝ ๊ธฐ์ค€ ๋„์ฐฉ์ ์ด ๋‹ค์Œ ์กฐํšŒํ•œ ์‹œ์ž‘์ ์„ ๋„˜์–ด์„ ๋‹ค๋ฉด ๊ธธ์ด๋ฅผ ์—ฐ์žฅํ•˜๊ฑฐ๋‚˜ ๋ฎ์–ด์”Œ์šธ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๊ธฐ์ค€ ๋„์ฐฉ์ ๊ณผ ๋„์ฐฉ์  ์ค‘ ํฐ ๊ฐ’์„ ๊ธฐ์ค€ ๋„์ฐฉ์ ์œผ๋กœ ์„ค์ •ํ•œ๋‹ค.
  5. ์ด ๊ณผ์ •์„ ๋ชจ๋‘ ๊ฑฐ์น˜๋ฉด ret์— ํ˜„์žฌ ์ €์žฅ์ค‘์ธ ๊ธฐ์ค€ ์‹œ์ž‘์ ๊ณผ ๋„์ฐฉ์  ๊ฐ„ ๊ฑฐ๋ฆฌ๋ฅผ ๋”ํ•˜๊ณ  ์ถœ๋ ฅํ•œ๋‹ค.


๊ฒฐ๊ณผ

๋งž์•˜์Šต๋‹ˆ๋‹ค!!


๋ชจ๋ฒ”๋‹ต์•ˆ

์ถœ์ฒ˜

from sys import stdin, stdout

N = int(stdin.readline())
line = [tuple(map(int, stdin.readline().split())) for _ in range(N)]
line.sort(key=lambda x:x[0])
ans = 0
limit = -1000000001
for i in range(N):
    if limit < line[i][0]:
        ans += line[i][1] - line[i][0]
        limit = line[i][1]
    elif limit < line[i][1]:
        ans += line[i][1] - limit
        limit = line[i][1]
stdout.write(f"{ans}")

ํฐ ์ฐจ์ด๋Š” ์•„๋‹ˆ์ง€๋งŒ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ์ด๋‚˜ ์‹œ๊ฐ„๋ฉด์—์„œ ์†Œํญ ๋‚˜์€ ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•ด๋ณด๋ ค ํ•œ๋‹ค. ๋น„์Šทํ•œ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•œ ๊ฒƒ ๊ฐ™๋‹ค๋งŒ tuple์„ ์‚ฌ์šฉํ–ˆ๊ณ , ์ค‘๋ณต์„ ๊ฑธ๋Ÿฌ๋‚ด์ง€ ์•Š๊ณ  ๊ทธ๋ƒฅ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ–ˆ๋‹ค.

์ดํ›„ limit์„ ๊ฐฑ์‹ ํ•ด๋‚˜๊ฐ€๋ฉฐ ๊ธธ์ด๋ฅผ ๋งค๋ฒˆ ์ƒˆ๋กœ ๋”ํ•ด๊ฐ€๋ฉฐ ์ €์žฅํ–ˆ๋‹ค.

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ