Codeforces #380(Div.2)D.Sea Battle【贪心】好题~

本文探讨了一种在一维网格上进行的海战游戏算法挑战。任务是在已知若干失败射击的情况下,找到最少射击次数及位置以确保击中至少一艘敌舰。文章详细解释了解决方案的逻辑,并提供了一个实现该算法的示例代码。

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的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 Round 1005 Div. 2中的D解答或解释,当前提供的引用资料并未直接涉及该轮次的比赛目详情。然而,可以基于过往比赛的经验以及相似类型的编程竞赛问提供一般性的指导。 ### 解决方案概述 通常情况下,在解决此类算法竞赛目时,会遵循特定的方法论来处理输入数据并计算所需的结果。虽然具体到Codeforces Round 1005 Div. 2 Problem D的信息未被提及,但可以根据以往经验推测可能涉及到的数据结构和算法技术: - **读取测试案例数量**:程序首先应该能够接收多个独立的测试案例数目\(t\),其中每一个案例都包含了不同的参数集[^3]。 - **解析数组元素**:针对每个测试案例,需解析给定长度为\(n\)的一系列整数\[a_1, a_2,...,a_n\]作为操作对象[^2]。 - **查询次数限制**:需要注意的是,所有测试案例中查询总数不得超过设定的最大值,比如这里提到不超过\(2 \times 10^5\)次查询[^1]。 - **输出格式规定**:当准备打印最终答案时,应按照指定格式输出结果,并继续处理下一个测试案例直到完成全部测试[^4]。 考虑到这些通用原则,如果要设计一个适用于此类型问的解决方案框架,则可能会如下所示: ```python def solve_problem(): import sys input = sys.stdin.read data = input().split() index = 0 results = [] t = int(data[index]) index += 1 for _ in range(t): n = int(data[index]) index += 1 numbers = list(map(int, data[index:index+n])) index += n # 假设这里是解决问的核心逻辑部分, # 需要根据具体的Problem D描述调整这部分代码实现。 result_for_case = "Example Result" results.append(result_for_case) print("\n".join(results)) ``` 上述伪代码展示了如何构建基本架构用于批量处理多组测试用例,但是核心业务逻辑需要依据实际问定义进行填充和完善。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值