#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 2000005
#define pb push_back
#define ff first
#define ss second
#define sz(s) (int)s.size()
ll n, t, ind, a[N], b[N], gr;
set <ll> nw;
deque <ll> v;
int main () {
ios::sync_with_stdio(0);cin.tie(0);
cin >> n;
bool tr = 0;
for(int i = 1; i <= n; i++) {
cin >> a[i];
if(i == 1 || !tr && a[i] >= a[i-1]) {
nw.insert(a[i]);
ind = i + 1;
continue;
}
tr = 1;
}
gr = 1;
if(ind == n + 1) {
cout << gr << '\n';
return 0;
}
for(int i = ind; i <= n; i++) {
if(sz(v) && v[sz(v) - 1] <= a[i]) {
v.pb(a[i]);
continue;
}
while(sz(v) > 0) {
auto p = nw.upper_bound(v[0]);
if(p == nw.end()) {
while(sz(v) > 0) {
nw.insert(v[0]);
v.pop_front();
}
gr++;
break;
}
gr++;
while(sz(v) > 0 && v[0] <= *p) {
nw.insert(v[0]);
v.pop_front();
}
}
v.pb(a[i]);
}
while(sz(v) > 0) {
auto p = nw.upper_bound(v[0]);
if(p == nw.end()) {
while(sz(v) > 0) {
nw.insert(v[0]);
v.pop_front();
}
gr++;
break;
}
gr++;
while(sz(v) > 0 && v[0] <= *p) {
nw.insert(v[0]);
v.pop_front();
}
}
cout << gr << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |