문제
2979번: 트럭 주차
첫째 줄에 문제에서 설명한 주차 요금 A, B, C가 주어진다. (1 ≤ C ≤ B ≤ A ≤ 100) 다음 세 개 줄에는 두 정수가 주어진다. 이 정수는 상근이가 가지고 있는 트럭이 주차장에 도착한 시간과 주차장
www.acmicpc.net

코드
My answer
import sys
input=sys.stdin.readline
answer=0
price=list(map(int,input().split()))
count_list=[0 for i in range(100)]
for i in range(3):
a,b=map(int,input().split())
for j in range(a,b):
count_list[j]+=1
for i in range(3):
answer+=price[i]*(count_list.count(i+1))*(i+1)
print(answer)
Another answer
a, b, c = map(int, input().split())
arr = [0]*100; answer=0
for _ in range(3):
begin, end = map(int, input().split())
for i in range(begin, end): arr[i] += 1
for j in arr:
answer += {0:0, 1:a, 2:b*2, 3:c*3}[j]
print(answer)
풀이
매우 간단한 구현 알고리즘 문제였다.
중복되는 구간만 찾으면 되기 때문에, 주어진 시간 범위인 0~100까지 빈 리스트를 생성한 후
단순하게 첫번째 트럭부터 머무른 시간에 해당하는 인덱스에 1씩 더해주면 됐다.
이렇게 첫번째부터 마지막 트럭까지 하면 중복되는 구간은 숫자가 2혹은 3이 되어 있을 것이다.
another answer은 나와 같은 아이디어를 좀 더 간단하게 구현한 코드이다.
728x90
'코딩테스트 > 백준[Python]' 카테고리의 다른 글
| [Python] 백준 #1260- DFS와 BFS (0) | 2023.09.05 |
|---|---|
| [Python] 백준 #2606 - 바이러스 (0) | 2023.09.04 |
| [Python] 백준 #11501- 주식 (0) | 2023.09.01 |
| [Python] 백준 #21314- 민겸 수 (0) | 2023.09.01 |
| [Python] 백준 #20365- 블로그 2 (0) | 2023.09.01 |