题意:一个数组只含有0或1,1表示该元素可以覆盖其自身、左边r-1个元素和右边r-1个元素,问最少保留多少个1元素可以覆盖整个数组。

思路:一个指针指向当前未被覆盖的最左边的元素下标,每次找离它最远且能够覆盖它的一个1元素来覆盖,并更新指针。

#include<iostream>
#include <string.h>
#include<cstdio>
using namespace std; int main()
{
int n,r;
while(scanf("%d%d",&n,&r)!=EOF)
{
int cnt=,heater[];
for(int i=;i<n;i++)
{
int num;
scanf("%d",&num);
if(num==)
heater[cnt++]=i;
}
int left=,flag=-,i,res=;
for(i=;i<cnt;i++)
{
int ll=heater[i]-r+;
int rr=heater[i]+r-;
if(ll>left)
{
if(flag>=)
{
res++;
left=heater[flag]+r;
if(ll<=left)
flag=i;
else
flag=-;
}
else
break;
}
else
flag=i;
if(i==cnt)
if(left<n)
if(flag>=)
if(heater[flag]+r>=n)
{
res++;
left=heater[flag]+r;
}
if(left<n)
printf("-1\n");
else
printf("%d\n",res);
}
return ;
}

codeforces_1066_B.Heaters的更多相关文章

  1. [LeetCode] Heaters 加热器

    Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...

  2. Leetcode: Heaters

    Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...

  3. heaters

    https://leetcode.com/problems/heaters/ 开始的时候,下面的代码对于两边数字完全一样的情况,测试不通过.原因是heater会有重复情况,这时候对于飘红部分就不会往前 ...

  4. [Leetcode] Binary search -- 475. Heaters

    Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...

  5. [Swift]LeetCode475. 供暖器 | Heaters

    Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...

  6. LeetCode算法题-Heaters(Java实现)

    这是悦乐书的第239次更新,第252篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第106题(顺位题号是475).冬天来了!您在比赛期间的第一份工作是设计一个固定温暖半径 ...

  7. 【leetcode】475. Heaters

    problem 475. Heaters solution1: class Solution { public: int findRadius(vector<int>& house ...

  8. 475. Heaters

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  9. 475. Heaters (start binary search, appplication for binary search)

    Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...

随机推荐

  1. C++明确规定,不能获取构造函数和析构函数的地址

    C++标准明确规定,不能获取构造函数和析构函数的地址,因此也无法形成指向他们的成员函数指针. 指向成员函数的指针可以,指向构造函数析构函数的不行.因为构造函数和析构函数都是没有返回值的,无法声明一个没 ...

  2. jvm部分知识总结

    1.jvm有三种执行模式,分别是解释执行,混合执行和编译执行,默认情况是混合执行模式. java version " Java(TM) SE Runtime Environment (bui ...

  3. Ubuntu上配置Eclipse:安装CDT【转】

    本文转载自:http://www.linuxdiyf.com/linux/23519.html 在最新的 Ubuntu Kylin 16.04 中安装了eclipse,在纠结了很久的网络问题之后,开始 ...

  4. apktool工具下载地址

    apktool工具下载地址 http://ibotpeaches.github.io/Apktool/

  5. [HEOI 2012] 采花

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2743 [算法] 首先预处理nxt[]数组 , 其中 , nxt[i]表示下一个和i号 ...

  6. 从free到page cache

    Free 我们经常用free查看服务器的内存使用情况,而free中的输出却有些让人困惑,如下:   图1-1 先看看各个数字的意义以及如何计算得到: free命令输出的第二行(Mem):这行分别显示了 ...

  7. RobotFramework模拟手机浏览器

    转自 http://blog.csdn.net/max229max/article/details/70808867 感谢max bai提供的思路 Python - Selenium Chrome 模 ...

  8. hdu 3507(DP+斜率优化)

    Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)To ...

  9. SqlSugar解决SQLite访问的问题:Unable to load DLL 'SQLite.Interop.dll'

    SqlSugar用的版本是4.5.9.5,访问SQLite数据提示错误.在本机调试一时没有什么错误,把代码发布到服务器上以后刚开始运行没有问题,一段时间后报错. English Message : C ...

  10. E20180206-E

    fundamental   adj. 基础的,基本的,根本的,重要的,原始的,主要的,十分重大的; [物] 基频的,基谐波的; [乐] 基音的; n. 原理,原则,基本,根本,基础; [乐] 基音; ...