코딩테스트
[백준] 1449 수리공 항승 - python
CuckooBird
2023. 2. 1. 13:30
백준 실버3 1449 수리공 항승 python
https://www.acmicpc.net/problem/1449
1449번: 수리공 항승
첫째 줄에 물이 새는 곳의 개수 N과 테이프의 길이 L이 주어진다. 둘째 줄에는 물이 새는 곳의 위치가 주어진다. N과 L은 1,000보다 작거나 같은 자연수이고, 물이 새는 곳의 위치는 1,000보다 작거나
www.acmicpc.net
코드
맞았습니다가 뜬 코드입니다. - 메모리 31256KB | 시간 44ms | 코드 길이 396B
import sys
N, L = map(int, sys.stdin.readline().rstrip().split())
leak_li = list(map(int, sys.stdin.readline().rstrip().split()))
leak_li.sort()
tape = 1
temp = 0
for i in range(N-1):
if abs(leak_li[i] - leak_li[i+1]) < L: # 연속되는 두 구멍의 간격이 L보다 작을 때
temp += abs(leak_li[i] - leak_li[i+1]) # temp는 이전 간격의 정보도 있음
if temp >= L: # 간격이 L보다 크거나 같다면 테이프 하나 더 써야함
tape += 1
temp = 0 # temp 초기화
else: # 연속되는 두 구멍의 간격이 L보다 크면 테이프 하나 더 써야함
temp = 0
tape += 1
print(tape)
Try 1.
틀렸습니다가 뜬 코드입니다. - 코드 길이 598B
import sys
N, L = map(int, sys.stdin.readline().rstrip().split())
leak_li = list(map(int, sys.stdin.readline().rstrip().split()))
leak_li.sort()
continuity = []
cnt = 1
if N == 1:
print(1)
else:
for i in range(N-1):
if leak_li[i] + 1 == leak_li[i+1]:
cnt += 1
else:
continuity.append(cnt)
cnt = 1
if i == N-2:
continuity.append(cnt)
tape = 0
print(continuity)
for i in continuity:
if i <= L:
tape += 1
else:
tape += i // L
tape += i % L
print(tape)
- 연속하는 수만 생각해서 틀렸습니다. -> 떨어져있어도 L거리 안에 있으면 됨
후기
처음에 안 되길래 반례 찾으려고 백준 질문페이지를 봤는데 테이프에 붙는 건 연속되는 것만이 아니라는 걸 깨달았습니다.
100문제 달성!