题意:一个数组只含有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. openfire重新配置数据库oracle、mysql

    1.openfire重新配置设置: 删除target/openfire/conf/openfire.xml中的 <setup>true</setup> 2.删除浏览器之前访问过 ...

  2. I.MX6 MAC Address 导致的系统崩溃

    /**************************************************************************** * I.MX6 MAC Address 导致 ...

  3. Git dev分支合并到master分支完美实战

    aa@aa-PC MINGW64 /f/online-center/cloud/server-functions_services (master)                          ...

  4. java笔记线程的生命周期图解

  5. (转)Javascript中console.log()用法

    原文地址应该是这个吧:http://my.oschina.net/junn/blog/142728 注意:必须要提前打开IE的开发者模式才能看到输入 否则就会报错. IE下可以这个判断: if (co ...

  6. 微信公众号开发——获取access_token(PHP版)

    access_token是调用微信接口的唯一凭据,每两小时刷新一次,我们需要每两小时就获取一次access_token. <?php class TokenUtil { //获取access_t ...

  7. CCF 201409-1 相邻数对 (水题)

    问题描述 给定n个不同的整数,问这些数中有多少对整数,它们的值正好相差1. 输入格式 输入的第一行包含一个整数n,表示给定整数的个数. 第二行包含所给定的n个整数. 输出格式 输出一个整数,表示值正好 ...

  8. element-ui公用模态框自定义样式与scoped样式生效问题解决方案

    //先插如效果图 里面内容均为传进来的.包括取消与确定按钮,因为每个页面的绑定事件不一样. //下面这个图片为初始样式 //拖动模态框指令需要插件.详情看我下一篇,以下是地址 https://www. ...

  9. WPF-DataGrid(数据表格)美化

    我们不多哔哔先上图: 数据表格使用背景: 当我们在做二次开发发现我我们的表格无法向WEB的表格一样好看,这时我们就需要对数据表格进行美化和重构 表格美化思维引导: WPF数据表格是由表头和表体(内容) ...

  10. 使用python计算softmax函数

    softmax计算公式:                        Softmax是机器学习中一个非常重要的工具,他可以兼容 logistics 算法.可以独立作为机器学习的模型进行建模训练.还可 ...