BOJ 5612 - 터널의 입구와 출구
Joonas' Note
BOJ 5612 - 터널의 입구와 출구 본문
- 문제
- 풀이
- 코드
https://www.acmicpc.net/problem/5612
문제
1분 단위로 터널에 들어간 차량의 수와 터널에서 나온 차량의 수가 주어졌을 때, 터널에는 차량이 최대 몇 대 있었는가?
풀이
처음에 터널에 있었던 차량 수에서, 매분 들어간 차량의 수를 더하고 나간 차량의 수를 빼면 된다.
중간에 한번이라도 터널 안 차량의 수가 음수가 되면 0을 출력하는 것에 주의하면 정답.
코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
int main() { | |
int n, m; | |
scanf("%d %d", &n, &m); | |
int ans = m; | |
while (n--) { | |
int in, out; | |
scanf("%d %d", &in, &out); | |
m += in - out; | |
ans = m > ans ? m : ans; | |
if (m < 0) return puts("0"), 0; | |
} | |
printf("%d\n", ans); | |
return 0; | |
} |
'알고리즘 > 문제 풀이' 카테고리의 다른 글
BOJ 1699 - 제곱수의 합 (0) | 2019.11.06 |
---|---|
BOJ 17521 - Byte Coin (0) | 2019.11.06 |
BOJ 13325 - 이진 트리 (0) | 2019.09.16 |
UVa 10918 - Tri Tiling (3) | 2019.04.15 |
BOJ 17142 - 연구소 3 (0) | 2019.04.15 |