[BOJ][๐ŸŸก5][๋ฐฑ์ค€#22238] ๊ฐ€ํฌ์™€ btd5

์ž‘์„ฑ:    

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

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

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #22238


๋ฌธ์ œ

btd5์—๋Š” Darting Gun Tower๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. Darting Gun Tower๋Š” ์•„๋ž˜์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ํ’์„ ์„ ๊ณต๊ฒฉํ•ฉ๋‹ˆ๋‹ค.

๊ณต๊ฒฉํ•˜๊ณ ์ž ํ•˜๋Š” ๋ชฉํ‘œ๋ฌผ์˜ ๋ฐฉํ–ฅ์œผ๋กœ ๊ณต๊ฒฉ ๋ฐฉํ–ฅ์„ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค. ๊ณต๊ฒฉ ๋ฐฉํ–ฅ์— ์žˆ๋Š” ํ’์„ ๋“ค์˜ ์ฒด๋ ฅ์„ d์”ฉ ๋‚ฎ์ถฅ๋‹ˆ๋‹ค.

Darting Gun Tower๋Š”ย ์ขŒํ‘œ (0, 0)์— ํ•˜๋‚˜ ์žˆ์Šต๋‹ˆ๋‹ค. Darting Gun Tower๊ฐ€ ๊ณต๊ฒฉ์„ ํ•˜๊ฒŒ ๋˜๋ฉด, ๊ณต๊ฒฉํ•˜๋Š” ๋ฐฉํ–ฅ์— ๋†“์ธ ๋ชจ๋“  ํ’์„ ๋“ค์€ย ๋™์ผํ•œ ์ˆ˜์น˜์˜ ํ”ผํ•ด๋ฅผ ์ž…ํžˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ดˆ๊ธฐ์— ํ’์„ ์€ N๊ฐœ ์žˆ๊ณ , Darting Gun Tower๋Š” ๊ณต๊ฒฉ์„ M๋ฒˆ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ณต๊ฒฉ์„ ๋๋‚ผ ๋•Œ ๋งˆ๋‹ค, ๋‚จ์€ ํ’์„ ์˜ ์ˆ˜๋ฅผ ์„ธ์–ด ์ฃผ์„ธ์š”. ์ดˆ๊ธฐ ์ƒํƒœ์— Darting Gun Tower๊ฐ€ ํŠน์ • ๋ฐฉํ–ฅ์œผ๋กœ ๋ฐ๋ฏธ์ง€๊ฐ€ 109ย ์ด์ƒ์˜ ๊ณต๊ฒฉ์„ ํ–ˆ์„ ๋•Œ, ๋ชจ๋“  ํ’์„ ์„ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.


์ž…๋ ฅ

์ฒซ ๋ฒˆ์งธ ์ค„์— N, M์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. 2๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ N+1๋ฒˆ์งธ ์ค„๊นŒ์ง€ ํ’์„ ์ด ์žˆ๋Š” x์ขŒํ‘œ, y์ขŒํ‘œ, ์ฒด๋ ฅ์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. N+2๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ N+M+1๋ฒˆ์งธ ์ค„๊นŒ์ง€ Darting Gun Tower์˜ ๊ณต๊ฒฉ ๋ฐฉํ–ฅ (x, y)์™€, Darting Gun Tower๊ฐ€ ์ค€ ๋ฐ๋ฏธ์ง€ d๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.


์ถœ๋ ฅ

x๋ฒˆ์งธ ์ค„์— x๋ฒˆ์งธ ๊ณต๊ฒฉ์ด ๋๋‚ฌ์„ ๋•Œ ๋‚จ์€ ํ’์„ ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•ด ์ฃผ์„ธ์š”.


์ œํ•œ

N๊ณผ M์€ ๊ตฌ๊ฐ„ [1, 2ร—105]์— ์†ํ•˜๋Š” ์ •์ˆ˜์ž…๋‹ˆ๋‹ค. ํ’์„ ๋“ค์˜ x ์ขŒํ‘œ์™€ y ์ขŒํ‘œ๋Š” [-109, 109]์— ์†ํ•˜๋Š” ์ •์ˆ˜์ž…๋‹ˆ๋‹ค. ํ’์„ ๋“ค์˜ ์œ„์น˜๋Š” ๊ณ ์ •๋˜์–ด ์žˆ์œผ๋ฉฐ, ํ’์„ ์ด ์žˆ๋Š” ์œ„์น˜์— Darting Gun Tower๊ฐ€ ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‘ ๊ฐœ ์ด์ƒ์˜ ํ’์„ ์ด ๊ฐ™์€ ์œ„์น˜์— ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ’์„ ๋“ค์˜ hp์™€ Darting Gun Tower๊ฐ€ ์ฃผ๋Š” ๋ฐ๋ฏธ์ง€๋Š” ๊ตฌ๊ฐ„ [1, 109]์— ์†ํ•˜๋Š” ์ •์ˆ˜์ž…๋‹ˆ๋‹ค.


์˜ˆ์ œ

์ž…๋ ฅ

3 1
1 1 3
3 3 4
2 2 2
1 1 3


์ถœ๋ ฅ

1


My Sol

import sys
input = sys.stdin.readline
from heapq import heappop, heappush
from math import gcd

def find_s(x, y):
    g = gcd(x, y)
    return (x/g, y/g)


N, M = map(int, input().split())
D = {}
for _ in range(N):
    x, y, d = map(int, input().split())
    s = find_s(x, y)
    if not D.get(s, ''):
        D[s] = [0, []]
    heappush(D[s][1], d)

for _ in range(M):
    x, y, d = map(int, input().split())
    s = find_s(x, y)
    if not D.get(s, ''):
        print(N)
        continue
    D[s][0] += d
    while D[s][1]:
        if D[s][1][0] > D[s][0]: break
        heappop(D[s][1])
        N -= 1

    print(N)

๊ฝค๋‚˜ ๊นŒ๋‹ค๋กœ์šด ๋ฌธ์ œ์˜€๋‹ค. heap์„ ์‚ฌ์šฉํ•ด์„œ ์กฐ๊ธˆ ์–ด๋ ต๊ฒŒ ํ’€์—ˆ๋˜ ๊ฒƒ๋„ ๊ฐ™์ง€๋งŒ ํ’€ ๋•Œ์—๋Š” ์ตœ์„ ์ด์—ˆ๋‹คโ€ฆ

  1. ๋”•์…”๋„ˆ๋ฆฌ D๋Š” ํŠน์ • slope์—์„œ์˜ ๋ˆ„์  ๋ฐ๋ฏธ์ง€์™€ ํ•ด๋‹น slope์˜ ์˜ํ–ฅ๊ถŒ์— ์žˆ๋Š” ํ’์„ ๋“ค์˜ ์ฒด๋ ฅ๋“ค์„ ์ €์žฅํ•œ๋‹ค.
  2. M๋ฒˆ์˜ ๊ณต๊ฒฉ ์ž…๋ ฅ๋™์•ˆ ๋ฐ๋ฏธ์ง€๋ฅผ ํ•ด๋‹น slope์˜ ๋ˆ„์  ๋ฐ๋ฏธ์ง€์— ๋”ํ•˜๊ณ  ์ด๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ฒด๋ ฅ์˜ ํ’์„ ๋“ค์€ ์ œ๊ฑฐํ•˜๋ฉฐ N์„ ์ค„์ธ๋‹ค.
  3. slope๋Š” y/x๋กœ ๊ตฌํ•˜๋ ค ํ–ˆ์œผ๋‚˜ ๊ทน๋‹จ์ ์ธ ์ผ€์ด์Šค์—์„œ ๋งค์šฐ ์ž‘์€ ์†Œ์ˆ˜์˜ ํฌ๊ธฐ ์ฐจ์ด๋Š” ๊ตฌ๋ถ„์ด ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜ gcd๋ฅผ ํ™œ์šฉํ•ด ๊ตฌ๋ถ„ํ•œ๋‹ค.


์ตœ์ดˆ์—”, ๊ฐ™์€ ๋ผ์ธ์— ์žˆ๋Š” ๊ตฌ๋ถ„์„ y/x์˜ ๊ธฐ์šธ๊ธฐ ๊ณ„์‚ฐ์„ ํ†ตํ•ด์„œ ํ–ˆ๋Š”๋ฐ, ์•Œ ์ˆ˜ ์—†๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ทน๋‹จ์ ์ธ ์‚ฌ๊ณ„๋ฅผ ํ™œ์šฉํ•ด ๋ฌธ์ œ๋ฅผ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

1 2
999999998 1000000000 10
999999998 999999999 20
999999997 999999998 30

์ด ๊ฒฝ์šฐ 1.0000โ€ฆ1, 2 ์ˆ˜์ค€์œผ๋กœ ๋งค์šฐ ์ž‘์€ ํฌ๊ธฐ์˜ ์ฐจ์ด๊ฐ€ ๋ฐœ์ƒํ•˜๊ณ , ์ด๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ์†Œ์ˆ˜ ์ฒ˜๋ฆฌ ํŠน์„ฑ์ƒ ๊ฐ™์€ ์ˆ˜๋กœ ์ฒ˜๋ฆฌ๋œ๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ๋Ÿฌ๋ฉด ์•ˆ ๋˜๋ฏ€๋กœ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜๋ฅผ ํ™œ์šฉํ•œ ๊ตฌ๋ถ„์„ ํ•ด์ฃผ์—ˆ๋‹ค.


๋˜ํ•œ gcd ๋„์ž… ์ „์— ๋ฐฉํ–ฅ์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด 12์‹œ ๋ฐฉํ–ฅ๋ถ€ํ„ฐ ์‹œ๊ณ„๋ฐฉํ–ฅ์œผ๋กœ 1๋ถ€ํ„ฐ 8๊นŒ์ง€์˜ ๊ตฌ๋ถ„์„ ๋‘์–ด returnํ–ˆ๋‹ค.

def find_s(x, y):
    g = gcd(x, y)
    if not x: return (0, 0, 1) if y > 0 else (0, 0, 5)
    if not y: return (0, 0, 3) if x > 0 else (0, 0, 7)

    if x > 0: d = 2 if y > 0 else 4
    else: d = 8 if y > 0 else 6

    return (x/g, y/g, d)


ํ•˜์ง€๋งŒ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐฉํ–ฅ์˜ ํŒŒ์•…์ด ๋ถˆํ•„์š”ํ•˜๋ฏ€๋กœ ์•„๋ž˜๊ณผ ๊ฐ™์ด ์งง๊ฒŒ ํ‘œํ˜„๋  ์ˆ˜ ์žˆ๋‹ค.

def find_s(x, y):
    g = gcd(x, y)
    return (x/g, y/g)

๊ทธ๋Ÿฐ๋ฐ ์˜์™ธ๋กœ ์œ„์˜ ํ•จ์ˆ˜๊ฐ€ ์—ฐ์‚ฐ์‹œ๊ฐ„์ด ๋” ์งง์•˜๋‹ค. ๋ถ„๊ธฐ์ฒ˜๋ฆฌ๊ฐ€ ๋งŽ์•„์„œ ๋‹น์—ฐํžˆ ๋” ์˜ค๋ž˜ ๊ฑธ๋ฆด ์ค„ ์•Œ์•˜๋Š”๋ฐ, gcd ํ•จ์ˆ˜๋ฅผ ๋œ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์ด ์•„๋‹๊นŒ ์ถ”์ธกํ•ด๋ณธ๋‹ค.


๊ฒฐ๊ณผ

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

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