#include <bits/stdc++.h>
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define F first
#define S second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
const ll MAX = 1e6+7;
const ll MOD = 1e9+7;
ll n,a[MAX], id[MAX];
vector <pll> v;
void solve() {
cin >> n;
for(ll i=1; i<=n; ++i) cin >> a[i];
ll type = 1, ans = 0;
set <ll> s;
for(ll i=1; i<=n; ++i) {
type += (a[i] < a[i-1]);
auto r = s.upper_bound(a[i]);
if(r == s.begin()) {
if(id[*r] != type) ans ++;
}
else if(r == s.end()) {
auto l = --r;
if(id[*l] != type) ans ++;
}
else {
auto l = --r;
if(id[*l] != type && id[*r] != type)
ans ++;
}
s.insert(a[i]);
id[a[i]] = type;
// cout << ans << " ";
}
cout << ans;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//if(fopen("TASK.INP","r")) {
// freopen("TASK.INP","r",stdin);
// freopen("TASK.OUT","w",stdout);
//}
solve();
cerr << "\n" << "Time elapsed: " << TIME << "s\n";
return 0;
}
| # | 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... |