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;
}

猜你喜欢

转载自blog.csdn.net/Dilly__dally/article/details/83064141