#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+1, inf = INT_MAX;
int n, m, a[N], pre[N];
vector<int> suf, dp[2];
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> m;
a[0] = 0;
pre[0] = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
pre[i] = max(a[i], pre[i-1]);
}
dp[0].clear();
dp[0].resize(1);
dp[0][0] = 0;
for (int i = 1; i <= n; i++) {
int x = min(pre[i-1]+1, m*(i-1) + 1);
suf.clear();
suf.resize(x);
for (int j = x-1; j >= 0; j--) {
suf[j] = min(dp[(i-1)%2][j], (j+1 < x ? suf[j+1] : inf));
}
int y = min(pre[i]+1, m*i + 1);
dp[i%2].clear();
dp[i%2].resize(y);
for (int j = 0; j < y; j++) {
dp[i%2][j] = suf[max(0, j-m)] + (j != a[i]);
}
}
int ans = inf;
for (int i = 0; i <= min(pre[n], n*m); i++) {
ans = min(ans, dp[n%2][i]);
}
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |