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 ...
随机推荐
- Lightoj 1011 - Marriage Ceremonies
You work in a company which organizes marriages. Marriages are not that easy to be made, so, the job ...
- POJ1984 Navigation Nightmare —— 种类并查集
题目链接:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K T ...
- 区块链共识算法 PBFT(拜占庭容错)、PAXOS、RAFT简述
共识算法 区块链中最重要的便是共识算法,比特币使用的是POS(Proof of Work,工作量证明),以太币使用的是POS(Proof of Stake,股权证明)使得算理便的不怎么重要了,而今PO ...
- 《Visual C++ 2010入门教程》系列二:安装、配置和首次使用VS2010
作者:董波 日期:2010.6.15 写在前面 在我还在上学的时候,我选择了C++,最初我用VC6作为我的IDE,我看过很多本C++的教材,有的适合我,有的不适合我,其中有一本叫<Visual ...
- POJ3617:Best Cow Line (贪心&&后缀数组)
FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Year" competiti ...
- [Tjoi2016&Heoi2016] 树
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4551 [算法] 树链剖分 时间复杂度 : O(QlogN) [代码] #includ ...
- [Codeforces 507E] Breaking Good
[题目链接] https://codeforces.com/contest/507/problem/E [算法] 首先BFS求出1到其余点的最短路 , N到其余点的最短路,记为distA[]和dist ...
- bzoj 1731 Layout 排队布局 —— 差分约束
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1731 差分约束: ML: dis[y] - dis[x] <= k,即 x 向 y 连 ...
- 前端之html第一天
一.内容
- bzoj 4784: [Zjoi2017]仙人掌【tarjan+树形dp】
其实挺简单的但是没想出来---- 首先判断无解情况,即,一开始的图就不是仙人掌,使用tarjan判断如果一个点dfs下去有超过一个点比他早,则说明存在非简单环. 然后考虑dp,显然原图中已经属于某个简 ...