#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6;
int n;
int a[MAXN + 5];
int b[MAXN + 5];
int A[MAXN + 5][25], B[MAXN + 5][25];
int le[MAXN + 5];
int dp[MAXN + 5];
int datvla (int l, int r){
if (l > r)
return n + 1;
int x = 0, y = 1;
while (y * 2 < r - l + 1){
x++;
y *= 2;
}
return min (A[l][x], B[r][x]);
}
int main()
{
cin>>n;
for (int i = 1; i <= n; i++)
cin>>a[i];
for (int i = 1; i <= MAXN; i++)
b[i] = n + 1;
for (int i = n; i >= 1; i--)
b[a[i]] = i;
for (int i = 1; i <= n; i++){
A[i][0] = b[i];
B[i][0] = b[i];
}
for (int i = 1; i < 20; i++)
for (int j = 1; j <= n; j++){
A[j][i] = A[j][i - 1];
if (j + (1<<(i - 1)) <= n)
A[j][i] = min (A[j][i], A[j + (1<<(i - 1))][i - 1]);
B[j][i] = B[j][i - 1];
if (j - (1<<(i - 1)) >= 1)
B[j][i] = min (B[j][i], B[j - (1<<(i - 1))][i - 1]);
}
le[1] = 1;
for (int i = 2; i <= n; i++){
le[i] = i;
if (a[i] >= a[i - 1])
le[i] = le[i - 1];
}
dp[1] = 1;
for (int i = 2; i <= n; i++){
int I = i;
for (int j = 19; j >= 0; j--){
if (I - (1<<j) < le[i])
continue;
if (datvla (a[I - (1<<j)], a[i]) >= I - (1<<j))
I -= (1<<j);
}
dp[i] = dp[I - 1] + 1;
}
cout<<dp[n]<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4344 KB |
Output is correct |
2 |
Correct |
5 ms |
4216 KB |
Output is correct |
3 |
Correct |
5 ms |
4216 KB |
Output is correct |
4 |
Incorrect |
5 ms |
4088 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4344 KB |
Output is correct |
2 |
Correct |
5 ms |
4216 KB |
Output is correct |
3 |
Correct |
5 ms |
4216 KB |
Output is correct |
4 |
Incorrect |
5 ms |
4088 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4344 KB |
Output is correct |
2 |
Correct |
5 ms |
4216 KB |
Output is correct |
3 |
Correct |
5 ms |
4216 KB |
Output is correct |
4 |
Incorrect |
5 ms |
4088 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4344 KB |
Output is correct |
2 |
Correct |
5 ms |
4216 KB |
Output is correct |
3 |
Correct |
5 ms |
4216 KB |
Output is correct |
4 |
Incorrect |
5 ms |
4088 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |