codeforces_1066_B.Heaters
题意:一个数组只含有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的更多相关文章
- [LeetCode] Heaters 加热器
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...
- Leetcode: Heaters
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...
- heaters
https://leetcode.com/problems/heaters/ 开始的时候,下面的代码对于两边数字完全一样的情况,测试不通过.原因是heater会有重复情况,这时候对于飘红部分就不会往前 ...
- [Leetcode] Binary search -- 475. Heaters
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...
- [Swift]LeetCode475. 供暖器 | Heaters
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...
- LeetCode算法题-Heaters(Java实现)
这是悦乐书的第239次更新,第252篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第106题(顺位题号是475).冬天来了!您在比赛期间的第一份工作是设计一个固定温暖半径 ...
- 【leetcode】475. Heaters
problem 475. Heaters solution1: class Solution { public: int findRadius(vector<int>& house ...
- 475. Heaters
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- 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 ...
随机推荐
- SQL语句--常用
一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备 ...
- EF1:MVC/EF(Entity Framewok) /First Migrations
1. 概念 Entity Framework: ADO.NET Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对应 (O/R Mapping) 解决方案.(此处 ...
- BZOJ_3998_[TJOI2015]弦论_后缀自动机
BZOJ_3998_[TJOI2015]弦论_后缀自动机 Description 对于一个给定长度为N的字符串,求它的第K小子串是什么. Input 第一行是一个仅由小写英文字母构成的字符串S 第二行 ...
- Consistent Hashing算法
前几天看了一下Memcached,看到Memcached的分布式算法时,知道了一种Consistent Hashing的哈希算法,上网搜了一下,大致了解了一下这个算法,做下记录. 数据均衡分布技术在分 ...
- 手把手VirtualBox虚拟机下安装rhel6.4 linux位系统详细文档
使用Virtual Box,感觉跟Vmware差不多,我的本子的系统是win7 64位. 下面演示安装的是在VirtualBox里安装rhel 6.4 linux 32位系统.32位系统安装和 64位 ...
- pycharm中关于django和buildout的配置
pycharm提供了对django和buildout的支持,具体的配置如下: 1.django support 在pycharm的settings中修改如下3个选项 1)django project ...
- jquery cloudzoom 3.0,magiczoom 放大镜插件 破解 移除版权信息
jquery Cloud Zoom一款放大镜插件.但是无奈 官方下载的始终有版权信息,因此想到如下方法去掉版权信息,测试可行! 官方网址:http://www.starplugins.com/clou ...
- 【转载】CAS操作
[本文转载]http://blog.csdn.net/hsuxu/article/details/9467651 CAS CAS:Compare and Swap, 翻译成比较并交换. java.ut ...
- PCB 铜皮(Surface)折线多边形扩大缩小实现(第一节)
继续铜皮多边形的相关的算法, 如何用代码实现多边形的扩大与缩小,这部份内容准备分为四节内容来讲解, 第一节,折线多边形的扩大缩小(不包含圆弧) 此篇讲第一节 第二节,带圆弧的多边形的扩大缩小 第三 ...
- Ruby Numeric类
Numeric类 Numeric ---------> Integer ---------> Fixnum ...