#include <bits/stdc++.h>
#define FastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ll long long
#define PB push_back
#define ALL(v) (v).begin(), (v).end()
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; i--)
#define fi first
#define se second
#define BIT(x, i) (((x) >> (i)) & 1)
using namespace std;
/** END OF TEMPLATE **/
const int mxN = 1e5 + 5;
struct fenwick {
vector<int> bit;
};
int n, m;
int a[mxN], b[mxN];
int dp[mxN][2];
int main()
{
FastIO;
//freopen(".inp", "r", stdin);
//freopen(".out", "w", stdout);
cin >> n >> m;
FOR(i, 1, n) cin >> a[i];
memset(dp, 0x3f, sizeof dp);
dp[0][0] = dp[0][1] = 0;
FOR(i, 1, n) {
FORD(j, i-1, 0) {
if(a[i] <= a[j] + m || a[j] >= a[i])
dp[i][0] = min(dp[i][0], dp[j][0] + i-j-1);
if(a[i] <= b[j] + m || b[j] >= a[i])
dp[i][0] = min(dp[i][0], dp[j][1] + i-j-1);
}
if(dp[i][1] > dp[i-1][0] + 1) {
dp[i][1] = dp[i-1][0] + 1;
b[i] = a[i-1] + m;
}
if(dp[i][1] > dp[i-1][1] + 1) {
dp[i][1] = dp[i-1][1] + 1;
b[i] = b[i-1] + m;
}
//cerr << dp[i-1][0] << ' ' << dp[i-1][1] << '\n';
}
cout << min(dp[n][0], dp[n][1]);
return 0;
}
/*
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
1 ms |
1108 KB |
Output is correct |
4 |
Correct |
1 ms |
1092 KB |
Output is correct |
5 |
Correct |
1 ms |
1108 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
1 ms |
1108 KB |
Output is correct |
8 |
Correct |
1 ms |
1108 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1096 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Incorrect |
1 ms |
1108 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
1 ms |
1108 KB |
Output is correct |
4 |
Correct |
1 ms |
1092 KB |
Output is correct |
5 |
Correct |
1 ms |
1108 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
1 ms |
1108 KB |
Output is correct |
8 |
Correct |
1 ms |
1108 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1096 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Incorrect |
1 ms |
1108 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
1 ms |
1108 KB |
Output is correct |
4 |
Correct |
1 ms |
1092 KB |
Output is correct |
5 |
Correct |
1 ms |
1108 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
1 ms |
1108 KB |
Output is correct |
8 |
Correct |
1 ms |
1108 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1096 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Incorrect |
1 ms |
1108 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
1 ms |
1108 KB |
Output is correct |
4 |
Correct |
1 ms |
1092 KB |
Output is correct |
5 |
Correct |
1 ms |
1108 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
1 ms |
1108 KB |
Output is correct |
8 |
Correct |
1 ms |
1108 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1096 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Incorrect |
1 ms |
1108 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |