๐Ÿ˜Ž ๊ณต๋ถ€ํ•˜๋Š” ์ง•์ง•์•ŒํŒŒ์นด๋Š” ์ฒ˜์Œ์ด์ง€?

[Softeer] python ๊ธˆ๊ณ ํ„ธ์ด ๋ณธ๋ฌธ

๐Ÿฆฅ ์ฝ”ํ…Œ/Softeer

[Softeer] python ๊ธˆ๊ณ ํ„ธ์ด

์ง•์ง•์•ŒํŒŒ์นด 2023. 1. 6. 17:24
728x90
๋ฐ˜์‘ํ˜•

<๋ณธ ๋ธ”๋กœ๊ทธ๋Š” Softeer์˜ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๋ฌธ์ œ๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ๊ณต๋ถ€ํ•˜๋ฉฐ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค>

https://softeer.ai/practice/info.do?idx=1&eid=395 

 

Softeer

์—ฐ์Šต๋ฌธ์ œ๋ฅผ ๋‹ด์„ Set์„ ์„ ํƒํ•ด์ฃผ์„ธ์š”. ์ทจ์†Œ ํ™•์ธ

softeer.ai

 

โญ ๋ฌธ์ œ

์–ธ์–ด๋ณ„ ์‹œ๊ฐ„/๋ฉ”๋ชจ๋ฆฌ
Python 3์ดˆ 256MB

๋ฌธ์ œ
๋ฃจํŒก์€ ๋ฐฐ๋‚ญ์„ ํ•˜๋‚˜ ๋ฉ”๊ณ  ์€ํ–‰๊ธˆ๊ณ ์— ๋“ค์–ด์™”๋‹ค.
๊ธˆ๊ณ  ์•ˆ์—๋Š” ๊ฐ’๋น„์‹ผ ๊ธˆ, ์€, ๋ฐฑ๊ธˆ ๋“ฑ์˜ ๊ท€๊ธˆ์† ๋ฉ์–ด๋ฆฌ๊ฐ€ ์ž”๋œฉ ๋“ค์–ด์žˆ๋‹ค.
๋ฐฐ๋‚ญ์€ W ใŽ๊นŒ์ง€ ๋‹ด์„ ์ˆ˜ ์žˆ๋‹ค.

๊ฐ ๊ธˆ์†์˜ ๋ฌด๊ฒŒ์™€ ๋ฌด๊ฒŒ๋‹น ๊ฐ€๊ฒฉ์ด ์ฃผ์–ด์กŒ์„ ๋•Œ ๋ฐฐ๋‚ญ์„ ์ฑ„์šธ ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ๊ฐ’๋น„์‹ผ ๊ฐ€๊ฒฉ์€ ์–ผ๋งˆ์ธ๊ฐ€?
๋ฃจํŒก์€ ์ „๋™ํ†ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ ๊ท€๊ธˆ์†์€ ํ†ฑ์œผ๋กœ ์ž๋ฅด๋ฉด ์ž˜๋ ค์ง„ ๋ถ€๋ถ„์˜ ๋ฌด๊ฒŒ๋งŒํผ ๊ฐ€์น˜๋ฅผ ๊ฐ€์ง„๋‹ค.

์ œ์•ฝ์กฐ๊ฑด

1 ≤ N ≤ 106์ธ ์ •์ˆ˜
1 ≤ W ≤ 104์ธ ์ •์ˆ˜
1 ≤ Mi, Pi ≤ 104์ธ ์ •์ˆ˜

์ž…๋ ฅํ˜•์‹
์ฒซ ๋ฒˆ์งธ ์ค„์— ๋ฐฐ๋‚ญ์˜ ๋ฌด๊ฒŒ W์™€ ๊ท€๊ธˆ์†์˜ ์ข…๋ฅ˜ N์ด ์ฃผ์–ด์ง„๋‹ค.
i + 1 (1 ≤ i ≤ N)๋ฒˆ์งธ ์ค„์—๋Š” i๋ฒˆ์งธ ๊ธˆ์†์˜ ๋ฌด๊ฒŒ Mi์™€ ๋ฌด๊ฒŒ๋‹น ๊ฐ€๊ฒฉ Pi๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

์ถœ๋ ฅํ˜•์‹
์ฒซ ๋ฒˆ์งธ ์ค„์— ๋ฐฐ๋‚ญ์— ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ๋น„์‹ผ ๊ฐ€๊ฒฉ์„ ์ถœ๋ ฅํ•˜๋ผ.

์ž…๋ ฅ์˜ˆ์ œ1
100 290 170 2

์ถœ๋ ฅ์˜ˆ์ œ1
170

 

โœ… Code

# ๊ธˆ, ์€, ๋ฐฑ๊ธˆ ๋“ฑ์˜ ๊ท€๊ธˆ์† ๋ฉ์–ด๋ฆฌ
# ๋ฐฐ๋‚ญ์€ W ใŽ๊นŒ์ง€ ๋‹ด์„ ์ˆ˜ ์žˆ์Œ
# ๊ท€๊ธˆ์†์€ ํ†ฑ์œผ๋กœ ์ž๋ฅด๋ฉด ์ž˜๋ ค์ง„ ๋ถ€๋ถ„์˜ ๋ฌด๊ฒŒ๋งŒํผ ๊ฐ€์น˜๋ฅผ ๊ฐ€์ง

# ๊ฐ ๊ธˆ์†์˜ ๋ฌด๊ฒŒ์™€ ๋ฌด๊ฒŒ๋‹น ๊ฐ€๊ฒฉ์ด ์ฃผ์–ด์กŒ์„ ๋•Œ
# ๋ฐฐ๋‚ญ์„ ์ฑ„์šธ ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ๊ฐ’๋น„์‹ผ ๊ฐ€๊ฒฉ?

import sys

input = sys.stdin.readline

# ๋ฐฐ๋‚ญ ๋ฌด๊ฒŒ, ๊ท€๊ธˆ์† ์ข…๋ฅ˜
W, N = map(int, input().split())

table = []
for i in range(N):
    m, p = map(int, input().split())
    table.append((m, p))

# ๊ฐ€๊ฒฉ ๋น„์‹ผ ์ˆœ์„œ๋Œ€๋กœ -> [(70,2),(90,1)]
table.sort(key=lambda x: x[1], reverse=True)

answer = 0
# ๊ธˆ์†์˜ ๋ฌด๊ฒŒ Mi์™€ ๋ฌด๊ฒŒ๋‹น ๊ฐ€๊ฒฉ
for m, p in table:
    if W > m:
        answer += m * p  # ๋ณด์„ ๋ฌด๊ฒŒ * ๊ฐ€์น˜
        W -= m
    else:
        answer += W * p  # ์ž˜๋ผ๋‚ธ ๋ณด์„ ๋ฌด๊ฒŒ * ๊ฐ€์น˜
        break
print(answer)

 

728x90
๋ฐ˜์‘ํ˜•
Comments