forked from Autoratch/Thailand-Olympiad-in-Informatics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoi13_cat.cpp
40 lines (31 loc) · 767 Bytes
/
toi13_cat.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
#include <vector>
#include <iostream>
#include <stack>
#include <unordered_map>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int n;
cin >> n;
vector<int> a(n);
long long x;
for(int i = 0;i < n;i++){cin >> x; x = --x; a[i] = x; }
stack<int> s;
unordered_map<int,bool> visited;
int ans = -1;
for(int i = 0;i < n;i++)
{
if(!visited[a[i]]){ s.push(a[i]); visited[a[i]] = true; }
else
{
int mx = -1;
if(ans>a[i]) continue;
while(!s.empty() and s.top()!=a[i]){ mx = max(mx,s.top()); s.pop(); }
ans = max(ans,min(a[i],mx));
mx = max(mx,a[i]);
s.push(mx);
}
}
cout << ans + 1;
}