码迷,mamicode.com
首页 > 其他好文 > 详细

Heaters (codeforces 1066B)

时间:2018-10-13 14:46:45      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:for   有一个   using   贪心   while   题解   codeforce   last   force   

贪心题

策略

在最左边设置一个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;  
} 

Heaters (codeforces 1066B)

标签:for   有一个   using   贪心   while   题解   codeforce   last   force   

原文地址:https://www.cnblogs.com/baccano-acmer/p/9782677.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!