#include<bits/stdc++.h>
using namespace std;
int n,m,a[105],v[105][10005];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n>>m;
for(int i = 1; i <= n; i++) cin>>a[i];
memset(v, -1, sizeof(v));
queue<tuple<int,int,int>> q;
q.push({0, 0, 0});
v[0][0] = 0;
while(q.size()){
auto [p, h, c] = q.front();
q.pop();
if(p == n){
cout<<c<<'\n';
return 0;
}
int nx = p + 1, t = a[nx];
if(t - h <= m){
if(v[nx][t] == -1 || v[nx][t] > c){
v[nx][t] = c;
q.push({nx, t, c});
}
}
else{
int nh = h + m;
if(v[p][nh] == -1 || v[p][nh] > c + 1){
v[p][nh] = c + 1;
q.push({p, nh, c + 1});
}
if(v[nx][nh] == -1 || v[nx][nh] > c + 1){
v[nx][nh] = c + 1;
q.push({nx, nh, c + 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... |