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 ...
随机推荐
- web项目开发 之 前端规范 --- HTML编码规范
此文严格按照W3C规范和部分实际项目可读性,浏览器加载,性能等众多属性权衡,做出平时前端编码规范文 档.供广大web工作者参考并实施,对维护和项目扩展升级都能省时省力. 转载请注明出处,JS前端实用开 ...
- 原生js写简单轮播图方式1-从左向右滑动
轮播图就是让图片每隔几秒自动滑动,达到图片轮流播放的效果.轮播图从效果来说有滑动式的也有渐入式的,滑动式的轮播图就是图片从左向右滑入的效果,渐入式的轮播图就是图片根据透明度渐渐显示的效果,这里说的是实 ...
- Java 抽象类和接口的理解
Java 抽象类和接口的理解 一.抽象类 为什么使用抽象类(个人理解): 面向对象的概念是,我们知道的所有的对象都是通过类来描绘的,如果类包含的信息不能描绘一个具体的对象,就需要抽象来解决了,意思是一 ...
- GC回收算法--当女友跟你提分手!
Java语言引入了垃圾回收机制,让C++语言中令人头疼的内存管理问题迎刃而解,使得我们Java狗每天开开心心地创建对象而不用管对象死活,这些都是Java的垃圾回收机制带来的好处.但是Java的垃圾回收 ...
- Violet蒲公英
传送门 题目要求求出给定区间内编号最小的众数,强制在线. 虽然说这是个黑题……不过我们可以用暴力分块解决它.首先先对所有数离散化,这个不影响众数.我们先预处理出每个数在前i个块内出现了多少次,再预处理 ...
- 洛谷 P1541 乌龟棋 —— DP
题目:https://www.luogu.org/problemnew/show/P1541 DP. 代码如下: #include<iostream> #include<cstdio ...
- org.springframework.web.struts.ContextLoaderPlugIn 和 org.springframework.web.context.ContextLoaderListener
org.springframework.web.struts.ContextLoaderPlugIn 和 org.springframework.web.context.ContextLoaderLi ...
- VS2010打包回顾
1. 在vs2010 选择“新建项目”à“ 其他项目类型”à“ Visual Studio Installerà “安装项目”: 命名为:Setup1 . 这是在VS2010中将有三个文件夹, 1. ...
- 不让浏览器缓存input的值
方法一: 在不想使用缓存的input中添加 autocomplete="off"eg: <input type="text" autocomplete=& ...
- failed to push some refs to 'https://gitee.com/ftl_663/java-shop.git'
1.git init 2.git add . 3.git commit -m "init" 4.git remote add origin https://gitee.com/ ...