Heaters (codeforces 1066B)

贪心题

策略

在最左边设置一个array 0,每一次从右往左,如果有heater的话就寻找heater左边界是不是小于等于目前的上一个heater的右边界,如果没有一个这样的,那么就直接输出-1

代码(cf的题解的算法)

#include <bits/stdc++.h>
using namespace std;
int a[1100];
main()
{
    int n,r;
    cin>>n>>r;
    for(int i=1;i<=n;i++)
    cin>>a[i];
    int last=0;
    int ans=0;
    while(last<n)
    {
        int pos=0;
        for(int i=n;i>max(0,last-(r-1));i--)
        if(a[i]&&i-r<=last)
        {
            pos=i;
            break;
        }
        if(!pos)
        {
            cout<<"-1";
            return 0;
        }
        ans++;
        last=pos+r-1;
    }
    cout<<ans;  
} 

猜你喜欢

转载自www.cnblogs.com/baccano-acmer/p/9782677.html