A:The Way to Home

link:http://codeforces.com/contest/910/problem/A

题面:有每次最大跳跃距离d,只有一部分的点可以落脚,求最少几步达到终点D

 

Solution :预处理+贪心

用f[i]表示离点i最近的可落脚点,贪心即可(dp同理)

#include <bits/stdc++.h>

using namespace std;
int n,d,pre[105];
string s;

int main()
{
    cin >> n >> d >> s;
    n--;
    
    pre[0]=0;
    for(int i=1;i<s.size();i++)
        if(s[i]=='1') pre[i]=i;
        else pre[i]=pre[i-1];
        
    int cur=0,res=0;
    bool f=true;
    while(cur<n)
    {
        if(pre[min(cur+d,n)]==cur)
        {
            f=false;
            break;
        }
        cur=pre[min(cur+d,n)];
        res++;
    }
    
    if(!f) cout << -1;
    else cout << res;
    
    return 0;
}
Problem A

相关文章:

  • 2022-12-23
  • 2021-11-16
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-27
  • 2022-12-23
  • 2022-02-19
猜你喜欢
  • 2022-12-23
  • 2022-01-01
  • 2022-12-23
  • 2022-01-15
  • 2021-12-07
  • 2022-12-23
  • 2021-09-05
相关资源
相似解决方案