BOJ/๐Ÿฅˆ

โ˜…Greedy Intermediate I - 20 Solvedโ˜…

metamong 2022. 12. 5.

๐Ÿง‍โ™‚๏ธ greedy ์ค‘๊ธ‰๋ฌธ์ œ๋Š” ์ „ํ˜•์ ์ธ greedy ๋Œ€ํ‘œ ์˜ˆ์ œ ๋ชจ์Œ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ์‰ฝ๋‹ค! greedy ์œ ํ˜•์€ ์ฃผ์–ด์ง„ ์ƒํ™ฉ์—์„œ ๋‹ค์–‘ํ•œ ์„ ํƒ์ง€๊ฐ€ ์žˆ์„ ๋•Œ ๋‹น์žฅ์˜ ์ตœ์ ์˜ ์„ ํƒ์ง€๋ฅผ ๊ตฌํ•œ ๊ฒฐ๊ณผ๋ฅผ ์—ฐ์†ํ•  ๋•Œ ๊ทธ ๊ฒฐ๊ณผ๊ฐ€ ๊ณง ์ตœ์ ์˜ ๊ฒฐ๊ณผ๊ฐ€ ๋œ๋‹ค๋Š” ๋‚ด์šฉ์ด๋‹ค. ์ด ๋•Œ ์ค‘์š”ํ•œ ๊ฑด, ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์ƒํ™ฉ์„ ์ •ํ•ด ๋†“๊ณ , ๊ทธ ์ƒํ™ฉ์—์„œ ์ตœ์ ์˜ ์ƒํ™ฉ์„ ์—ฐ์†ํ•ด์„œ ๊ตฌํ•œ ์ƒํ™ฉ์˜ ๊ฒฐ๊ณผ ๋ชจ์Œ์ด ๊ณง ์ „์ฒด์˜ ์ตœ์  ์ƒํ™ฉ์ด์–ด์•ผ ํ•œ๋‹ค๋Š” ์ „์ œ๊ฐ€ ๊น”๋ ค ์žˆ๋‹ค.


โ˜… 6752 Time on task โ˜…

 

T = int(input()) 
C = int(input()) 

times = [int(input()) for _ in range(C)]
n = 0

times.sort()

for time in times:
    T -= time
    if T < 0:
        break
    n += 1

print(n)

 

๐Ÿง‍โ™‚๏ธ greedy ๊ด€์  ๋ถ„์„

: ๊ฐœ๋ณ„ ์ƒํ™ฉ) chore๋ฅผ ์„ ํƒํ•ด ํ•œ ๊ฐœ์˜ chore๋ฅผ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๋๋‚ด๋Š” ์ƒํ™ฉ

: ์ข…ํ•ฉ ์ƒํ™ฉ) ์„ ํƒํ•œ chore๋ฅผ ๋ชจ๋‘ ๋๋‚ด๊ธฐ / ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ) ์ตœ๋Œ€ ๊ฐœ์ˆ˜์˜ chore๋ฅผ ๋ชจ๋‘ ๋๋‚ด๊ธฐ

 

โ˜… ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ์€ ์ตœ์ ์˜ ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ๋ชจ์Œ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ , ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ์ตœ์  greedy ์†”๋ฃจ์…˜์€ ๊ฐ€์žฅ ์‹œ๊ฐ„์ด ์ ๊ฒŒ ๊ฑธ๋ฆฌ๋Š” chore๋ถ€ํ„ฐ ๊ณจ๋ผ์„œ ์ง„ํ–‰(์‹œ๊ฐ„์ด ์ดˆ๊ณผ๋˜๋ฉด break)


โ˜… 16435 ์Šค๋„ค์ดํฌ๋ฒ„๋“œ โ˜…

 

N,L = map(int, input().split())

h = list(map(int, input().split()))

h.sort()

for each_h in h:
    if each_h > L:
        break

    L += 1

print(L)

 

๐Ÿง‍โ™‚๏ธ greedy ๊ด€์  ๋ถ„์„

: ๊ฐœ๋ณ„ ์ƒํ™ฉ) snake ๊ธธ์ด๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ณผ์ผ์ด ์žˆ์œผ๋ฉด ๋จน๊ณ  snake ๊ธธ์ด๊ฐ€ ๋Š˜์–ด๋‚˜๋Š” ์ƒํ™ฉ

: ์ข…ํ•ฉ ์ƒํ™ฉ) ๋จน์„ ์ˆ˜ ์žˆ๋Š” ๊ณผ์ผ์„ ๋ชจ๋‘ ๋จน์Œ / ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ) ๋จน์œผ๋ฉด์„œ ๋Š˜์–ด๋‚  ์ˆ˜ ์žˆ๋Š” snake์˜ ์ตœ๋Œ€ ๊ธธ์ด

 

โ˜… ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ์€ ์ตœ์ ์˜ ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ๋ชจ์Œ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ , ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ์ตœ์  greedy ์†”๋ฃจ์…˜์€ ๊ณผ์ผ ๋†’์ด๋ฅผ ์ •๋ ฌํ•œ ๋‹ค์Œ, ์ž‘์€ ๋†’์ด๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ๋จน์œผ๋ฉด์„œ 1์”ฉ ์ฆ๊ฐ€ํ•˜๋ฉด์„œ ์ตœ๋Œ€ํ•œ ๊ธธ์ด ๋Š˜๋ฆฌ๊ธฐ (๋ชป ๋จน๋Š” ๊ณผ์ผ์ด ๊ทธ ๋‹ค์Œ ์žˆ์œผ๋ฉด break)


โ˜… 1026 ๋ณด๋ฌผ โ˜…

 

input()
print(sum([x*y for x,y in zip(sorted(map(int,input().split()),reverse=True),sorted(map(int,input().split())))]))

 

๐Ÿง‍โ™‚๏ธ greedy ๊ด€์  ๋ถ„์„

: ๊ฐœ๋ณ„ ์ƒํ™ฉ) A[i] * B[i]

: ์ข…ํ•ฉ ์ƒํ™ฉ) ๊ฐ A[i]*B[i]์˜ ๋ชจ๋“  ํ•ฉ์ธ S ๊ตฌํ•˜๊ธฐ / ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ) S ์ตœ์†Ÿ๊ฐ’ ๊ตฌํ•˜๊ธฐ

 

โ˜… ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ์€ ์ตœ์ ์˜ ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ๋ชจ์Œ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ , ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ์ตœ์  greedy ์†”๋ฃจ์…˜์€ ๋จผ์ € A๋ฅผ ์ฆ๊ฐ€ / B๋ฅผ ๊ฐ์†Œ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ๋’ค, A์˜ ์ตœ๋Œ“๊ฐ’ x B์˜ ์ตœ์†Ÿ๊ฐ’ + A์˜ ๋‘๋ฒˆ์งธ ํฐ ๊ฐ’ x B์˜ ๊ทธ ๋‹ค์Œ ์ตœ์†Ÿ๊ฐ’... ์ˆœ์„œ๋กœ ๋”ํ•˜๊ธฐ

: ๋ณด์ถฉ์„ค๋ช…) B๊ฐ€ ๊ณ ์ •๋˜์–ด ์žˆ๋Š” ์ƒํƒœ์—์„œ B์˜ ์ตœ๋Œ“๊ฐ’๋ถ€ํ„ฐ ๋ณด์ž๋ฉด, S๋ฅผ ์ตœ์†Ÿ๊ฐ’์œผ๋กœ ๋งŒ๋“ค๋ ค๋ฉด greedyํ•˜๊ฒŒ ์ผ๋‹จ A์˜ ์ตœ์†Ÿ๊ฐ’์œผ๋กœ ๊ณฑํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. ๊ฐ€์žฅ ํฐ ์ˆ˜๊ฐ€ ๊ณฑํ•ด์ง€๋Š” ์ˆ˜๋Š” ์ตœ์†Ÿ๊ฐ’์ด์–ด์•ผ S๊ฐ€ ์ตœ์†Œ. (์–ด์ฐจํ”ผ ๋ชจ๋‘ A์™€ B ๋ชจ๋‘๊ฐ€ ์„ ํƒํ•ด์„œ ๊ณฑํ•ด์ง€๋Š” ์ƒํ™ฉ์—์„œ)

+) zip ํ™œ์šฉ


โ˜… 1789 ์ˆ˜๋“ค์˜ ํ•ฉ โ˜…

 

print(int(((8*int(input())+1)**(1/2)-1)/2))

 

๐Ÿง‍โ™‚๏ธ greedy ๊ด€์  ๋ถ„์„

: ๊ฐœ๋ณ„ ์ƒํ™ฉ) ์•ž์„œ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ์ž์—ฐ์ˆ˜๋ฅผ 1๊ฐœ ์ •ํ•ด ๋”ํ•˜๊ธฐ

: ์ข…ํ•ฉ ์ƒํ™ฉ) ์„œ๋กœ ๋‹ค๋ฅธ ์ž์—ฐ์ˆ˜๋ฅผ ๋ชจ๋‘ ๋”ํ•ด S๋กœ ๋งŒ๋“ค๊ธฐ / ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ) ์„œ๋กœ ๋‹ค๋ฅธ ์ž์—ฐ์ˆ˜์˜ ๊ฐœ์ˆ˜ N๊ฐœ์˜ ์ตœ๋Œ“๊ฐ’ ๊ตฌํ•˜๊ธฐ

 

โ˜… ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ์€ ์ตœ์ ์˜ ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ๋ชจ์Œ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ , ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ์ตœ์  greedy ์†”๋ฃจ์…˜์€ ๋”ํ•ด์ง€๋Š” ์ž์—ฐ์ˆ˜์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ตœ๋Œ€์ด๋ ค๋ฉด ์ตœ๋Œ€ํ•œ ์ž‘์€ ์ž์—ฐ์ˆ˜์ธ 1๋ถ€ํ„ฐ 2, 3, 4 ์ˆœ์œผ๋กœ ์ตœ๋Œ€ํ•œ ๋”ํ•ด ๊ฐœ์ˆ˜๋ฅผ ์ตœ๋Œ€ํ™”ํ•œ๋‹ค. ๊ทธ ๋‹ค์Œ ๋‚จ์€ ์ž์—ฐ์ˆ˜๊ฐ€ ์žˆ๋‹ค๋ฉด ๊ธฐ์กด์— ๋”ํ•œ ์ž์—ฐ์ˆ˜์™€ ๋”ํ•ด์„œ ์ œ์ผ ํฐ ๋˜ ๋‹ค๋ฅธ ์ž์—ฐ์ˆ˜ 1๊ฐœ๋กœ ํ•ฉ์น˜๋ฉด ์„œ๋กœ ๋‹ค๋ฅธ ์ž์—ฐ์ˆ˜ ๊ฐœ์ˆ˜ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. (1๋ถ€ํ„ฐ ์•ˆ๋”ํ•˜๊ณ  ์˜ˆ๋ฅผ ๋“ค์–ด 10์„ ๋”ํ•œ๋‹ค๋ฉด 10์— 1์ด ๋“ค์–ด๊ฐ€๋ฏ€๋กœ greedy ์„ฑ๋ฆฝ)

: ๋ณด์ถฉ์„ค๋ช…) ์˜ˆ๋ฅผ ๋“ค์–ด S๊ฐ€ 198์ด๋ผ๋ฉด 1๋ถ€ํ„ฐ 19๊นŒ์ง€ ์ญ‰ ๋”ํ•œ ๊ฒฐ๊ณผ๋Š” 190์ด๊ณ  8์ด ๋‚จ๋Š”๋‹ค. ๋‚จ๋Š” 8์€ ๋”ํ•œ ์ž์—ฐ์ˆ˜ ์ตœ๋Œ“๊ฐ’ 19์™€ ํ•ฉ์ณ 27์„ ๋งŒ๋“ค๋ฉด ๋จ. ์ฆ‰ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋Š” 19. ๋”ฐ๋ผ์„œ 1๋ถ€ํ„ฐ x๊นŒ์ง€ ๋”ํ•œ ๊ฒฐ๊ณผ๊ฐ€ S๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๋‹ค๋ฉด, x์˜ ์ตœ๋Œ“๊ฐ’์ด ์ •๋‹ต. (์•„๋ž˜ ์‹ ํ™•์ธ)

 

๐Ÿง‍โ™‚๏ธ

$ \frac{n(n+1)}{2} \leq S < \frac{(n+1)(n+2)}{2}$

 

: ์œ„ ์‹์„ ๋งŒ์กฑํ•˜๋Š” n ๊ตฌํ•˜๊ธฐ. n์— ๋Œ€ํ•ด ๋ถ€๋“ฑ์‹ ํ’€์–ด์„œ int() ํ•จ์ˆ˜ ๊ฒฐ๊ณผ๋กœ ๊ตฌํ•˜๋ฉด ๋

: ๋ถ€๋“ฑ์‹์„ ํ’€๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

 

$n\leq \frac{\sqrt{8S+1}-1}{2}$


โ˜… 1758 ์•Œ๋ฐ”์ƒ ๊ฐ•ํ˜ธ โ˜…

 

import sys
def input(): return sys.stdin.readline().rstrip()

total = int(input())
max_tip = 0
think = [int(input()) for _ in range(total)]

think.sort(reverse=True)

for i in range(total):
    tip = think[i] - (i+1) + 1
    if tip > 0:
        max_tip += tip
    else:
        break

print(max_tip)

 

๐Ÿง‍โ™‚๏ธ greedy ๊ด€์  ๋ถ„์„

: ๊ฐœ๋ณ„ ์ƒํ™ฉ) ์ •ํ•ด์ง„ ์‚ฌ๋žŒ๋ณ„๋กœ ๋“ฑ์ˆ˜๋ฅผ ์ ์ ˆํžˆ ์ •ํ•˜๊ธฐ

: ์ข…ํ•ฉ ์ƒํ™ฉ) ์‚ฌ๋žŒ๋ณ„ ์ •ํ•ด์ง„ ๋“ฑ์ˆ˜์— ๋งž๊ฒŒ tip ๋ถ€์—ฌํ•ด์„œ ์ตœ์ข… tip ํ•ฉ๊ณ„ ๊ตฌํ•˜๊ธฐ / ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ) ์ตœ๋Œ“๊ฐ’ tip ๊ตฌํ•˜๊ธฐ

 

โ˜… ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ์€ ์ตœ์ ์˜ ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ๋ชจ์Œ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ , ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ์ตœ์  greedy ์†”๋ฃจ์…˜์€ ๊ฐ ์‚ฌ๋žŒ๋ณ„๋กœ [์ƒ๊ฐํ•œ tip - ๋“ฑ์ˆ˜ + 1]์˜ ํ•ฉ์ด ์ตœ๋Œ“๊ฐ’์ด ๋˜์–ด์•ผ ํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ ์ƒ๊ฐํ•œ tip๊ณผ 1์€ ์ •ํ•ด์ ธ ์žˆ์œผ๋ฏ€๋กœ ์ƒ๊ฐํ•œ tip์ด ๋งŽ์€ ์‚ฌ๋žŒ์ผ์ˆ˜๋ก ๋“ฑ์ˆ˜๋ฅผ ์ตœ์†Œํ™”ํ•ด์•ผ ํ•œ๋‹ค.


โ˜… 11047 ๋™์ „ 0 โ˜…

 

import sys
input=sys.stdin.readline
N,K=map(int, input().split())
a=0
l=[i for i in [int(input()) for _ in range(N)]if i<=K]
for A in l[::-1]:
    a+=(K//A)
    K%=A
    if K==0:break
print(a)

 

๐Ÿง‍โ™‚๏ธ greedy ๊ด€์  ๋ถ„์„

: ๊ฐœ๋ณ„ ์ƒํ™ฉ) ๋™์ „์˜ ์ข…๋ฅ˜๋ฅผ ์ •ํ•˜๊ธฐ

: ์ข…ํ•ฉ ์ƒํ™ฉ) K์›์„ ๋งŒ๋“ค๊ธฐ / ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ) K์›์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ๋™์ „์˜ ๊ฐœ์ˆ˜ ์ตœ์†Œํ™”

 

โ˜… ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ์€ ์ตœ์ ์˜ ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ๋ชจ์Œ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ , ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ์ตœ์  greedy ์†”๋ฃจ์…˜์€ ๋™์ „์˜ ์ข…๋ฅ˜ ๊ฐ€์น˜๊ฐ€ ๋†’์€ ๊ฒƒ๋ถ€ํ„ฐ ๋จผ์ € ์นด์šดํŠธ (์ด ๋•Œ ๋ชจ๋“  ๋™์ „์˜ ์ข…๋ฅ˜ ๊ฐ€์น˜: ํฐ ๊ฐ€์น˜๊ฐ€ ์ž‘์€ ๊ฐ€์น˜์˜ ๋ฐฐ์ˆ˜์ด๋ฏ€๋กœ ๋‹น์žฅ ํฐ ์ข…๋ฅ˜๋ฅผ ์„ ํƒํ•œ ์ƒํ™ฉ์ด greedy๋กœ ์„ฑ๋ฆฝ)


โ˜… 15761 Lemonade Line โ˜…

 

N = int(input())
ws = list(map(int, input().split()))
ws.sort(reverse=True)
front_num = 0
print(ws)
for i in range(N):
    if ws[i] >= front_num:
        front_num += 1

print(front_num)
#-----------------------------
N = int(input())
ws = list(map(int, input().split()))
ws.sort(reverse=True)
front_num = 0
for i in range(N):
    if ws[i]<front_num:
        break
    front_num += 1

print(front_num)

 

๐Ÿง‍โ™‚๏ธ greedy ๊ด€์  ๋ถ„์„

: ๊ฐœ๋ณ„ ์ƒํ™ฉ) cow wi๊ฐ€ ๊ฐˆ ์ง€ ๋ง ์ง€ ๊ฒฐ์ •

: ์ข…ํ•ฉ ์ƒํ™ฉ) line์— ์„œ๋Š” cow๋“ค / ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ) line์— ์„  cow์˜ ์ตœ์†Œ ๊ฐœ์ˆ˜

 

โ˜… ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ์€ ์ตœ์ ์˜ ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ๋ชจ์Œ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ , ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ์ตœ์  greedy ์†”๋ฃจ์…˜์€ wi๊ฐ€ ๋†’์€ cow๋ถ€ํ„ฐ ๋จผ์ € line up

: ๋ณด์ถฉ์„ค๋ช…) ๋Šฅ๋ ฅ์ด ํฐ ์ˆœ์„œ๊ฐ€ ๋จผ์ € ์˜ค๊ฒŒ๋” ํ•ด์„œ, ๋Šฅ๋ ฅ์ด ์ž‘์€ ์†Œ๊ฐ€ ํฐ ์†Œ์— ๋น„ํ•ด ๋จผ์ € ๋– ๋‚  ํ™•๋ฅ ์ด ๋†’๊ธฐ ๋•Œ๋ฌธ (๋Šฅ๋ ฅ์ด ์ž‘์€ ์†Œ์ผ์ˆ˜๋ก ์•ž์— ์„  ์†Œ์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ •ํ•ด์ ธ ์žˆ๋‹ค๋ฉด ๋– ๋‚  ํ™•๋ฅ ์ด ๋ฌด์กฐ๊ฑด ๋†’๊ธฐ ๋•Œ๋ฌธ์—)

+ ํ•œ ๋ฒˆ ์†Œ๊ฐ€ ๋– ๋‚œ๋‹ค๋ฉด ๊ทธ ๋’ค์˜ ์†Œ๋Š” wi๊ฐ€ ์ž‘์•„์ง€๋ฏ€๋กœ ๋ฌด์กฐ๊ฑด ์•ž์— ์„  ์†Œ์˜ ๊ฐœ์ˆ˜๋ณด๋‹ค ์ž‘๊ธฐ ๋•Œ๋ฌธ์— ๋– ๋‚จ → break ์„ค์ •


โ˜… 2839 ์„คํƒ• ๋ฐฐ๋‹ฌ โ˜…

 

N=int(input())
y=0
for i in range(N//5,-1,-1):
    if (N-5*i)%3==0:
        y+=1
        print(i+(N-5*i)//3)
        break
if y==0:print(-1)

 

๐Ÿง‍โ™‚๏ธ greedy ๊ด€์  ๋ถ„์„

: ๊ฐœ๋ณ„ ์ƒํ™ฉ) ์„คํƒ• ๋ฐฐ๋‹ฌํ•  ๋•Œ 3kg / 5kg ๋ด‰์ง€ ์ค‘ 1๊ฐœ ์„ ํƒํ•ด์„œ ๋ฐฐ๋‹ฌํ•˜๊ธฐ

: ์ข…ํ•ฉ ์ƒํ™ฉ) ์ •ํ™•ํžˆ Nkg ๋ฐฐ๋‹ฌ / ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ) ์ตœ์†Œ ๋ด‰์ง€์ˆ˜๋กœ ๋ฐฐ๋‹ฌํ•˜๊ธฐ

 

โ˜… ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ์€ ์ตœ์ ์˜ ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ๋ชจ์Œ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ , ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ์ตœ์  greedy ์†”๋ฃจ์…˜์€ ๋ด‰์ง€์ˆ˜๊ฐ€ ์ตœ์†Ÿ๊ฐ’์ด ๋˜์–ด์•ผ ํ•œ๋‹ค. ๋จผ์ € 5kg์„ ์ตœ๋Œ€ ์ˆ˜๋กœ ๋ฐฐ๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ์ง€ 5kg์œผ๋กœ ๋‚˜๋ˆˆ ๋ชซ๋ถ€ํ„ฐ 1๊นŒ์ง€ ์ฐจ๋ก€๋Œ€๋กœ ๊ฐ์†Œํ•˜๋ฉฐ 5kg์„ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋Š” 3kg๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋Š” ์ง€ ํ™•์ธ (ํ™•์ธ๋˜๋ฉด break)


โ˜… 14916 ๊ฑฐ์Šค๋ฆ„๋ˆ โ˜…

 

n=int(input())
flag=False
for i in range(n//5,-1,-1):
    if (n-i*5)%2==0:
        flag=True
        print(i+(n-i*5)//2)
        break
if not flag: print(-1)

 

๐Ÿง‍โ™‚๏ธ ์œ„ [2839 ์„คํƒ•๋ฐฐ๋‹ฌ] ๋ฌธ์ œ์™€ ์™„์ „ํžˆ ๋˜‘๊ฐ™์€ ๋ฌธ์ œ. 2์›๋ณด๋‹ค 5์›์ด ์šฐ์„ ์‹œ๋˜์–ด์•ผ ๋ด‰์ง€ ๊ฐœ์ˆ˜๊ฐ€ ์ตœ์†Œ์ด๋ฏ€๋กœ 5์› ๋จผ์ € ์•Œ์•„๋ณด๋Š” greedy. ์„ค๋ช… ์ƒ๋žต


โ˜… 1246 ์˜จ๋ผ์ธ ํŒ๋งค โ˜…

 

import sys
def input(): return sys.stdin.readline().rstrip()

N,M = map(int, input().split())
num=M
Ps = [int(input()) for _ in range(M)]
Ps.sort()

sug_price,max_profit=0,0
for price in Ps:
    n=min(num,N)
    if n*price > max_profit:
        max_profit = n*price
        sug_price = price
    num-=1
print(sug_price, max_profit)

 

๐Ÿง‍โ™‚๏ธ greedy ๊ด€์  ๋ถ„์„

: ๊ฐœ๋ณ„ ์ƒํ™ฉ) ๊ฐ ๊ณ ๊ฐ๋ณ„ Pi๊ฐ€ ๊ฒฝ๋ž˜๊ฐ€ ์ฑ…์ •ํ•œ A ๊ฐ€๊ฒฉ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๋‹ค๋ฉด ๊ตฌ๋งค 

: ์ข…ํ•ฉ ์ƒํ™ฉ) ๊ณ ๊ฐ ์ผ๋ถ€๋Š” ์‚ฌ๊ณ , ์ผ๋ถ€๋Š” ์‚ฌ์ง€ ์•Š์•„์„œ ์ˆ˜์ต ๋‚ด๊ธฐ / ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ) ์ข…ํ•ฉ ์ตœ๋Œ€ ์ˆ˜์ต์˜ ๊ฐ€์žฅ ๋‚ฎ์€ A ๊ฐ€๊ฒฉ

 

โ˜… ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ์€ ์ตœ์ ์˜ ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ๋ชจ์Œ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ , ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ์ตœ์  greedy ์†”๋ฃจ์…˜์€ ์ˆ˜์ต์€ ์ตœ๋Œ€ / ๊ฐ€๊ฒฉ A๋Š” ์ตœ์†Œ. Pi ์ •๋ ฌํ•˜๊ณ  ๋‚ฎ์€ Pi๋ถ€ํ„ฐ Pi x (min(๋‹ฌ๊ฑ€๊ฐœ์ˆ˜, ์ฑ…์ •๊ฐ€๊ฒฉ์— ๋”ฐ๋ฅธ ๊ตฌ๋งค ๊ฐ€๋Šฅ์ธ์›์ˆ˜))์˜ ์ตœ๋Œ“๊ฐ’ update

: ์ฃผ์–ด์ง„ Pi๋งŒ(*greedy) ํ™•์ธ → ๋‹ฌ๊ฑ€ ๊ฐœ์ˆ˜์™€ ๊ตฌ๋งค ๊ฐ€๋Šฅ์ธ์›์ˆ˜ ์ •ํ•ด์ง„ ์ƒํƒœ์—์„œ Pi๊ฐ€ ๋ฌด์กฐ๊ฑด ์ตœ๋Œ€์ด๋ฏ€๋กœ


โ˜… 11399 ATM โ˜…

 

import sys
input=sys.stdin.readline
input()
P=list(map(int,input().split()))
P.sort()
ans,a=0,0
for t in P:
    a+=t
    ans+=a
print(ans)

 

๐Ÿ‡ greedy ๊ด€์  ๋ถ„์„

: ๊ฐœ๋ณ„ ์ƒํ™ฉ) ATM ์ธ์ถœ

: ์ข…ํ•ฉ ์ƒํ™ฉ) ๋ชจ๋‘ ์ธ์ถœํ•˜๊ณ  ๋‚œ ๋’ค์˜ ์ตœ์ข… ์‹œ๊ฐ„ / ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ) ์ตœ์ข… ์‹œ๊ฐ„ ์ตœ์†Ÿ๊ฐ’

 

โ˜… ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ์€ ์ตœ์ ์˜ ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ๋ชจ์Œ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ , ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ์ตœ์  greedy ์†”๋ฃจ์…˜์€ ๋นจ๋ฆฌ ์ธ์ถœํ•  ์ˆ˜ ์žˆ๋Š” ์‚ฌ๋žŒ์„ ๋จผ์ €(Pi๊ฐ’์ด ์ž‘์€ ์ˆœ) ์ธ์ถœํ•˜๊ฒŒ๋” ํ•ด์•ผ ์ตœ์ข… ์‹œ๊ฐ„ ์ตœ์†Œ


โ˜… 20044 Project Teams โ˜…

 

import sys
input=sys.stdin.readline

n = int(input())
s = sorted(list(map(int, input().split())))

w = s[0] + s[-1]
l = len(s)

for i in range(l//2):
    w = min(w, s[i] + s[l-1-i])

print(w)

 

๐Ÿง‍โ™‚๏ธ greedy ๊ด€์  ๋ถ„์„

: ๊ฐœ๋ณ„ ์ƒํ™ฉ) 2์ธ 1์กฐ๋กœ ํŒ€์— ๋“ค์–ด๊ฐˆ ์ฝ”๋”ฉ ์—ญ๋Ÿ‰ 2๊ฐœ ์„ ํƒ

: ์ข…ํ•ฉ ์ƒํ™ฉ) ๋ชจ๋“  ํŒ€์˜ ๊ฐ๊ฐ์˜ ํŒ€ ์ฝ”๋”ฉ ์—ญ๋Ÿ‰ / ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ) ๊ฐ๊ฐ์˜ ํŒ€ ์ฝ”๋”ฉ ์—ญ๋Ÿ‰ ์ค‘ ์ตœ์†Ÿ๊ฐ’

 

โ˜… ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ์€ ์ตœ์ ์˜ ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ๋ชจ์Œ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ , ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ์ตœ์  greedy ์†”๋ฃจ์…˜์€ ํŒ€ ์ฝ”๋”ฉ ์—ญ๋Ÿ‰์ด ์ตœ๋Œ€ํ•œ ํŒ€๋ณ„๋กœ ์ผ์ •ํ•˜๊ฒŒ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ •๋ ฌํ•ด ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ์—์„œ๋ถ€ํ„ฐ ๊ฐ๊ฐ ์‹œ์ž‘ํ•ด ์ตœ์†Ÿ๊ฐ’๊ณผ ์ตœ๋Œ“๊ฐ’์˜ ํ•ฉ์œผ๋กœ ๋ฌถ์–ด ํŒ€์„ ๋งŒ๋“ค์–ด ๋‚˜๊ฐ„๋‹ค. (๋น„๋‘˜๊ธฐ ์ง‘์˜ ์›๋ฆฌ์— ์˜ํ•ด, ์ด์— ๋ฐ˜ํ•˜๊ฒŒ ํŒ€์„ ๊ตฌ์„ฑํ•˜๋ฉด ์ ์–ด๋„ ํ•œ ํŒ€์€ ๋‹ค๋ฅธ ํŒ€์— ๋น„ํ•ด ๋‘ ๋ช… ๋ชจ๋‘ ํŒ€์˜ ์—ญ๋Ÿ‰์ด ์ ๊ฒŒ ์‚ฐ์ถœ๋œ๋‹ค.)

 

โ€ป ์ฆ‰, ์œ„ ๋ฌธ์ œ์˜ $S_m$๊ฐ’์ด ์ตœ๋Œ€๊ฐ€ ๋˜๊ธฐ ์œ„ํ•ด์„  ์ตœ๋Œ€ํ•œ ํŒ€๋ผ๋ฆฌ ํ•ฉ์˜ ์ฐจ์ด๊ฐ€ ์•ˆ๋‚˜๋„๋ก ๊ณตํ‰ํ•˜๊ฒŒ ์–‘์ชฝ์—์„œ ๊ฐ€์šด๋ฐ๋กœ ์ง„ํ–‰ํ•˜๋ฉฐ ํ•ฉ์„ ๊ตฌํ•ด์•ผ ํ•จ

(์ง๊ด€์  ์ฆ๋ช…์ด ๋‹ค์†Œ ๊ฐ€๋ฏธ๋จ..!)


โ˜… 1448 ์‚ผ๊ฐํ˜• ๋งŒ๋“ค๊ธฐ โ˜…

 

import sys
input = sys.stdin.readline

N = int(input())
y = -1
straws = [int(input()) for i in range(N)]
straws.sort(reverse=True)

for i in range(N-2):
    if straws[i] < straws[i+1] + straws[i+2]:
        y = straws[i]+straws[i+1]+straws[i+2]
        break
print(y)

 

๐Ÿง‍โ™‚๏ธ ์‚ผ๊ฐํ˜•์˜ ๊ฒฐ์ • ์กฐ๊ฑด: ์ œ์ผ ๊ธด ๋ณ€ < (๋‚˜๋จธ์ง€ ๋‘ ๋ณ€์˜ ํ•ฉ)

 

๐Ÿง‍โ™‚๏ธ greedy ๊ด€์  ๋ถ„์„

: ๊ฐœ๋ณ„ ์ƒํ™ฉ) ์‚ผ๊ฐํ˜•์œผ๋กœ ๋งŒ๋“ค ์„ธ ๊ฐœ์˜ ๋ณ€ ์„ ํƒ

: ์ข…ํ•ฉ ์ƒํ™ฉ) ์„ ํƒํ•œ ์„ธ ๋ณ€์ด ์‚ผ๊ฐํ˜•์ด ๋˜๋Š” ์ง€ ํ™•์ธ / ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ) ๋งŒ๋“ค์–ด์ง„ ์‚ผ๊ฐํ˜• ์„ธ ๋ณ€์˜ ํ•ฉ ์ตœ๋Œ“๊ฐ’

 

โ˜… ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ์€ ์ตœ์ ์˜ ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ๋ชจ์Œ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ , ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ์ตœ์  greedy ์†”๋ฃจ์…˜์€ ๋จผ์ € ์ •๋ ฌ(reverse=True)ํ•œ ๋’ค(greedy), ์ตœ๋Œ“๊ฐ’๋ถ€ํ„ฐ ๊ทธ ๋‹ค์Œ ๋‘ ๊ฐœ์˜ ๋ณ€์˜ ํ•ฉ์ด ๋” ํฐ ์ง€ ํ™•์ธ (์ด ๋•Œ, ๋” ํฌ์ง€ ์•Š๋‹ค๊ณ  ๊ฒฐ๋ก ์ด ๋‚ฌ์œผ๋ฉด ๊ทธ ์ดํ›„์˜ ์ˆ˜๋Š” ๋” ์ž‘์œผ๋ฏ€๋กœ ๋‹น์—ฐํžˆ ์‚ผ๊ฐํ˜•์˜ ๊ฒฐ์ •์กฐ๊ฑด์„ ๋งŒ์กฑ์‹œํ‚ฌ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ์œ„ for๋ฌธ i index ๋ฐ”๋กœ ์ด๋™: greedy)

∴ ์œ„ ๋‘ greedyํ•œ ์ƒํ™ฉ์„ ์„ค์ •ํ•ด ๋ฉ”๋ชจ๋ฆฌ, ์‹œ๊ฐ„ ์ดˆ๊ณผ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค! (greedy์˜ ์žฅ์ )


โ˜… 13305 ์ฃผ์œ ์†Œ โ˜…

 

 

 

๐Ÿง‍โ™‚๏ธ greedy ๊ด€์  ๋ถ„์„

: ๊ฐœ๋ณ„ ์ƒํ™ฉ) ๊ฐ ๊ตฌ๊ฐ„ ๊ฑฐ๋ฆฌ๋ณ„ ์„ ํƒํ•  ์ฃผ์œ ์†Œ

: ์ข…ํ•ฉ ์ƒํ™ฉ) ๋ชฉ์ ์ง€๊นŒ์ง€ ๋„์ฐฉ ํ›„ ์ง€๋ถˆํ•œ ๊ธˆ์•ก / ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ) ์ง€๋ถˆ ๊ธˆ์•ก์ด ์ตœ์†Œ

 

โ˜… ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ์€ ์ตœ์ ์˜ ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ๋ชจ์Œ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ , ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ์ตœ์  greedy ์†”๋ฃจ์…˜์€ ์–ด์ฐจํ”ผ ๊ตฌ๊ฐ„ ๊ฑฐ๋ฆฌ๋Š” ๋ชจ๋‘ mulsum์œผ๋กœ ๊ณฑํ•ด์ง€๋ฏ€๋กœ, ์šฐ๋ฆฌ๊ฐ€ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋Š” ๊ฑด ์ฃผ์œ ์†Œ ๊ฐ€๊ฒฉ. ์ข…ํ•ฉ ๊ธˆ์•ก์ด ์ตœ์†Œ์ผ๋ ค๋ฉด ํ˜„์žฌ ๊ตฌ๊ฐ„๊นŒ์ง€์˜ ์ฃผ์œ ์†Œ ๊ฐ€๊ฒฉ์„ ์ตœ์†Ÿ๊ฐ’์œผ๋กœ updateํ•˜๋ฉด์„œ ๊ณ„์† ๊ฐ€๊ฒฉ์„ ๊ตฌํ•ด๋‚˜๊ฐ

import sys
input=sys.stdin.readline

N = int(input())

t=0

dist=list(map(int, input().split()))
price=list(map(int, input().split()))

cur_min_price=price[0]
price=price[:-1]

for i in range(N-1):   
    if price[i]<cur_min_price:
        cur_min_price=price[i]
        t+=price[i]*dist[i]
    else:t+=cur_min_price*dist[i]

print(t)

โ˜… 1343 ํด๋ฆฌ์˜ค๋ฏธ๋…ธ โ˜…

 

import sys
input = sys.stdin.readline

board = list(input())
length = len(board)
Xs,ans=[],[]
cnt = 0
possible = True

for i in range(length):
    if board[i] == 'X':
        cnt += 1
    else:
        Xs.append(cnt)
        cnt = 0
    if i == (length-1) and cnt != 0:
        Xs.append(cnt)

for i in range(len(Xs)):
    if Xs[i]%2 == 1:
        possible = False
        print(-1)
        break
    if (Xs[i]//4) != 0:
        ans.append('AAAA'*(Xs[i]//4))
    if (Xs[i]%4) != 0:
        ans.append('BB'*((Xs[i]%4)//2))
    if i != (len(Xs)-1):
        ans.append('.')

if possible: print(*ans,sep='')

 

๐Ÿง‍โ™‚๏ธ ์‚ฌ์ „์ˆœ์œผ๋กœ ๊ฐ€์žฅ ์•ž์„œ๋Š” ๋‹ต์„ ์ถœ๋ ฅํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๋‹น์žฅ์˜ ์„ ํƒ์ง€ (1) AAAA (2) BB ์ค‘์—์„œ (1)์„ ๋ฌด์กฐ๊ฑด ๊ณจ๋ผ์•ผ ํ•œ๋‹ค! ๋‹ค๋งŒ, ์ด ๋•Œ ๋ณด๋“œํŒ์„ ๋ฎ์„ ๋•Œ ๋ช‡๊ฐ€์ง€ ์ƒ๊ฐํ•  ์ƒํ™ฉ๋งŒ ๊ณ ๋ คํ•ด์„œ ์ฝ”๋”ฉํ•ด์ฃผ๋ฉด ๋!

 

๐Ÿง‍โ™‚๏ธAAAA๋กœ ๋ฎ์„ ๋•Œ ๊ณ ๋ ค ์ƒํ™ฉ

(1) ๋จผ์ € X๋ฌถ์Œ๋‚ด์˜ X๊ฐœ์ˆ˜๋ฅผ ์ญ‰ ์•Œ์•„์•ผ ํ•œ๋‹ค. (.์„ ๋งŒ๋‚˜๋ฉด ๊ทธ ์ „๊นŒ์ง€ X ๋ฌถ์Œ์ด ์ƒ์„ฑ)

(2) X๊ฐœ์ˆ˜๊ฐ€ ๋“ค์–ด๊ฐ„ Xs๋ฅผ list๋กœ ๋Œ๋ฆฌ๋ฉด์„œ

→ X์˜ ๊ฐœ์ˆ˜๊ฐ€ ํ™€์ˆ˜๋ฉด ๋ณด๋“œํŒ์„ AAAA, BB๋กœ ๋ฎ์„ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ -1 ์ถœ๋ ฅ. ๋ฐ”๋กœ break

→ X์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ง์ˆ˜์ผ ๊ฒฝ์šฐ 4๋กœ ๋‚˜๋ˆˆ ๋ชซ ๊ฐœ์ˆ˜๋งŒํผ AAAA๋กœ ๋จผ์ € ๋ฎ๊ณ (greedy), 4๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ€ 2์ผ ๊ฒฝ์šฐ BB๋ฅผ ๊ทธ ๋‹ค์Œ ๋ฎ์–ด์ค€๋‹ค.

(3) ํ•œ๋ฒˆ X๋ฌถ์Œ์„ ๋ชจ๋‘ ๋ฎ๊ณ  ๋‚˜๋ฉด .์œผ๋กœ ๋งˆ๋ฌด๋ฆฌ

 

๐Ÿง‍โ™‚๏ธ greedy ๊ด€์  ๋ถ„์„

: ๊ฐœ๋ณ„ ์ƒํ™ฉ) ๊ฐ ๋ฌธ์ž๋ฅผ A๋กœ ๋ฐ”๊พธ๊ฑฐ๋‚˜ B๋กœ ๋ฐ”๊พธ๊ฑฐ๋‚˜ ๊ทธ๋Œ€๋กœ ๋†”๋‘๊ฑฐ๋‚˜

: ์ข…ํ•ฉ ์ƒํ™ฉ) ์ตœ์ข…์ ์œผ๋กœ ๋ฐ”๋€ ๋ณด๋“œํŒ / ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ) ๋ฐ”๋€ ๋ณด๋“œํŒ์ด ์‚ฌ์ „์ˆœ์œผ๋กœ ์ œ์ผ ์•ž์„œ๋Š” ๊ฒฝ์šฐ ์ถœ๋ ฅ

 

โ˜… ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ์€ ์ตœ์ ์˜ ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ๋ชจ์Œ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ , ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ์ตœ์  greedy ์†”๋ฃจ์…˜์€ ์ตœ๋Œ€ํ•œ AAAA๋กœ ๋ฎ๊ธฐ(X๊ฐ€ ํ™€์ˆ˜์ด๋ฉด ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ -1, ์ง์ˆ˜๋ผ๋ฉด 4๋กœ ๋‚˜๋ˆˆ ๋ชซ์€ A๋กœ 4๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€(์ฆ‰ 2๊ฐœ)๋Š” B๋กœ ๋ฎ๊ธฐ

 

๐Ÿง‍โ™‚๏ธ ans list์— ๊ณ„์† ๊ฒฐ๊ณผ๋ฅผ appendํ•˜๋ฉด์„œ ์ตœ์ข… ๊ฒฐ๊ณผ ์ถœ๋ ฅ!

 

++ ์ถ”๊ฐ€๋กœ python replace ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด ํ•œ๋ฒˆ์— ํ’€ ์ˆ˜ ์žˆ๋‹ค. XXXX๋จผ์ € AAAA๋กœ ๊น”๊ณ  ๋‚จ์€ XX๋ฅผ BB๋กœ ๊น๋‹ค. ๊ทธ๋ž˜๋„ X๊ฐ€ ๋‚จ์œผ๋ฉด -1 ์ถœ๋ ฅ

board = input().replace('XXXX', 'AAAA').replace('XX', 'BB')

print(-1 if 'X' in board else board)

โ˜… 12845 ๋ชจ๋‘์˜ ๋งˆ๋ธ” โ˜…

 

import sys
input=sys.stdin.readline

n=int(input())
L=list(map(int,input().split()))
print(max(L)*(n-1)+sum(L)-max(L))

 

๐Ÿง‍โ™‚๏ธ ์ธ์ ‘ํ•œ ์นด๋“œ ์Œ์˜ ๊ฐœ์ˆ˜๋Š” ์ •ํ•ด์ ธ ์žˆ๋‹ค. ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•œ ํ›„, ์ตœ๋Œ“๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์–‘์˜†์œผ๋กœ ์Œ์œผ๋กœ ๋ป—์–ด๋‚˜๊ฐ€๋ฉด์„œ ๋งค๋ฒˆ ๋”ํ•˜๋Š” ์Œ ๋‚ด๋ถ€์— ์ตœ๋Œ“๊ฐ’์ด ๋ฌด์กฐ๊ฑด ์ ์šฉ๋˜๊ฒŒ ํ•œ๋‹ค. (greedy)

 

: ์ •ํ•ด์ง„ ์ธ์ ‘์Œ ๊ฐœ์ˆ˜ ๋‚ด์—์„œ ๊ฐ๊ฐ์˜ ์Œ์— ์ตœ๋Œ“๊ฐ’์ด ๋“ค์–ด๊ฐ€๊ฒŒ ํ•˜๋Š” greedyํ•œ ์ƒํ™ฉ ์„ ํƒ, ์ตœ๋Œ“๊ฐ’ ์ œ์™ธ ๋ชจ๋“  ์นด๋“œ์˜ ์ˆซ์ž๋Š” ๊ฐ ์Œ์—์„œ ํ•œ ๋ฒˆ์”ฉ ๋”ํ•ด์ง€๋ฏ€๋กœ ์ตœ๋Œ“๊ฐ’ x (์Œ์˜ ๊ฐœ์ˆ˜) + (์ตœ๋Œ“๊ฐ’ ์ œ์™ธ ๋ชจ๋“  ์นด๋“œ ์ˆซ์ž ํ•ฉ)


โ˜… 20310 ํƒ€๋…ธ์Šค โ˜…

 

S=input()
zero=S.count('0')//2
one=S.count('1')//2
ans=[]
zero_cnt,one_cnt=0,0
for n in S:
    if n == '0':
        if zero_cnt < zero:
            ans.append(0)
            zero_cnt+=1
    else:
        if one_cnt >= one:
            ans.append(1)
        else:
            one_cnt+=1
print(*ans,sep='')

 

๐Ÿง‍โ™‚๏ธ0๊ณผ 1์˜ ๊ฐฏ์ˆ˜๋Š” ์ •ํ•ด์ ธ ์žˆ๋Š” ์ƒํƒœ์—์„œ ๊ฐ€์žฅ ์‚ฌ์ „์ˆœ์œผ๋กœ ์•ž์„œ๋Š” ์ˆซ์ž๋ฅผ ์ฐพ์œผ๋ ค๋ฉด 0์€ ์˜ค๋ฅธ์ชฝ๋ถ€ํ„ฐ ์ง€์›Œ์•ผ ํ•˜๊ณ , 1์€ ์™ผ์ชฝ๋ถ€ํ„ฐ ์ง€์›Œ์•ผ ํ•œ๋‹ค.

 

: ์ •ํ•ด์ง„ ๊ฐฏ์ˆ˜ ์ƒํ™ฉ ๋‚ด์—์„œ ์ตœ์ ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋‚ด๊ธฐ ์œ„ํ•ด ์™ผ์ชฝ๋ถ€ํ„ฐ 0์€ ๋†”๋‘๊ณ  1์€ ์ง€์šฐ๋Š” greedyํ•œ ์ƒํ™ฉ! ๋ฌธ์ œ๋งŒ ๋ฐ”๋กœ ์ฝ์œผ๋ฉด ๋ฐ”๋กœ ํ’€๋ฆผ.


โ˜… 15904 UCPC๋Š” ๋ฌด์—‡์˜ ์•ฝ์ž์ผ๊นŒ? โ˜…

 

i=0
for s in input():
    if s=='U' and i==0:i+=1
    if s=='C' and i==1:i+=1
    if s=='P' and i==2:i+=1
    if s=='C' and i==3:
        i+=1
        break
print('I love UCPC'if i==4 else'I hate UCPC')

 

๐Ÿ‡ greedy ๊ด€์  ๋ถ„์„

: ๊ฐœ๋ณ„ ์ƒํ™ฉ) ๋งค ๋ฌธ์ž๋ฅผ ํ™•์ธํ•˜๋Š” ์ƒํ™ฉ

: ์ข…ํ•ฉ ์ƒํ™ฉ) ๋ฌธ์ž๋ฅผ ํ™•์ธํ•ด์„œ UCPC ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ์ง€ ์ฒดํฌ

 

โ˜… ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ์€ ์ตœ์ ์˜ ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ๋ชจ์Œ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ , ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ์ตœ์  greedy ์†”๋ฃจ์…˜์€ U → C → P → C ์ˆœ์„œ๋กœ ํ™•์ธ(๋‹ค ๊ตฌํ•˜๋ฉด break)


โ˜… 2217 ๋กœํ”„ โ˜…

 

import sys
input=sys.stdin.readline
N=int(input())
ropes=[int(input()) for _ in range(N)]
ropes.sort()
i,ans=0,0
for rope in ropes:
    ans=max(ans,rope*(N-i))
    i+=1
print(ans)

 

๐Ÿ‡ greedy ๊ด€์  ๋ถ„์„

: ๊ฐœ๋ณ„ ์ƒํ™ฉ) ์ฃผ์–ด์ง„ ๋กœํ”„์˜ ๊ฐœ์ˆ˜ x์—์„œ ๋“ค์–ด์˜ฌ๋ฆฐ ์ตœ๋Œ€ ์ค‘๋Ÿ‰

: ์ข…ํ•ฉ ์ƒํ™ฉ) ์ฃผ์–ด์ง„ ๋กœํ”„์˜ ์—ฌ๋Ÿฌ ๊ฐœ์ˆ˜์—์˜ ์ตœ๋Œ€ ์ค‘๋Ÿ‰ ๋ชจ์Œ  / ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ) ๊ทธ ์ค‘ ์ตœ๋Œ“๊ฐ’

 

โ˜… ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ์€ ์ตœ์ ์˜ ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ๋ชจ์Œ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ , ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ์ตœ์  greedy ์†”๋ฃจ์…˜์€ (๋จผ์ € ropes ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ) ex) 1๊ฐœ์ผ ๊ฒฝ์šฐ (๊ฐ€์žฅ ํฐ rope * 1) / 2๊ฐœ์ผ ๊ฒฝ์šฐ ๊ฐ€์žฅ ํฐ rope๊ณผ ๊ทธ ๋‹ค์Œ ํฐ rope ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๊ณ  ๊ทธ ์ค‘ ์ตœ์†Ÿ๊ฐ’์ธ (๊ทธ ๋‹ค์Œ ํฐ rope * 2) /... ์ „์ฒด N๊ฐœ์ผ ๊ฒฝ์šฐ (์ตœ์†Ÿ๊ฐ’ rope * N) → ๊ฐ๊ฐ์˜ ๋ชจ๋“  ๊ฒฝ์šฐ์ค‘ ์ตœ๋Œ“๊ฐ’ ๊ตฌํ•˜๊ธฐ


โ˜… 29198 ์ด๋ฒˆ์—๋Š” C๋ฒˆ์ด ๋ฌธ์ž์—ด โ˜…

 

import sys
input=sys.stdin.readline
N,M,K=map(int,input().split())
print(''.join(sorted(''.join(sorted([''.join(sorted(input().strip())) for _ in range(N)])[:K]))))

 

โ˜… greedy 3๋ฒˆ ์ ์šฉ → ์ •๋ ฌ 3๋ฒˆ ์ ์šฉ ๋ฌธ์ œ / 2023 KSA Automata Summer Contest ๊ธฐ์ถœ

 

๐Ÿ‡ greedy ๊ด€์  ๋ถ„์„

: ๊ฐœ๋ณ„ ์ƒํ™ฉ) ๋ฌธ์ž์—ด K๊ฐœ ์„ ํƒํ•œ ๋’ค ๋งŒ๋“ค์–ด์ง„ KxM๊ฐœ์˜ ๋ฌธ์ž์ค‘ 1๊ฐœ์”ฉ ์„ ํƒ

: ์ข…ํ•ฉ ์ƒํ™ฉ) ๊ธธ์ด KxM์˜ ๋ฌธ์ž์—ด T ๋งŒ๋“ค๊ธฐ / ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ) ์‚ฌ์ „ ์ˆœ์œผ๋กœ ๊ฐ€์žฅ ๋จผ์ € ์˜ค๋Š” ๋ฌธ์ž์—ด T ๋งŒ๋“ค๊ธฐ

 

โ˜… ์ตœ์ ์˜ ์ข…ํ•ฉ ์ƒํ™ฉ์€ ์ตœ์ ์˜ ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ๋ชจ์Œ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ , ๊ฐœ๋ณ„ ์ƒํ™ฉ์˜ ์ตœ์  greedy ์†”๋ฃจ์…˜์€ (1) ๋จผ์ € ์ •๋ ฌ๋œ ๋ฌธ์ž์—ด S1 ~ Sn ์ค‘, ์ •๋ ฌํ•ด ์‚ฌ์ „ ์ˆœ ๊ฐ€์žฅ ์•ž์— ์˜ค๋Š” ๋ฌธ์ž์—ด K๊ฐœ ์„ ํƒ (2) ์ด KxM๊ฐœ์˜ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ์ •๋ ฌ(์‚ฌ์ „์ˆœ ์ •๋ ฌ)

 

: ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ๊ฑด, ๋ฌธ์ž๋ฅผ ์„œ๋กœ ์„ž์–ด ์žฌ๋ฐฐ์น˜ํ•  ์ˆ˜ ์žˆ์Œ. ๋”ฐ๋ผ์„œ (1) ๋ฌธ์ž์—ด ๋‚ด๋ถ€ ๋จผ์ € ์ •๋ ฌ / (2) K๊ฐœ ์„ ํƒ์„ ์œ„ํ•ด ๋ฌธ์ž์—ด์„ ์ •๋ ฌ / (3) ๋งŒ๋“ค์–ด์ง„ KxM๊ฐœ์˜ ๋ฌธ์ž๋ฅผ ์ •๋ ฌ โ–ถ ์šฐ์„ ์ ์œผ๋กœ ๋งจ ์•ž์ž๋ฆฌ๊ฐ€ ๋’ท์ž๋ฆฌ๋ณด๋‹ค ์‚ฌ์ „์ˆœ์œผ๋กœ ์ค‘์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— greedy์›๋ฆฌ ์ ์šฉ


๋Œ“๊ธ€