This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
/*
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |