当前位置: 代码迷 >> 综合 >> Codeforces Round #380D. Sea Battle(贪心)
  详细解决方案

Codeforces Round #380D. Sea Battle(贪心)

热度:63   发布时间:2023-12-06 20:06:54.0
D. Sea Battle
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The first line contains four positive integers nabk (1?≤?n?≤?2·1051?≤?a,?b?≤?n0?≤?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.

Output

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.

Examples
input
5 1 2 1
00100
output
2
4 2
input
13 3 2 3
1000000010001
output
2
7 11
Note

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个格子,给你a条船,这a条船,每条船的长度都为b,且这a条船都放在为0的格子上,没有重叠。问你在不知道船在何处的情况下,最少射几箭就可以射中其中的一条船。

分析:

看看三个0,船的长度如果为2,那么只要射箭一次即可。如果四个0且船的长度为2,即0 0 0 0,只需要射两箭。很多人会想这不有三组连续的两个0么,那不是最起码得射3箭。其实只要只要射第二个0和第四个零就可以了,如果船横跨第二个和第三个零,这样也可以被射中。

具体做的时候只要计算每段被1划分开来的区间中0的个数,然后除以船的长度,就是这段区间所要射的最少箭数。所有区间的射箭数加起来之后,减去船的个数,然后+1,即为最少需要射箭的次数。因为没有加1之前得到的差为最坏情况下,必须试探射箭的次数。


AC代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 200005;
int a[maxn], index[maxn];
int main()
{int n, boat, length, miss;char tmp;while(scanf("%d%d%d%d", &n, &boat, &length, &miss) != EOF){for(int i = 0; i < n; i++){scanf(" %c", &tmp);a[i] = tmp - 48;}int pre = -1, cou = 0, zero = 0, cnt = 0, ans;for(int i = 0; i < n; i++){if(a[i] == 1){int dif = i - pre - 1;cou += dif / length;pre = i;zero = 0;}else{zero++;if(zero == length){zero = 0;index[cnt++] = i+1;}}}if(a[n-1] == 0){int dif = n - pre - 1;cou += dif / length;}ans = cou - boat + 1;cout << ans << endl;for(int i = 0; i < ans; i++){if(i == ans - 1)printf("%d\n", index[i]);elseprintf("%d ", index[i]);}}return 0;
}



  相关解决方案