海战(模拟题)

E - 海战

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 b consecutive 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 n , a , b , k ( 1 ≤  n  ≤ 2·10 5 , 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.

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.``
以上是题目;

题意难读,主要因为是英文的,英语不好真是硬伤。。。。?
题意大致是这样的,给你一排空间,长度为n,给了每艘船的尺寸b以及需要放置多少艘船a。空间上显示1的格子不能放炸弹。保证所有的船都能放在长度为n的空间上,并且船不能相交。问Galya最少需要多少炸弹可以炸其中的任意一条船。

题意搞懂之后,就简单多了。我的做法是遍历一遍每个单位的空间,用1来把长度为n的空间隔开,然后尽量把每个格子空间都放满…(有贪心的赶脚)
然后遍历出总共最多可以放置的船的数量再减去需要放置船的数量,这就是空格的数量,假设最坏的情况,炸弹刚好全在空格上,这时候再多一个炸弹,就必然可以炸到某一条船。所以只需要空格的数量再加上一就行了。

以下是代码:

#include <cstdio>
#include <algorithm>
#include <algorithm>
#include <cstring>
using namespace std;
char ss[200000];
int sit[200000];
int main()
{
    int n, a, b, k;
    scanf("%d%d%d%d",&n,&a,&b,&k);
    scanf("%s",ss+1);
    int t = 0;
    int num = 0;
    int cnt = 0;
    for(int i = 1; i <= n; i ++)
    {
        if(ss[i] == '1')
        {
            t = 0;
        }
        else
            t++;
        if(t % b == 0 && t != 0)
        {
            cnt ++;
            sit[num++] = i;
        }
    }
    int ans = cnt - a + 1;
    printf("%d\n",ans);
    for(int i = 0; i < ans-1; i ++)
    {
        printf("%d ",sit[i]);
    }
    printf("%d\n", sit[ans-1]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值