PS

[백준] 14469번 소가 길을 건너간 이유 3 (C++)

-minari- 2024. 9. 27. 21:15

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;
}