그리디 문제.
문제의 핵심은 결국 토기를 최대한 많이 만들어두고, 극한의 이득을 취하면서 커피를 담아둬야 한다고 생각했다.
따라서 0부터 시간을 증가시키며 커피를 담아둘 수 있는 시간이 아니고, 손님이 방문하는 시간이 아닌 경우에는 모두 토기를 만들었다.
그리고 커피를 담아둬야 하는 경우 토기가 있다면 커피를 즉시 담고, 토기가 없다면 토기를 만들고 커피를 담았다.
이때 모든 과정 중 한 번이라도 커피를 담을 수 없거나 토기를 만들 수 없으면 fail을 출력했다.
코드는 다음과 같다.
# -*- coding: utf-8 -*-
import sys
from collections import deque
import heapq
import bisect
import math
from itertools import product
from itertools import combinations
"""
from itertools import combinations
from itertools import combinations_with_replacement
from itertools import permutations
import copy
"""
#input=sys.stdin.readline
#print=sys.stdout.write
#sys.setrecursionlimit(100000000)
n,m=map(int,input().split())
L=list(map(int,input().split()))
togi=0
time=0
A=[0]*1000001
for i in range(n):
A[L[i]]=1
for i in range(n):
if L[i]>time:
while time<L[i]-m:
if not A[time]:
togi+=1
time+=1
if togi:
while time<L[i]:
if not A[time]:
break
time+=1
if time<L[i]:
time+=1
togi-=1
else:
print('fail')
sys.exit(0)
else:
while time<L[i]:
if not A[time]:
togi+=1
time+=1
break
time+=1
while time<L[i]:
if not A[time]:
break
time+=1
if time<L[i]:
time+=1
togi-=1
else:
print('fail')
sys.exit(0)
else:
print('fail')
sys.exit(0)
print('success')
조금 더 깔끔하게 풀 수는 없었을까하는 아쉬움이 남는다.
'BOJ' 카테고리의 다른 글
백준(BOJ) 2573 빙산(Python) (0) | 2024.03.20 |
---|---|
백준(BOJ) 5549 행성 탐사(Python) (0) | 2023.04.20 |
백준(BOJ) 3663 고득점(Python) (0) | 2023.04.19 |
백준(BOJ) 24041 성싶당 밀키트(Python) (0) | 2023.04.18 |
백준(BOJ) 2258 정육점(Python) (0) | 2023.02.24 |