알고리즘
백준 2594번 놀이공원 C++
영춘권의달인
2023. 2. 20. 14:51
https://www.acmicpc.net/problem/2594
2594번: 놀이공원
첫째 줄에 놀이기구의 개수 N이 주어진다. 이어 N줄에 걸쳐 각 놀이기구의 운행시작 시각과 종료 시각이 빈 칸을 사이에 두고 주어진다. 시각은 시간단위 두 자리, 분 단위 두 자리로 구성되며 오
www.acmicpc.net
앞시간-10, 뒷시간+10 를 pair로 저장해서 정렬하고 답을 구하면 되는데,
1100 1300, 1130 1230과 같이 늦게 시작하는 놀이기구가 먼저 시작하는 놀이기구보다 빨리 끝나는 경우를 조심해야한다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
int n;
vector<pair<int, int>> v;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
v = vector<pair<int, int>>(n);
for (int i = 0; i < n; i++) {
string s, e;
cin >> s >> e;
int h_s = stoi(s.substr(0, 2));
int m_s = stoi(s.substr(2, 4));
int h_e = stoi(e.substr(0, 2));
int m_e = stoi(e.substr(2, 4));
v[i].first = h_s * 60 + m_s - 10;
v[i].second = h_e * 60 + m_e + 10;
}
v.push_back({ 0,600 });
v.push_back({ 1320,1320 });
sort(v.begin(), v.end());
int last = 600;
int ans = 0;
for (int i = 0; i < v.size() - 1; i++) {
int e = max(v[i].second, last);
int s = v[i + 1].first;
ans = max(ans, s - e);
last = max(last, e);
}
cout << ans;
}