// solved by using fenvick tree
// + binary search
# include <iostream>
using namespace std;
const int N = 1e6 + 100;
int n, ans = 1;
int a[N];
int cnt[N];
void update(int i)
{
while(i <= N){
cnt[i]++;
i += (i & -i);
}
}
int get(int i){
int ret = 0;
while(i > 0){
ret += cnt[i];
i -= (i & -i);
}
return ret;
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
update(1e6);
update(a[1]);
int x = 1e6;
for (int i = 2; i <= n; i++){
if(a[i] < a[i - 1] || x < a[i]){
int l = a[i], m, r = 1e6;
while(r > l){
m = (r + l) >> 1;
if(get(a[m]) > get(a[i]))
r = m;
else
l = m + 1;
}
x = l;
++ ans;
}
update(a[i]);
}
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |