Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5-pu2rile #18

Merged
merged 4 commits into from
Jul 11, 2024
Merged

5-pu2rile #18

merged 4 commits into from
Jul 11, 2024

Conversation

pu2rile
Copy link
Collaborator

@pu2rile pu2rile commented Apr 10, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

https://www.acmicpc.net/problem/1417

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

30๋ถ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

  • ๋ฌธ์ œ ์š”์•ฝ

    • ๊ตญํšŒ์˜์› ํ›„๋ณด์˜ ์ˆ˜๋Š” N
    • ๋‹ค์†œ์ด๋Š” ๊ธฐํ˜ธ 1๋ฒˆ
    • ๋‹ค์†œ์ด์˜ ๋“ํ‘œ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋งŽ๋„๋ก ๋‹ค๋ฅธ ํ›„๋ณด์ž์˜ ํ‘œ๋ฅผ ๋งค์ˆ˜
    • ๋งค์ˆ˜ํ•œ ํ‘œ์˜ ์ตœ์†Ÿ๊ฐ’์€?
  • ์•Œ๊ณ ๋ฆฌ์ฆ˜

    1. ํ›„๋ณด์˜ ์ˆ˜ ์ž…๋ ฅ(N) -> ์ด ๋ช‡ ๋ช…์ด ์ถœ๋งˆํ•˜๋Š”์ง€
    2. ๋‹ค์†œ์ด๊ฐ€ ๊ธฐํ˜ธ 1๋ฒˆ์ด๋ฏ€๋กœ ๋‹ค์†œ์ด๋ถ€ํ„ฐ ์ž…๋ ฅ
    3. ๋‹ค์†œ์ด๋ฅผ ์ œ์™ธํ•˜๊ณ  ์ด ๋ช‡๋ช…์ด ์ถœ๋งˆํ•˜๋Š”์ง€ ๋ชจ๋ฅด๋‹ˆ ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ -> vote[]
    4. ๋‹ค์†œ์ด๋ฅผ ์ œ์™ธํ•œ N-1๋ช…์˜ ํ›„๋ณด์ž์˜ ๋“ํ‘œ์ˆ˜๋ฅผ vote[]์— ์‚ฝ์ž…
    5. ๋“ํ‘œ์ˆ˜๊ฐ€ ๋งŽ์€ ์‚ฌ๋žŒ๋ถ€ํ„ฐ ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด vote.sort(reverse = True)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
    6. ๋งŒ์•ฝ ๋‹จ์ผ ํ›„๋ณด๋ผ๋ฉด (N==1) '0' ์ถœ๋ ฅ
    7. ๋‹จ์ผ ํ›„๋ณด๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ๋“ํ‘œ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋งŽ์€ ํ›„๋ณด vote[0]๊ฐ€ ๋‹ค์†œ์ด๋ณด๋‹ค ๋งŽ๊ฑฐ๋‚˜ ๊ฐ™์„ ๋•Œ๊นŒ์ง€
      ๋‹ค์†œ์ด ํ‘œ + 1 ๋‹ค๋“ํ‘œ์ž - 1 ๋งค์ˆ˜ํ•ด์•ผ ํ•˜๋Š” ์‚ฌ๋žŒ ์ˆ˜(cnt) +1 => ๋‹ค์‹œ vote[] ์ •๋ ฌ
    8. cnt ์ถœ๋ ฅ

๐Ÿ’ป ์ฝ”๋“œ

N = int(input())
dasom = int(input())
vote = []
cnt = 0

for _ in range(N-1):
  vote.append(int(input()))

vote.sort(reverse=True) #๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ

if N == 1:
  print('0')
else:
    while vote[0] >= dasom:
        dasom += 1
        vote[0] -= 1
        cnt += 1
        vote.sort(reverse=True)
    print(cnt)

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

๋งˆ์นจ PR ๋‚ ์ด ์ด์„  ๋‚ ์ด๋ผ ๊ตญํšŒ์˜์› ์„ ๊ฑฐ ๋ฌธ์ œ๋ฅผ ํ’€์–ด ๋ดค์Šต๋‹ˆ๋‹ค!!
์ถœ๋งˆ์ž ๋ฆฌ์ŠคํŠธ๋ฅผ ์–ด๋–ป๊ฒŒ ์ •๋ฆฌํ•ด์„œ ์กฐ๊ฑด์„ ๋ฝ‘์•„๋‚ผ๊นŒ ๊ณ ๋ฏผ์„ ๋งŽ์ด ํ–ˆ๋Š”๋ฐ .sort(reverse=True)๋ผ๋Š” ๊ฒŒ ์žˆ๋”๋ผ๊ตฌ์š”!!
๋•๋ถ„์— ์ œ๋ฒ• ํšจ์œจ์ ์ธ ์ฝ”๋“œ๊ฐ€ ๋‚˜์˜จ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ท

@Ghdrn1399
Copy link
Contributor

์„ ๊ฑฐ๋‚  ๊ตญํšŒ์˜์› ์„ ๊ฑฐ ๋ฌธ์ œ ํ’€์–ด๋ณด๋Š” ์„ผ์Šค ๋ฐฐ์šฐ๊ณ  ๊ฐ‘๋‹ˆ๋‹ค^^
๊น”๋”ํ•˜๊ฒŒ ์ฝ”๋“œ ์ ์–ด์ฃผ์…”์„œ ์ดํ•ดํ•˜๊ธฐ ์ •๋ง ์‰ฌ์› ๋˜๊ฒƒ ๊ฐ™์•„์š”!!

์œ„์—์„œ .sort(reverse=True ์–ธ๊ธ‰ ํ•ด ์ฃผ์…จ๋Š”๋ฐ ๊ด€๋ จ๋ธ”๋กœ๊ทธ ๋งํฌ ์ฐธ๊ณ ํ•˜๋ฉด ๋„์›€ ๋˜์‹ค๊ฒƒ ๊ฐ™์•„์„œ ๋งํฌ ๊ฑธ์–ด๋‘๊ณ  ๊ฐ‘๋‹ˆ๋‹ค~ใ…Žใ…Ž
https://blog.naver.com/wideeyed/221745432175

5๋ฒˆ์งธ PR ์ˆ˜๊ณ ๋งŽ์œผ์…จ์–ด์š”!!

for _ in range(N-1):
vote.append(int(input()))

vote.sort(reverse=True) #๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜ค... ์ €๋„ sort(reverse=True)์ฒ˜์Œ๋ณด๋Š”๋ฐ ์ƒˆ๋กญ๊ฒŒ ์•Œ์•„๊ฐ€์š”..!!^^

Copy link
Member

@oesnuj oesnuj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด์„ ๋‚ ์ด์—ฌ์„œ ๊ตญํšŒ์˜์„  ์„ ๊ฑฐ ๋ฌธ์ œ๋ฅผ ํ‘ธ์…จ๊ตฐ์š” ใ…Žใ…Ž
๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ๋กœ ํ˜„์žฌ ๊ฐ€์žฅ ๋งŽ์€ ํ‘œ๋ฅผ ๊ฐ€์ง„ ํ›„๋ณด์ž๋ฅผ ์ฐพ์•„๋‚ด๋Š” ์›๋ฆฌ๋„ค์š”
์ฝ”๋“œ ์ž˜ ๋ดค์Šต๋‹ˆ๋‹ค!! ์ด๋ฒˆ PR๋„ ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค๐Ÿ‘

Copy link
Collaborator

@suhyun113 suhyun113 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋Šฆ๊ฒŒ ๋ฆฌ๋ทฐ ๋‹ฌ์•„์„œ ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค๐Ÿ˜ญ๐Ÿ˜ญ
์„ ๊ฑฐ๋‚ ์ด๋ผ ์„ ๊ฑฐ ๊ด€๋ จ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ผ๋‹ˆ,,, ์„ผ์Šค ๋Œ€๋ฐ•์ด์—์š”!
์„ ๊ฑฐ๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ๋ฌธ์ œ๋ผ๋‹ˆ..์‹ ๊ธฐํ•˜๋„ค์š”ใ…Žใ…Ž ๋ฌธ์ œ ์ž˜ ๋ดค์–ด์š”!

์ด ๋ฌธ์ œ์—์„œ๋Š” N๋ช…์˜ ํ›„๋ณด์ž ์ค‘ ๋‹ค์†œ์ด์˜ ํ‘œ๋ฅผ ๋‹ค๋ฅธ ํ›„๋ณด์ž๋“ค๋ณด๋‹ค ๋” ๋งŽ์ด ๋ฐ›๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์ตœ์†Œํ•œ์˜ ํ‘œ๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๋Š” ํšŸ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ด์•ผ ํ•˜๋Š”๋ฐ, ๋„ํ˜„๋‹˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ˆ, ์ง๊ด€์ ์œผ๋กœ ์ž˜ ์ดํ•ด๊ฐ€ ๋˜์—ˆ์–ด์š”!

๋”ฐ๋กœ ์ฐพ์•„๋ณด๋‹ˆ, ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋Š” ํž™์„ ์ด์šฉํ•ด์„œ๋„ ํ’€ ์ˆ˜ ์žˆ๋”๋ผ๊ตฌ์š”!
๋„ํ˜„๋‹˜์ด ์ฝ”๋“œ๋Š” ๊ฐ€์žฅ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ์„ ํ†ตํ•ด ๊ฐ€์žฅ ๋“ํ‘œ์ˆ˜๊ฐ€ ๋งŽ์€ ์‚ฌ๋žŒ์„ ๊ณ„์† ํ™•์ธํ•ด ๋งค์ˆ˜ํ•˜์—ฌ ๋‹ค์†œ์ด์˜ ๋“ํ‘œ์ˆ˜๋ฅผ ์˜ฌ๋ฆฌ๋Š” ๊ฒƒ์„ ๋ฐ˜๋ณตํ•˜์ž–์•„์š”? ๊ทธ๋Ÿฌ๋‹ค๊ฐ€ ์ œ์ผ ๋“ํ‘œ์ˆ˜๊ฐ€ ๋งŽ์€ ์‚ฌ๋žŒ์ด ๋‹ค์†œ์ด๋ณด๋‹ค ๋“ํ‘œ์ˆ˜๊ฐ€ ๋‚ฎ์•„์ง€๋Š” ์ˆœ๊ฐ„ ๋ฉˆ์ถ”๋Š” ๊ฒƒ์„ while๋ฌธ์œผ๋กœ ๊ตฌํ˜„ํ–ˆ์–ด์š”. ๋”ฐ๋ผ์„œ ๊ฐ€์žฅ ๋“ํ‘œ์ˆ˜๊ฐ€ ๋งŽ์€ ์‚ฌ๋žŒ์„ ์•Œ๊ณ  ์‹ถ์€ ๊ฒƒ์ธ๋ฐ, ๋งค๋ฒˆ ์ •๋ ฌํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” ์ตœ๋Œ€, ์ตœ์†Œ๋ฅผ ๊ณ„์† ์•Œ์•„์•ผ ํ•˜๋Š” ์ด๋Ÿฐ ๋ฌธ์ œ์— ์ข€ ๋” ์ ํ•ฉํ•œ ์ž๋ฃŒ๊ตฌ์กฐ๋กœ ํž™์ด ์žˆ์–ด์š”!

ํž™(์šฐ์„ ์ˆœ์œ„ ํ)์„ ์ด์šฉํ•˜๋ฉด, ๋‹ค์†œ์ด์˜ ์ฒ˜์Œ ๋“ํ‘œ์ˆ˜๋ฅผ ์ €์žฅํ•ด ๋‘์—ˆ๋‹ค๊ฐ€ ๋‹ค์†œ์ด ์™ธ์˜ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์˜ ๋“ํ‘œ์ˆ˜๋ฅผ ํž™์— pushํ•ด ๋‘๋Š” ๊ฒƒ์ด์—์š”. ๊ทธ ํ›„, ๋˜‘๊ฐ™์ด ๋‹ค์†œ์ด์˜ ๋“ํ‘œ์ˆ˜๊ฐ€ 1๋“ฑ์ด ๋  ๋•Œ๊นŒ์ง€ ํž™์—์„œ ๊ฐ€์žฅ ๋งŽ์€ ๋“ํ‘œ์ˆ˜๋ฅผ ๊ฐ€์ง„ ์‚ฌ๋žŒ์„ ๋ฝ‘์•„ ๊ทธ ์‚ฌ๋žŒ์˜ ํ‘œ๋ฅผ ๋งค์ˆ˜ํ•ด ๋‹ค์†œ์ด์˜ ๋“ํ‘œ์ˆ˜๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ์ฃผ๊ณ , ํž™์— ์ €์žฅ๋œ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ๋“ํ‘œ์ˆ˜๋ฅผ 1 ๊ฐ์†Œ์‹œํ‚จ ํ›„ ๋‹ค์‹œ ํž™์— pushํ•˜๋Š” ๊ฑฐ์—์š”.
๊ฒฐ๊ตญ, ๋„ํ˜„๋‹˜์˜ ๋ฐฉ์‹๊ณผ ๊ตฌํ˜„ ๋ฐฉ์‹์ด ๊ฑฐ์˜ ๋˜‘๊ฐ™์€ ๊ฒƒ์œผ๋กœ ๋ณด์ด์ง€๋งŒ, heap์„ ์ด์šฉํ•˜๋ฉด ๊ฐ€์žฅ ๋งŽ์€ ํ‘œ๋ฅผ ๊ฐ€์ง„ ํ›„๋ณด๋ฅผ ์ข€ ๋” ํšจ์œจ์ ์œผ๋กœ ์ฐพ๊ณ  ์—…๋ฐ์ดํŠธํ•  ์ˆ˜ ์žˆ์–ด์š”! ๋”ฐ๋ผ์„œ ๋ฆฌ์ŠคํŠธ์˜ ์ •๋ ฌ ์—†์ด๋„ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๋น ๋ฅด๊ฒŒ ์ฐพ์„ ์ˆ˜ ์žˆ์–ด ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ์ค„์ผ ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์–ด์š”.(ํž™ -> O(log N), ์ •๋ ฌ -> O(N log N))

๐Ÿง์ฐธ๊ณ ๐Ÿง
-ํž™ : ์™„์ „ ์ด์ง„ ํŠธ๋ฆฌ์˜ ์ผ์ข…์œผ๋กœ ์ด์ง„ํŠธ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ -> ์ตœ๋Œ€ ํž™๊ณผ ์ตœ์†Œ ํž™์œผ๋กœ ๋‚˜๋‰˜๋ฉฐ, ๋ถ€๋ชจ ์ž์‹ ๊ฐ„์˜ ๊ด€๊ณ„๊ฐ€ ๋Œ€์†Œ ๊ด€๊ณ„๋ฅผ ์ด๋ฃจ๊ณ  ์žˆ๋‹ค. ์ด์ง„ ํŠธ๋ฆฌ๋Š” ์ค‘๋ณต๊ฐ’์ด ํ—ˆ์šฉ๋˜์ง€ ์•Š์ง€๋งŒ, ํž™์€ ์ค‘๋ณต๋œ ๊ฐ’์ด ํ—ˆ์šฉ๋œ๋‹ค๋Š” ํŠน์ง•์„ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ, ํ•ญ์ƒ ๋ฃจํŠธ ๋…ธ๋“œ๊ฐ€ ๊ฐ€์žฅ ์ž‘๊ฑฐ๋‚˜ ๊ฐ€์žฅ ํฌ๋‹ค. ์ฆ‰, ํž™์€ ์ตœ๋Œ€, ์ตœ์†Œ๊ฐ’์„ ๋น ๋ฅด๊ฒŒ ์ฐพ๊ธฐ ์œ„ํ•ด ๊ณ ์•ˆ๋œ ์ž๋ฃŒ๊ตฌ์กฐ์ด๋‹ค.

๋‹ค์–‘ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ†ตํ•ด ํ‘ธ๋Š” ๊ฒƒ์ด ๋„์›€์ด ๋  ๊ฒƒ ๊ฐ™์•„ ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ฒจ๋ถ€ํ•ด ๋ด์š”!! ์ฐธ๊ณ ํ•ด๋ณด์‹œ๋ฉด ์ข‹์„ ๋“ฏ ํ•ด์š”

-ํž™์—์„œ ์ž๋ฃŒ๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ๋Š” heappush, ์ž๋ฃŒ๋ฅผ ์‚ญ์ œํ•  ๋•Œ๋Š” heappop์„ ์‚ฌ์šฉํ•˜๋ฉฐ, ์ง์ ‘ heap์„ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ํŒŒ์ด์ฌ์—์„œ ์ œ๊ณตํ•˜๋Š” heapq ๋ชจ๋“ˆ์„ ์ด์šฉํ•˜๋ฉด ๋” ๊ฐ„ํŽธํ•˜๊ณ  ๋น ๋ฅธ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

import heapq

N = int(input())

hq = []
for i in range(N):
    vote = int(input())
    if i == 0 : # ๊ธฐํ˜ธ 1๋ฒˆ ๋‹ค์†œ์ด์ธ ๊ฒฝ์šฐ
        dasom = vote # ๋‹ค์†œ์ด์˜ ๋“ํ‘œ์ˆ˜
        continue

    heapq.heappush(hq, -vote) # ๋‹ค์†œ์ด๋ฅผ ์ œ์™ธํ•œ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์˜ ๋“ํ‘œ์ˆ˜

cnt = 0 # ๋น„๊ต ํšŸ์ˆ˜
while hq:
    vote = -heapq.heappop(hq) # ํž™์— ์ €์žฅ๋œ ๋“ํ‘œ์ˆ˜ ์ฐจ๋ก€๋Œ€๋กœ ๋น„๊ต
    if vote < dasom: #(๋‹ค์†œ์ด์˜ ๋“ํ‘œ์ˆ˜๊ฐ€ 1๋“ฑ์ด ๋  ๋•Œ๊นŒ์ง€)
        break
    dasom += 1
    cnt += 1
    heapq.heappush(hq, -(vote - 1))
    
print(cnt)

@pu2rile pu2rile merged commit 1920fc3 into main Jul 11, 2024
@pu2rile pu2rile deleted the 5-pu2rile branch July 11, 2024 12:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants