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

6-pu2rile #25

Merged
merged 2 commits into from
Jul 8, 2024
Merged

6-pu2rile #25

merged 2 commits into from
Jul 8, 2024

Conversation

pu2rile
Copy link
Collaborator

@pu2rile pu2rile commented May 10, 2024

πŸ”— 문제 링크

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

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

20λΆ„

✨ μˆ˜λ„ μ½”λ“œ

  1. 문제 μš”μ•½
  • μ‚¬λžŒμ€ 1λ²ˆλΆ€ν„° Nλ²ˆκΉŒμ§€ λ²ˆν˜Έκ°€ 맀겨져 있으며, i번 μ‚¬λžŒμ΄ λˆμ„ μΈμΆœν•˜λŠ”λ° κ±Έλ¦¬λŠ” μ‹œκ°„μ€ PiλΆ„
  • ex. 총 5λͺ…이 있고, P1 = 3, P2 = 1, P3 = 4, P4 = 3, P5 = 2인 경우 3번 μ‚¬λžŒμ€ 1번, 2번 μ‚¬λžŒμ΄ λˆμ„ 뽑을 λ•ŒκΉŒμ§€ κΈ°λ‹€λ €μ•Ό ν•˜κΈ° λ•Œλ¬Έμ— 총 3+1+4 = 8뢄이 ν•„μš”
  • 이 κ²½μš°μ— 각 μ‚¬λžŒμ΄ λˆμ„ μΈμΆœν•˜λŠ”λ° ν•„μš”ν•œ μ‹œκ°„μ˜ 합은 3+4+8+11+13 = 39λΆ„
  • 쀄을 μ„œ μžˆλŠ” μ‚¬λžŒμ˜ 수 Nκ³Ό 각 μ‚¬λžŒμ΄ λˆμ„ μΈμΆœν•˜λŠ”λ° κ±Έλ¦¬λŠ” μ‹œκ°„ Piκ°€ μ£Όμ–΄μ‘Œμ„ λ•Œ, 각 μ‚¬λžŒμ΄ λˆμ„ μΈμΆœν•˜λŠ”λ° ν•„μš”ν•œ μ‹œκ°„μ˜ ν•©μ˜ μ΅œμ†Ÿκ°’μ„ κ΅¬ν•˜λΌ
  1. μ•Œκ³ λ¦¬μ¦˜
  • n 번째 μ‚¬λžŒμ΄ μΈμΆœν•  λ•Œ n 번째 μ‚¬λžŒ μ•žκΉŒμ§€ λͺ¨λ“  μ‚¬λžŒμ˜ μ‹œκ°„μ„ time에 더해 μ£Όκ³  μ΅œμ†Œλ‘œ λ‚˜μ˜€λŠ” resultλ₯Ό 좜λ ₯
  • P1 = 3, P2 = 1, P3 = 4, P4 = 3, P5 = 2인 경우, μ‹œκ°„μ΄ 적게 κ±Έλ¦¬λŠ” μ‹œκ°„ 순으둜 μ •λ ¬ν•˜μ—¬ [P2, P5, P1, P4, P3] 순으둜 λˆ„μ ν•©μ„ κ΅¬ν•˜λ©΄ 됨

πŸ–₯️ μ½”λ“œ

num = int(input())
arr = list(map(int, input().split())) 
arr.sort() #μž…λ ₯받은 μ •μˆ˜ μ˜€λ¦„μ°¨μˆœ μ •λ ¬
result = 0
time = 0
for i in range(num): 
    time += arr[i] #ν˜„μž¬ μ‚¬λžŒμ΄ κΈ°λ‹€λ¦¬λŠ” μ‹œκ°„μ„ κ³„μ‚°ν•΄μ„œ time에 더함
    result += time #ν˜„μž¬ μ‚¬λžŒκΉŒμ§€μ˜ 총 λŒ€κΈ° μ‹œκ°„μ„ 결과에 더함
print(result)

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

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번째 μ‚¬λžŒμ΄ μΈμΆœν•  λ•Œ κ±Έλ¦¬λŠ” μ‹œκ°„μ€ μ΄μ „μ˜ μ‚¬λžŒμ΄ κ±Έλ¦¬λŠ” μ‹œκ°„κΉŒμ§€ ν•©ν•˜μ—¬ κ³„μ‚°ν•˜κΈ° λ•Œλ¬Έμ— ν•©μ˜ μ΅œμ†Ÿκ°’μ„ κ΅¬ν•˜κΈ° μœ„ν•΄μ„œλŠ” μ•žμ˜ μ‚¬λžŒμ΄ κ±Έλ¦¬λŠ” μ‹œκ°„μ΄ 적을 수둝 μœ λ¦¬ν•΄ μ²˜μŒμ— μ˜€λ¦„μ°¨μˆœμœΌλ‘œ 정렬이 ν•„μš”ν•©λ‹ˆλ‹Ή. λ‚˜λ¨Έμ§€λŠ” μˆ˜ν•™μ μΈ κ°œλ…μ΄ λ“€μ–΄κ°„ 것 κ°™μ•„μš”!

저도 파이썬으둜 κ΅¬ν˜„ν•΄λ΄€λŠ”λ°, μ•„λž˜ μ½”λ“œ μ°Έκ³ ν•΄μ£Όμ„Έμš©~

p = int(input()) # ATM에 쀄을 μ„  μ‚¬λžŒ 수
pi = list(map(int, input().split())) # 각 μˆœμ„œμ˜ μ‚¬λžŒλ“€μ΄ λˆμ„ μΈμΆœν•˜λŠ”λ° κ±Έλ¦¬λŠ” μ‹œκ°„

pi.sort() # λˆμ„ μΈμΆœν•˜λŠ”λ° κ±Έλ¦¬λŠ” μ‹œκ°„μ΄ 적은 순으둜 μ •λ ¬

current_sum = 0 # ν˜„μž¬ μ‚¬λžŒκΉŒμ§€μ˜ λˆμ„ μΈμΆœν•˜λŠ”λ° κ±Έλ¦¬λŠ” μ‹œκ°„μ˜ ν•©
total_waiting_time = 0 # 전체 λˆμ„ μΈμΆœν•˜λŠ”λ° κ±Έλ¦¬λŠ” μ‹œκ°„μ˜ ν•©

for time in pi: 
    current_sum += time
    total_waiting_time += current_sum

print(total_waiting_time)

μ‹ κΈ°ν•œκ²Œ,,,λ„ν˜„λ‹˜μ΄λž‘ μ™„μ „ λ˜‘κ°™μ΄ μ§œμ‘Œμ–΄μš”γ…Žγ…Ž μ€€μ„œλ‹˜λ„ 같은 μ•Œκ³ λ¦¬μ¦˜μœΌλ‘œ ν’€μ—ˆλ‹€λ˜λ°

λ”°λ‘œ μ•Œμ•„λ΄€λŠ”λ°, μ’€ 더 μˆ˜ν•™μ μœΌλ‘œ μ ‘κ·Όν•΄λ³Ό 수 μžˆλ”λΌκ΅¬μš”.

for i, p in enumerate(pi):
    total_waiting_time += p * (p-i)

enumerate()ν•¨μˆ˜λ„ μ˜ˆμ „μ— λ°°μ› λ˜ 기얡이 μžˆλŠ”λ° 잘 κΈ°μ–΅λ‚˜μ§€ μ•Šλ”λΌκ΅¬μš”. μ•Œμ•„λ³΄λ‹ˆ 인자둜 λ„˜μ–΄μ˜¨ λͺ©λ‘μ„ κΈ°μ€€μœΌλ‘œ μΈλ±μŠ€μ™€ μ›μ†Œλ₯Ό μ°¨λ‘€λ‘œ μ ‘κ·Όν•΄μ£ΌλŠ” 반볡자 객체λ₯Ό λ°˜ν™˜ν•΄μ£ΌλŠ” ν•¨μˆ˜μ˜€μ–΄μš”! μ•Œκ³ κ³„μ‹€μ§€λ„ λͺ¨λ₯΄μ§€λ§Œ 참고해도 쒋을 것 κ°™μ•„μš”

두 방법 λͺ¨λ‘ μ‹œκ°„ λ³΅μž‘λ„κ°€ κ°™μ•„ μ–΄λŠ 것이 더 νš¨μœ¨μ μ΄λ‹€λΌκ³  λ§ν•˜κΈ°λŠ” μ–΄λ ΅μ§€λ§Œ,, 이런 방법도 μžˆμ–΄μ„œ μ‹ κΈ°ν–ˆμŠ΅λ‹ˆλ‹Ή
λŒ€μ‹  μˆ˜ν•™μ  풀이닀 λ³΄λ‹ˆ μ²˜μŒμ— κ΅¬ν˜„ν•œ μ½”λ“œλ³΄λ‹€ μ΄ν•΄λŠ” μ–΄λ €μ› μ–΄μš”. κ·Έλž˜λ„ ν•¨μˆ˜λ„ κ³΅λΆ€ν•˜κ²Œ λ˜μ—ˆκ³  이런 풀이도 μžˆλ‹€λŠ” 것을 μ•Œκ²Œλ˜μ–΄ μœ μ΅ν–ˆμŠ΅λ‹ˆλ‹€γ…

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.

3 participants