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 pb push_back
#define eb emplace_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define uniq(x) x.erase(unique(all(x)), x.end())
#define rall(x) x.rbegin(), x.rend()
//#define int long long
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;
const double eps = 1e-9;
void setIO() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
int n, m;
vector<int> v;
ll dp[5005][5005];
ll f(int pos, int h) {
if(pos == n) return 0;
if(dp[pos][h] != -1) return dp[pos][h];
ll ans = 1e18;
if(v[pos+1] < h) ans = min(ans, f(pos+1, v[pos+1]));
else if(v[pos+1] - h <= m) ans = min(ans, f(pos+1, v[pos+1]));
for(int i=0; i<5005; i++) {
if(i == v[pos+1]) continue;
if(i < h) ans = min(ans, 1 + f(pos+1, i));
else if(i - h <= m) ans = min(ans, 1 + f(pos+1, i));
}
return dp[pos][h] = ans;
}
int32_t main() {
setIO();
memset(dp, -1, sizeof(dp));
cin >> n >> m;
v.resize(n+1);
for(int i=1; i<=n; i++) cin >> v[i];
cout << f(0, v[0]) << '\n';
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... |