https://www.acmicpc.net/problem/14469
문제 설명
n 마리의 소가 주어질 때, 각 소의 도착 시간과 검문 시간이 주어진다.
모든 소가 검문을 다 마치는 최소 시간을 구하는 문제이다.
주요 로직
1. 도착 시간을 기준으로 소 검문을 시작한다.
-> sort() 함수를 사용해 빨리 도착한 순으로 정렬해 검문을 한다.
정답 코드
#include <bits/stdc++.h>
using namespace std;
int n,a,b;
vector<pair<int,int>> v;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> n;
for(int i=0; i<n; i++){
cin >> a >> b;
v.push_back({a,b});
}
sort(v.begin(), v.end());
int ret = 0;
for(auto it : v){
if(ret < it.first) ret = it.first;
ret += it.second;
}
cout << ret << "\n";
return 0;
}
'PS' 카테고리의 다른 글
[백준] 1743번 음식물 피하기 (C++) (0) | 2024.10.08 |
---|---|
[백준] 1260번 DFS와 BFS (C++) (1) | 2024.10.07 |
[백준] 2109번 순회 강연 (C++) (0) | 2024.09.25 |
[백준] 14405번 피카츄 (C++) (0) | 2024.09.23 |
[백준] 2234번 성곽 (C++) (0) | 2024.09.21 |