#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
#define __TIME (1.0 * clock() / CLOCKS_PER_SEC)
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const int N = 1e6 + 5;
const int oo = 1e9, mod = 1e9 + 7;
int n, m, k, a[N];
vector<int> bit1(N), bit2(N);
void updatePoint(vector<int>& b, int u, int v) {
int idx = u;
while (idx <= n) {
b[idx] += v;
idx += (idx & (-idx));
}
}
void updateRange(int l, int r, int v) {
updatePoint(bit1, l, (n - l + 1) * v);
updatePoint(bit1, r + 1, -(n - r) * v);
updatePoint(bit2, l, v);
updatePoint(bit2, r + 1, -v);
}
int getSumOnBIT(vector<int>& b, int u) {
int idx = u, ans = 0;
while (idx > 0) {
ans += b[idx];
idx -= (idx & (-idx));
}
return ans;
}
int prefixSum(int u) {
return getSumOnBIT(bit1, u) - getSumOnBIT(bit2, u) * (n - u);
}
int rangeSum(int l, int r) {
return prefixSum(r) - prefixSum(l - 1);
}
void process() {
cin >> n;
foru(i,1,n) cin >> a[i];
int ans=0, pre=0;
foru(i,1,n-1) {
if (i<n && a[i] > a[i+1]) {
ans++;
updateRange(pre+1, i, 1);
pre=i;
continue;
}
if (a[i]==a[i+1]) continue;
if (rangeSum(a[i+1]-1, a[i+1] - 1) -rangeSum(a[pre+1], a[pre+1])>0) {
ans++;
updateRange(pre+1, i, 1);
pre=i;
}
}
cout << ans+1;
return;
}
signed main() {
cin.tie(0)->sync_with_stdio(false);
//freopen(".inp", "r", stdin);
//freopen(".out", "w", stdout);
process();
cerr << "Time elapsed: " << __TIME << " s.\n";
return 0;
}
// dont stop
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
15964 KB |
Output is correct |
2 |
Incorrect |
4 ms |
15964 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
15964 KB |
Output is correct |
2 |
Incorrect |
4 ms |
15964 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
15964 KB |
Output is correct |
2 |
Incorrect |
4 ms |
15964 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
15964 KB |
Output is correct |
2 |
Incorrect |
4 ms |
15964 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |