Codeforces Round #515 (Div. 3) B. Heaters【 贪心 区间合并细节 】

别再让时钟漂移搞砸你的高速通信:手把手拆解PCIe/USB3.0弹性缓冲器(Elastic Buffer) 本文深入解析了PCIe/USB3.0弹性缓冲器(Elastic Buffer)在高速通信中的关键作用,针对时钟漂移问题提供了精妙解决方案。通过对比PCIe 3.0、USB3.0等协议的弹性缓冲特性,并结合Verilog实现示例,详细阐述了时钟补偿模块的工作原理和系统级设计考量,帮助工程师有效解决数据丢失问题。 阅读详情

任意门:http://codeforces.com/contest/1066/problem/B

B. Heaters
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vova's house is an array consisting of nn elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The ii-th element of the array is 11 if there is a heater in the position ii, otherwise the ii-th element of the array is 00.

Each heater has a value rr (rr is the same for all heaters). This value means that the heater at the position pospos can warm up all the elements in range [pos−r+1;pos+r−1][pos−r+1;pos+r−1].

Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.

Vova's target is to warm up the whole house (all the elements of the array), i.e. if n=6n=6, r=2r=2 and heaters are at positions 22 and 55, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first 33 elements will be warmed up by the first heater and the last 33 elements will be warmed up by the second heater).

Initially, all the heaters are off.

But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.

Your task is to find this number of heaters or say that it is impossible to warm up the whole house.

Input

The first line of the input contains two integers nn and rr (1≤n,r≤10001≤n,r≤1000) — the number of elements in the array and the value of heaters.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤10≤ai≤1) — the Vova's house description.

Output

Print one integer — the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.

Examples
input
Copy
6 2
0 1 1 0 0 1
output
Copy
3
input
Copy
5 3
1 0 0 0 1
output
Copy
2
input
Copy
5 10
0 0 0 0 0
output
Copy
-1
input
Copy
10 3
0 0 1 1 0 1 0 0 0 1
output
Copy
3
Note

In the first example the heater at the position 22 warms up elements [1;3][1;3], the heater at the position 33 warms up elements [2,4][2,4] and the heater at the position 66 warms up elements [5;6][5;6] so the answer is 33.

In the second example the heater at the position 11 warms up elements [1;3][1;3] and the heater at the position 55 warms up elements [3;5][3;5] so the answer is 22.

In the third example there are no heaters so the answer is -1.

In the fourth example the heater at the position 33 warms up elements [1;5][1;5], the heater at the position 66 warms up elements [4;8][4;8] and the heater at the position 1010 warms up elements [8;10][8;10] so the answer is 33.

 

题意概括:

N 个房间,标记为 1 的房间有加热器,加热范围 【 i-r+1, i+r-1】;求使用最少的加热器加热所有房间。

解题思路:

这个贪心并不难想,顺序遍历,尽可能的扩展右端点。

但wa了好多发,主要是区间连接的细节问题。

ans_ed 为已覆盖集合的最右端的下一个。

看代码吧,心好凉。

AC code:

 1 #include <bits/stdc++.h>
 2 #define LL long long
 3 #define INF 0x3f3f3f3f
 4 using namespace std;
 5 const int MAXN = 1e3+1;
 6 
 7 int a[MAXN], l[MAXN], r[MAXN], cnt;
 8 int ans_ed, now_st, now_ed; //当前已选集合能覆盖的最右端的下一个!!!下标!!!,当前结点能覆盖的左端,当前结点能覆盖的右端
 9 int pre_index, now_index;   //前一个可加的下标,当前可加的下标
10 int N, R;
11 int ans;
12 bool chch;
13 
14 int main()
15 {
16     scanf("%d%d", &N, &R);
17     bool flag = true;
18     cnt = 0;
19     for(int i = 1; i <= N; i++){
20         scanf("%d", &a[i]);
21         if(a[i] == 1){
22             flag = false;
23             //nxt[++cnt] = i;
24             cnt++;
25             l[cnt] = i-R+1; //当前可加热的左端
26             r[cnt] = i+R-1; //当前可加热的右端
27         }
28     }
29     //see see
30     //for(int i = 1; i <= cnt; i++) printf("%d ", nxt[i]);
31     //puts("");
32     if(flag) printf("-1\n");    //没有加热器
33     else{
34         chch = false;
35         ans = 0;
36         ans_ed = 1;             //答案最右端初始化为1
37         int k = 1;              //从头开始顺序遍历加热器
38         bool book = false;
39         do{
40             if(l[k] < ans_ed)   //当前加热器k可连接前一个加热区,说明该点可用
41             {
42                 book = true;    //标记有保留考虑项
43                 pre_index = k;  //记录当前下标
44                 k++;            //讨论下一个,如果下一个可加,则当前这个不加
45                 if(k == cnt+1){   //如果当前这个为最后一个
46                     if(ans_ed < N+1){ //当前未全覆盖
47                         ans++;
48                         ans_ed = r[k-1]+1;      //注意加1
49                     }
50                 }
51                 continue;
52             }
53 
54             if(book){           //说明前一个可选,但是未选
55                 if(l[k] > ans_ed){         //当前这个不可以加,说明上一个要加
56                     now_index = pre_index;
57                 }
58                 else now_index = k;          //当前这个可加
59                 ans++;
60                 ans_ed = r[now_index]+1;
61                 if(now_index == pre_index) --k;
62                 book = false;
63             }
64             else if(l[k] > ans_ed) //当前加热器不能连接前一个加热区,说明出现断层,但是是顺序遍历下来的,说明无解
65             {
66                 chch = true;break;
67             }
68             else{                      //无缝连接,选!
69                 ans++;                //答案加一
70                 ans_ed = r[k]+1;       //更新集合最右端
71             }
72             k++;                   //讨论下一个;
73         }while(k <= cnt);
74         if(ans_ed <= N || ans == 0) chch = true;
75         if(chch) printf("-1\n");
76         else printf("%d\n", ans);
77     }
78     return 0;
79 }
View Code

 

产业园区运营负责人如何实现创新资源的系统化配置?.docx 产业园区运营负责人如何实现创新资源的系统化配置? 立即下载

相关推荐

状态估计【扩展卡尔曼滤波算法的神经网络训练】BP神经网络、扩展卡尔曼滤波EKF+BP、粒子滤波PF轨迹估计研究(Matlab代码实现)

【状态估计】【扩展卡尔曼滤波算法的神经网络训练】BP神经网络、扩展卡尔曼滤波EKF+BP、粒子滤波PF轨迹估计研究(Matlab代码实现)内容概要:本文主要介绍了基于变频控制的多相交错LLC谐振变换器闭环仿真研究,重点探讨了多相交错并联技术在降低输出电流纹波和提升功率容量方面的优势,分析了交错角对纹波抵消的影响以及均流问题的成因。针对变频控制带来的非线性特性,文章采用扩展描述函数(EDF)建立小信号模型,用于指导PID控制器的设计,并验证其在稳态和动态工况下的控制性能。通过Simulink仿真平台对闭环系统进行建模与测试,考察了启动过程、负载阶跃响应及频率调节的平滑性,结果表明所设计的控制系统具有良好的稳态精度和动态响应能力。; 适合人群:电力电子、电气工程及其自动化等相关专业的高校师生及从事电源系统研发的工程技术人员。; 使用场景及目标:①掌握多相交错LLC谐振变换器的工作原理与建模方法;②学习变频控制下非线性系统的建模与PID环路设计技巧;③应用于高效率、低纹波开关电源的仿真设计与优化。; 阅读建议:建议结合文中提供的仿真模型与参数设置进行实践操作,重点关注EDF建模过程与控制器参数整定方法,通过对比不同交错角下的仿真结果加深对纹波抑制机理的理解。

CodeForces - 1066B Heaters (贪心,区间覆盖,index

贪心策略我感觉最不擅长了…想不到好的方法,以前做的青蛙跳,做的挺崩溃的。 实不相瞒, 这道题我想了好几节课呐,纲要的时候想,大学语文的时候想,回宿舍想… 其实我的思路并不清晰,那些所谓的想都是无用功。看别人的题解,因为乱七八糟的解法还有我以前写的冗长并且没用的代码,让我很浮躁…看不下去,导致这道题迟迟 a 不出来… 卡了18天,昨天终于做出来了。还是看到某位大佬的简短的代码,不仅让我卧槽一声,竟...

朝夕 464

Flink CDC 1.18.1 Oracle 数据同步到postgresql

8、修改Flink的配置文件,主要是各种服务的绑定地址,默认为localhost,统统改为0.0.0.0,如:rest.address: 0.0.0.0 #localhost。将flink-sql-connector-oracle-cdc-3.0.1.jar复制到flink-1.18.1/lib目录中。6、下载 flink-connector-jdbc-3.1.1-1.17.jar,postgresql-42.7.3.jar。7、安装postgresql就不说了,相信你已经有了数据库了。

ynzhangyao的专栏 1800

Codeforces Round #496 (Div. 3)(暂时只有前五道)

                Codeforces Round #496 (Div. 3) A. Tanya and Stairwaystime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputLittle girl Tanya climbs the s...

你的代码 受命于你 517

基于PDF文档结构的数字隐写

PDF隐写

xiongshivigor的博客 1426

【LaTeX】IEEE会议模板中使用BibTex插入文献(解决报错:Something‘s wrong--perhaps a missing \item. \end{thebibliography}

使用IEEE会议论文LaTeX模板时遇到的一个错误,问题描述为:使用Texstudio编译LaTeX模板报错,报错信息提示为。出现的原因为IEEE会议论文模板自带的参考文献引用方式为手动插入,即不使用格式的参考文献,手动将参考文献调整为合适格式,然后粘贴到文本中,我准备将其换成方法引用参考文献。我的替换步骤为删除原始参考文献对应内容,即模板中的: 将以下函数添加到原始参考文献对应位置,对应上文新建的文件。 再次编译出现报错 删除文件夹中的文件 修改模板中的引用名称 引用方法为: 将 替换为 再次编译——

TangPlusHPC的博客 9917

题目B-Competition - 20181017

题目 Vova's house is an array consisting of nn elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The ii-th elemen...

二喵君的博客 534

汉字跨越三千年的生命力与数字时代适应性

文字作为文明载体,其演变规律始终遵循实用性与适应性的平衡。汉字作为现存最古老的文字系统,凭借形义结合的六书构造原理,实现了超方言的文化统一功能。在计算机处理方面,五笔输入法与Unicode支持证明了其技术兼容性,而视觉识别效率的先天优势使其在信息时代更具传播效力。从甲骨文识别AI到现代书法设计,汉字系统持续展现强大的文化承载力与技术适应性,这种活态传承为数字人文研究提供了独特样本。

weixin_29058331的博客 355

CF1066B Heaters(贪心)

题意描述: Vova先生的家可以看作一个n×1的矩形,寒冷的冬天来了,Vova先生想让他的家里变得暖和起来。现在我们给你Vova先生家的平面图,其中111表示这个地方是加热炉,0表示这个地方什么也没有。所有加热器都有一个加热半径r,一个位于ai加热器可以加热[ai−r+1,ai+r−1]的范围。现在,Vova先生想让他的整个家都变得暖和,一开始所有的加热器都是关闭的,请你求出Vova先生最少要开...

weixin_30378311的博客 249

转录组数据去批次方法整理(combat,combat-seq,removeBatchEffect)

正是因为这些非生物学的因素存在就有可能会导致我们的结果偏离真实的情况,那么实际分析的过程中研究者应当评估是否存在批次效应,并决定是否要进行去批次处理。批次效应是指样本在不同批次处理及测量的情况下引入与生物学情况不相关的技术误差,比如不同试剂耗材,不同操作者,不同的实验时间等。combat函数是基于贝叶斯框架下的调整数据批次效应的函数,主要适用于已经被过滤和标准化后的数据(combat-seq函数是基于二项回归分布下的调整数据批次效应的函数,主要适用于。值得注意的是,即使使用了所谓的去批次效应的工具,

zfyyzhys的博客 7862

Unity 编辑器篇|(四)编辑器拓展GUI类 (全面总结 | 建议收藏)

GUI 类是 Unity GUI 的接口,并且具有手动定位功能。“即时模式”GUI 系统(也称为 IMGUI)是一个完全独立的功能系统,不同于 Unity 基于游戏对象的主 UI 系统。IMGUI 是一个代码驱动的 GUI 系统,主要用作程序员的工具。为了驱动该系统,需在实现脚本上调用 OnGUI 函数即时模式 GUI 系统常用于:创建游戏内调试显示和工具。为脚本组件创建自定义检视面板。创建新的编辑器窗口和工具以扩展 Unity 本身。

游戏开发小Y的博客 12万+

【CF】Codeforces Round #515 (Div. 3)

目录前言题目A - Vova and TrainDescriptionInputOutputExampleNoteSolutionCodeB - HeatersC - Books QueriesD - Boxes PackingE - Binary Numbers AND Sum 前言 打了一场 CFCFCF 的重现赛,然而发现自己连 DivDivDiv 333 都打不好…好颓呀 QAQQAQQAQ 怎么办办呢——写篇题解重温一下代码吧… PS:题目描述都用中文了,就不放英文了,敬请谅解! 题目 A - V

Admin-zx 的博客 1642

极验点选click3.1.0所对应fullpage.js的逆向分析

极验3代点选验证,本文适用于使用click.3.1.0.js和fullpage919-eyz9lh.js版本的web验证码,没有细节内容,适合有基础的开发者阅读。

shanf7921的博客 650

Codeforces Round #515 (Div. 3) 题解

文章目录A. Vova and TrainB. HeatersC. Books QueriesD. Boxes PackingE. Binary Numbers AND SumF. Yet another 2D Walking A. Vova and Train 题意 给出区间[1,n][1,n][1,n],其中你想访问坐标为vvv的倍数的点,而[l,r][l,r][l,r]区间是访问不了的,问你能访问多少个vvv的倍数的点。 解题思路 先解决这样一个问题,[1,n][1,n][1,n]区间中有多少

pursuit的博客 444

yolov5直接调用zed相机实现三维测距(python)

zed代码比较简洁,但是一定要把环境配置好,后续自己会把他当做普通双目标定,去对比分析效果,并完善相关功能。

积极向上的mr.d的博客 1万+

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Codeforces Round #515 (Div. 3) B. Heaters

题目大意 房间为一条直线长度为n 如果pos位置有加热器并且打开则能加热[pos - r + 1, pos + r - 1]区域 告诉你哪些位置有加热器0无1有 问最少开启多少加热器能把整个房间加热 i遍历每个未加热的位置 使用last记录上次开启的加热器位置 j扫描i + r - 1位置到上次的加热器位置内是否有加热器如果有则打开 i跳到下一个未加热的位置i = k + r 注意数组开2000因...

weixin_45113721的博客 98

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

Codeforces Round #515 (Div. 3)B【贪心】B. Heaters(较详细讲解)

//题意:有n个位置m个热水器每个热水器加热范围是[i-r+1,i+r-1],每个地方有热水器为一否则为零 //问至少需要多少个热水器可以保证每个地方至少被一个热水器加热,不能保证每个地方被一个热水器加热的话输出-1 //思路:贪心,首先我们先从r开始向前枚举这样可以保证前面被加热但后面也覆盖较多的位置.找到一个i时我们就要从 //2*r+i-1向前枚举因为枚举时尽量从第一个可以覆盖i~x的整个地方开始如下图 //这样的话可以保证用的热水器最少 // AC代码如下 #include<iostream

sssssss521的博客 138

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Codeforces Round #515 (Div. 3)【未完结】

2022.3.9 题单地址:https://codeforces.com/contest/1066 目录A. Vova and Train【思维】 A. Vova and Train【思维】

辉小歌的博客 411

Codeforces Round #515 (Div. 3) 解题报告(A~E)

题目链接:http://codeforces.com/contest/1066 1066 A. Vova and Train 题意:Vova想坐火车从1点到L点,在路上v的整数倍的点上分布着灯笼,而在路上从l到r处停着别的火车,它挡着Vova的视线使他看不到灯笼。给定L,v,l,r求Vova能看到的灯笼数。 分析:从1到x上所有的灯笼数量为x/v个。则路上所有的灯笼数为L...

weixin_30515513的博客 100

Codeforces Round #515 (Div. 3) A、B、C、D

A. Vova and Train time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Vova plans to go to the conference by train. Initially, the trai...

cloudy_happy的博客 384

Codeforces Round #547 (Div. 3) D

Input Copy 10 codeforces dodivthree Output Copy 5 7 8 4 9 2 2 9 10 3 1 Input Copy 7 abaca?b zabbbcc Output Copy 5 6 5 2 3 4 6 7 4 1 2 #include <stdio.h> #include <string.h> #include <ma...

weixin_43870114的博客 179

从零开始的LC刷题(98): Heaters

原题: Winter is coming! Your first job during the contest is to design a standard heater with fixed warm radius to warm all the houses. Now, you are given positions of houses and heaters on a horizon...

Kuroneko 214

Educational Codeforces Round 7 题解

A. Infinite SequenceConsider the infinite sequence of integers: 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5…. The sequence is built in the following way: at first the number 1 is written out, then the

long-long-ago 804

C. Chessboard time limit per test1 second memory limit per test256 megabytes inputstandard input out

C. Chessboardtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputMagnus decided to play a classic chess game. Though what he saw in his locker shocked ...

SunMoonVocano的博客 3815
上一篇: POJ 1984 Navigation Nightmare 【经典带权并查集】
下一篇: 2016 ACM/ICPC亚洲区大连站-重现赛 解题报告
莜莫oO
博客等级 码龄9年 7粉丝 226原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值