1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
| // 더 많은 정보는 42jerrykim.github.io 에서 확인하세요.
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n; if(!(cin >> n)) return 0;
vector<pair<int,int>> segs; segs.reserve(n);
int maxCoord = 0;
for(int i=0;i<n;++i){
int a,b; cin >> a >> b;
segs.emplace_back(a,b);
maxCoord = max(maxCoord, max(a,b));
}
vector<int> startCount(maxCoord+2,0), endCount(maxCoord+2,0);
for(auto &p: segs){
startCount[p.first] += 1;
endCount[p.second] += 1; // [a,b) 해석: b에서 -1
}
// s2: 라인 스위핑으로 최대 동시 탑승자
int s2 = 0, cur = 0;
for(int x=1;x<=maxCoord;++x){
cur += startCount[x];
cur -= endCount[x];
if(cur > s2) s2 = cur;
}
// 누적/접미 합
vector<int> endPrefix(maxCoord+2,0), startSuffix(maxCoord+3,0);
for(int i=1;i<=maxCoord;++i) endPrefix[i] = endPrefix[i-1] + endCount[i];
for(int i=maxCoord;i>=1;--i) startSuffix[i] = startSuffix[i+1] + startCount[i];
int s1 = 0;
for(auto &p: segs){
int L = p.first, R = p.second;
int overlap = n - endPrefix[L] - startSuffix[R];
if(overlap > s1) s1 = overlap;
}
cout << s1 << ' ' << s2 << '\n';
return 0;
}
|