-
Notifications
You must be signed in to change notification settings - Fork 167
/
haunted-house.cpp
51 lines (47 loc) · 903 Bytes
/
haunted-house.cpp
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
47
48
49
50
51
//Haunted House
//Weekly Challenges - Week 15
//Author: derekhh
//May 26, 2015
#include<iostream>
#include<vector>
#include<algorithm>
#include<climits>
using namespace std;
const int MAXV = 300000 + 1;
vector<pair<int, int>> v;
int p[MAXV];
int main()
{
ios_base::sync_with_stdio(false);
int N;
cin >> N;
for (int i = 0; i < N; i++)
{
int x, y;
cin >> x >> y;
v.push_back(make_pair(x, y));
}
for (int i = 0; i < N; i++)
v.push_back(make_pair(i, INT_MAX));
sort(v.begin(), v.end());
int tot = 0, prev = -1, ans = 0;
for (int i = 0; i < static_cast<int>(v.size()); i++)
{
if (v[i].second == INT_MAX)
{
if (prev != -1)
tot -= p[prev];
//cout << v[i].first << " " << tot << endl;
if (tot >= v[i].first + 1 && v[i].first + 1 > ans)
ans = v[i].first + 1;
prev = v[i].first;
}
else
{
p[v[i].second]++;
tot++;
}
}
cout << ans << endl;
return 0;
}