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

本文详细解析了Codeforces B. Heaters题目,介绍了一种使用贪心算法解决加热器覆盖问题的方法,通过尽可能扩展右端点来找到最小数量的加热器,以覆盖所有房间。

任意门: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 [posr+1;pos+r1][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=6r=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 (1n,r10001≤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 (0ai10≤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

 

内容概要:本文围绕六自由度机械臂的人工神经网络(ANN)设计展开,重点研究了正向与逆向运动学求解、正向动力学控制以及基于拉格朗日-欧拉法推导逆向动力学方程,并通过Matlab代码实现相关算法。文章结合理论推导与仿真实践,利用人工神经网络对复杂的非线性关系进行建模与逼近,提升机械臂运动控制的精度与效率。同时涵盖了路径规划中的RRT算法与B样条优化方法,形成从运动学到动力学再到轨迹优化的完整技术链条。; 适合人群:具备一定机器人学、自动控制理论基础,熟悉Matlab编程,从事智能控制、机器人控制、运动学六自由度机械臂ANN人工神经网络设计:正向逆向运动学求解、正向动力学控制、拉格朗日-欧拉法推导逆向动力学方程(Matlab代码实现)建模等相关方向的研究生、科研人员及工程技术人员。; 使用场景及目标:①掌握机械臂正/逆运动学的数学建模与ANN求解方法;②理解拉格朗日-欧拉法在动力学建模中的应用;③实现基于神经网络的动力学补偿与高精度轨迹跟踪控制;④结合RRT与B样条完成平滑路径规划与优化。; 阅读建议:建议读者结合Matlab代码动手实践,先从运动学建模入手,逐步深入动力学分析与神经网络训练,注重理论推导与仿真实验的结合,以充分理解机械臂控制系统的设计流程与优化策略。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值