zoukankan      html  css  js  c++  java
  • codeforces_738D

    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 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 nabk (1 ≤ n ≤ 2·105, 1 ≤ 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 ton, 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.

     题意:1*n的矩阵上,a条船,每条都是b的长度,已经开了k炮,每个格子只能被一条船占据。问最少再开多少炮能至少击中一条船,输出这几炮的位置。

    比赛是以为搞不出来,结果给搞出来了,开心!!!

    思路:抽象一下,也就是说,在序列中某些位置加上1后,序列中能放下的长度b的0序列的个数<=a-1。

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    #define N 200005
    
    struct Seg
    {
        int l,r,dis;
    } seg[N];
    bool cmp(Seg a,Seg b)
    {
        return a.dis>b.dis;
    }
    
    int main()
    {
        int n,a,b,k;
        char str[N];
        scanf("%d%d%d%d",&n,&a,&b,&k);
        scanf("%s",str);
        int st=0,en=0,p=0,cnt=0;
        while(p<n)
        {
            if(str[p]=='1')
            {
                en=p-1;
                Seg se;
                se.l=st;
                se.r=en;
                se.dis=en-st+1;
                if(se.l<=se.r)
                    seg[cnt++]=se;
                while(str[p]=='1')
                    p++;
                st=en=p;
            }
            if(str[n-1]=='0'&&p==n-1)
            {
                en=n-1;
                Seg se;
                se.l=st;
                se.r=en;
                se.dis=en-st+1;
                if(se.l<=se.r)
                    seg[cnt++]=se;
            }
            p++;
        }
        sort(seg,seg+cnt,cmp);
        int ans[N],cnta=0;
        int cntt=0;
        for(int i=0; i<cnt; i++)
            for(int j=seg[i].l+b-1; j<=seg[i].r; j+=b)
            {
                if(cntt>=a-1)
                    ans[cnta++]=j+1;
                else
                    cntt++;
            }
        printf("%d
    ",cnta);
        for(int i=0; i<cnta; i++)
        {
            printf("%d",ans[i]);
            if(i==cnta-1)
                printf("
    ");
            else
                printf(" ");
        }
        return 0;
    }
  • 相关阅读:
    虚函数和纯虚函数
    函数指针
    const成员函数
    随笔
    Myeclipse/eclipse的Web project改写成Maven项目
    Maven项目配置不接文件名
    Tomcat需要更改三个端口,才能在一台机器上搭载多个tomcat
    maven错误:Project configuration is not up-to-date with pom.xml
    Failed to execute goal on project MakeFriends: Could not resolve dependencie The POM for .chengpai.jtd:jtd-service-api:jar:1.0-SNAPSHOT is missing, no dependency information available
    编译器问题:运行maven,报错-Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable and mvn script match.
  • 原文地址:https://www.cnblogs.com/jasonlixuetao/p/6086487.html
Copyright © 2011-2022 走看看