1. 程式人生 > >Codeforces Round #515 (Div. 3) B(模擬)

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