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 工程资料 立即下载

相关推荐

基于李雅普诺夫的MPC模型预测AUV水下航行器自主编队轨迹跟踪控制复现

内容概要:本文围绕“基于李雅普诺夫的MPC模型预测AUV水下航行器自主编队轨迹跟踪控制”展开,复现并改进了一种结合李雅普诺夫稳定性理论与模型预测控制(MPC)的先进控制策略,用于实现多个欠驱动自主水下航行器(AUV)的协同编队与精确轨迹跟踪。该方法通过构建合理的代价函数和系统动力学模型,在满足控制约束的同时,利用李雅普诺夫函数确保系统的渐近稳定性,提升了编队控制的鲁棒性与动态响应性能。文中提供了完整的Matlab仿真代码,涵盖环境建模、控制器设计、状态反馈与轨迹优化等关键环节,实现了对多AUV系统在复杂水下环境中自主编队与路径跟踪的高效仿真验证。; 适合人群:具备自动控制理论基础、熟悉Matlab/Simulink仿真环境,从事水下机器人、智能控制、多智能体协同控制等相关领域研究的研发人员与高校研究生。; 使用场景及目标:① 掌握基于李雅普诺夫的MPC控制在非线性欠驱动系统中的设计与实现方法;② 学习多AUV编队控制中稳定性分析与轨迹跟踪的协同优化策略;③ 借助提供的Matlab代码进行算法复现、性能调优与二次开发,应用于相关科研或工程项目。; 阅读建议:建议读者结合现代控制理论与非线性系统分析知识,重点理解李雅普诺夫函数与MPC代价函数的融合机制,并通过调试仿真代码直观感受控制效果,进而拓展至其他多智能体协同控制场景。

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

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

朝夕 464

基于YOLO11的垃圾分类AI模型训练实战

通过这次实战,我们成功训练出了一个高性能的垃圾分类AI模型。项目展示了从数据准备、模型训练到结果分析的完整流程,为AI在环保领域的应用提供了有价值的参考。项目文件结构│ ├── best.pt # 最佳模型权重│ └── last.pt # 最新模型权重├── results.csv # 详细训练数据├── results.png # 训练曲线图├── confusion_matrix.png # 混淆矩阵└── args.yaml # 训练参数配置。

lydz1的博客 1663

从零开始的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 215

VFIO框架源码分析(一)- 从初始化到设备访问

本文深入剖析了Linux内核中的VFIO(Virtual Function I/O)框架,详细解读了其从初始化到设备访问的全过程。VFIO通过Container->Group->Device的层次化设计,利用IOMMU提供安全隔离,允许用户空间直接访问物理设备。文章基于Linux v5.15版本,重点分析了vfio_init模块入口、Container创建与配置、Group关联以及最终设备访问等关键环节,揭示了VFIO如何通过逐层授权机制构建安全可靠的设备直通模型,为虚拟化技术提供核心支持。

qq_36071928的博客 383

C++ muduo网络库知识分享01 - Linux平台下muduo网络库源码编译安装

Muduo is a multithreaded C++ network library based on the reactor pattern. muduo库的介绍就是:一个基于reactor反应堆模型的多线程C++网络库。 muduo网络库是C++语言开发的一个非常优秀的网络库,作者陈硕,muduo网络库在多线程环境下性能非常高,该系列文章会分享一些muduo库源码的知识,包括它的线程池源码...

大秦坑王的专栏 3万+

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

Xilinx PCIe IP核示例工程仿真流程与关键任务解析

本文详细解析了Xilinx PCIe IP核示例工程的仿真流程与关键任务。通过搭建仿真环境、解读初始化日志,并深入剖析TSK_TX_MEMORY_WRITE_32等核心任务的TLP报文生成与交互过程,帮助开发者掌握PCIe IP核的仿真调试方法,为实际硬件设计提供可靠的验证基础。

weixin_28314575的博客 513

CF1066B Heaters(贪心)

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

weixin_30378311的博客 249

电容式麦克风:从原理到实际电路波形解析

本文深入解析电容式麦克风的工作原理,从核心的振膜与背板结构、电荷锁定机制到场效应管放大器的作用。通过实物拆解与等效电路分析,阐明声音振动如何转换为电压信号,并指导如何用示波器实测波形,直观展现声电转换过程,适合电子爱好者和音频技术学习者。

Black的专栏 1100

Windows下Anaconda换清华镜像源最全指南(含常见问题解决)

本文提供了一份详尽的Windows系统下Anaconda配置清华镜像源的完整指南。针对网络下载缓慢的痛点,文章详细对比了命令行添加与直接编辑配置文件两种主流方法,并重点解决了配置过程中常见的文件缺失、安装失败、环境求解缓慢等疑难问题,帮助用户一劳永逸地提升包管理效率。

brandy的博客 343

【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 的博客 1643

Python爬虫实战:逆向解析央视频加密参数(附完整代码)

本文详细解析了央视频加密体系的技术实现,通过Python逆向工程实战演示如何解密动态密钥、AES加密和自定义哈希算法等核心组件。文章提供完整代码示例,涵盖GUID生成、参数构造和异步请求处理,帮助开发者掌握现代视频平台的反爬机制破解方法。

weixin_42532473的博客 1150

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的博客 445

【Segment Anything Model】十(1):利用SAM做分类任务,方法二,不放过SAM强大特征提取能力,将SAM提取到的ImageEmbedding插入到其他cnn网络中

直接黏贴复制就好'''该类定义了VGG-16的基本结构。features: 代表VGG网络的主体部分,即所有的卷积层和池化层。avgpool: 是自适应平均池化层,将特征图大小调整为7x7。classifier: 是VGG网络的分类部分,由全连接层组成。_initialize_weights(): 初始化权重的方法。

cvxiayixiao的博客 3567

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

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

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

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

假人有限元模型

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

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

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

辉小歌的博客 412

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的博客 101

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的博客 385

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

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的博客 180

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

题目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...

二喵君的博客 535
上一篇: 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、付费专栏及课程。

余额充值