Codeforces Round #515 (Div. 3) B(模拟)

题意:在数列中值为1的位置有1个加热器,它能覆盖它的左边第 r-1 位置到它的右边 r-1 的位置,问最少多少个加热器能覆盖整个区间。

思路:模拟这个过程,首先now=1,然后遍历所有位置,找到最远的满足now这个位置能加热的点,再另now=i+r-1+1,之所以要+1,是因为,i+r-1这个位置已经包含在刚才那个区间了。

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int maxn=200005;
const int mod=1e9+7;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll qpow(ll a,ll b){ll t=1;while(b){if(b%2){t=(t*a)%mod;b--;}a=(a*a)%mod;b/=2;}return t;}
int main()
{
    std::ios::sync_with_stdio(false);
    int n,r,a[1005];
    while(cin>>n>>r)
    {
        for(int i=1;i<=n;i++)
            cin>>a[i];
        int now=1,ans=0,flag=0;
        while(now<=n)
        {
            flag=0;
            for(int i=1;i<=n;i++)
            {
                if(a[i]&&now>=i-r+1&&now<=i+r-1)
                {
                    flag=i;
                }
            }
            if(flag)
            {
                ans++;
                now=flag+r;
            }
            else
            {
                cout<<-1<<endl;
                return 0;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值