BOJ
14469 소가 길을 건너간 이유 3
공부정리
2018. 11. 10. 16:20
소가 검문소에 도착하는 시간 = a[i] 와 검문에 걸리는 시간 = b[i] 가 주어질 때 모든 소가 입장할때까지 얼마나 걸리는지 출력하는 문제다.
해법
선입선출 개념이므로 도착하는 시간에 대해서 오름차순으로 정렬하고 차례대로 훑으면서
현재 시간 cur_time보다 먼저 도착하면 cur_time = cur_time + b[i]
cur_time보다 늦게 도착하면 cur_time = a[i] + b[i]
이런식으로 cur_time을 갱신하고 출력하면 된다.
#include <iostream> #include <algorithm> using namespace std; pair<int, int> a[101]; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, ans = 0; cin >> n; for (int i = 0; i < n; ++i) cin >> a[i].first >> a[i].second; sort(a, a + n); ans = a[0].first + a[0].second; for (int i = 1; i < n; ++i) { if (ans <= a[i].first) ans = a[i].first + a[i].second; else ans += a[i].second; } cout << ans << '\n'; return 0; } | cs |