Codeforces #380(Div.2)D.Sea Battle【贪心】好题~
来源:互联网 发布:儿童中文配音软件 编辑:程序博客网 时间:2024/05/29 08:30
Galya is playing one-dimensional Sea Battle on a 1 × n grid. In this game a ships are placed on the grid. Each of the ships consists of bconsecutive cells. No cell can be part of two ships, however, the ships can touch each other.
Galya doesn't know the ships location. She can shoot to some cells and after each shot she is told if that cell was a part of some ship (this case is called "hit") or not (this case is called "miss").
Galya has already made k shots, all of them were misses.
Your task is to calculate the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship.
It is guaranteed that there is at least one valid ships placement.
The first line contains four positive integers n, a, b, k (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ n, 0 ≤ k ≤ n - 1) — the length of the grid, the number of ships on the grid, the length of each ship and the number of shots Galya has already made.
The second line contains a string of length n, consisting of zeros and ones. If the i-th character is one, Galya has already made a shot to this cell. Otherwise, she hasn't. It is guaranteed that there are exactly k ones in this string.
In the first line print the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship.
In the second line print the cells Galya should shoot at.
Each cell should be printed exactly once. You can print the cells in arbitrary order. The cells are numbered from 1 to n, starting from the left.
If there are multiple answers, you can print any of them.
5 1 2 100100
24 2
13 3 2 31000000010001
27 11
There is one ship in the first sample. It can be either to the left or to the right from the shot Galya has already made (the "1" character). So, it is necessary to make two shots: one at the left part, and one at the right part.
题目大意:
有一个长度为N的1*N的一个范围内,一共有a艘船,每只船长度为b,其中有K个已经轰炸过的位子,保证这些轰炸过的位子一定不是船的某部分,问你最少再轰炸多少次,就能轰炸到一个船的某部分,输出任意方案即可。
输入一行长度为N的字符串,其中1表示轰炸过的地方,0表示没有轰炸过的地方。
思路:
1、首先可以确定这样一个问题,如果有连续的b个0,那么这里很有可能就是一艘船,那么对应我们统计一共有多少个这样的位子。
2、假设我们现在用有cont个这样的位子,那么对应就有cont个地方可能有船,然而本身一共有a只船,而且题目要求只要轰炸都一只船的某一部分就可以了,那么对应我们只要轰炸cont-(a-1)次,就一定能够轰炸到某一只船的某一个部分,那么对应输出cont-(a-1)个任意可能有船的位子即可。
Ac代码:
#include<stdio.h>#include<string.h>using namespace std;int ans[250000];char a[250000];int main(){ int n,b,aa,k; while(~scanf("%d%d%d%d",&n,&aa,&b,&k)) { scanf("%s",a); int cnt=0; int cont=0; for(int i=0;i<n;i++) { if(a[i]=='0') { cnt++; if(cnt==b) { ans[cont++]=i; cnt=0; } } else if(a[i]=='1') { cnt=0; } } cont-=aa-1; printf("%d\n",cont); for(int i=0;i<cont;i++) { printf("%d ",ans[i]+1); } printf("\n"); }}
- Codeforces #380(Div.2)D.Sea Battle【贪心】好题~
- Codeforces Round #380 Div. 1 B. Sea Battle(贪心)
- Codeforces Round #380 (Div. 2) D. Sea Battle
- Codeforces Round #380 D. Sea Battle (思维题)
- CodeForces 729D Sea Battle(思维题)
- Codeforces(738D)-D. Sea Battle
- Codeforces738D-Sea Battle(贪心)
- cf 729D Sea Battle 【模拟,贪心】
- codeforces 738D Sea Battle 思维+构造
- 【Codeforces 729D Sea Battle】+ 简单思维
- Codeforces Round #Pi (Div. 2) D. One-Dimensional Battle Ships
- Technocup 2017 - Elimination Round 2 D. Sea Battle
- Codeforces Round #339 (Div. 2) D.Skills(贪心)
- Hot Days Codeforces Round #132 (Div. 2) D(贪心)
- Codeforces Round #276 (Div. 2)(C贪心,D)
- Codeforces Round #298 (Div. 2) D. Handshakes(贪心)
- Codeforces Round #377 (Div. 2)D. Exams(二分+贪心)
- 【贪心】Codeforces Round #339 (Div. 2) D
- 关于中途放弃Maven出现的恶果
- AES,DES,3DES的区别
- Java语言类库中几个常用的包
- Hibernated多表查询
- 微信小程序开发之下拉刷新 上拉加载
- Codeforces #380(Div.2)D.Sea Battle【贪心】好题~
- Mysql常用的函数
- SimpleDateFormat类的使用
- Struts2访问Servlet API
- case语句中允许使用的值有哪些?
- Android(安卓)布局
- 浅谈javascript中的defer、async
- 浮点数的二进制表示
- 文章标题