zoukankan      html  css  js  c++  java
  • Codeforces 729D Sea Battle(简单思维题)

    http://codeforces.com/contest/738/problem/D

    https://www.cnblogs.com/flipped/p/6086615.html   原

    题意:海战棋游戏,长度为n的01串,1代表炸过且没有船的位置,0代表没有炸过的位置。有a个船,长度都是b,求打到一艘船至少还需要多少炸弹,并输出炸的位置。

    分析:每连续的b个0就要炸一次,不然不知道有没有是不是刚好一艘船在这b个位置上面。贪心可知炸这b个的最后一个最划算。因为只要炸到一艘即可,所以答案减去a-1,即有a-1艘可以不管它。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<set>
     7 #include<vector>
     8 #include<stack>
     9 #include<queue>
    10 #include<map>
    11 using namespace std;
    12 #define ll long long
    13 #define se second
    14 #define fi first
    15 const int Mos = 0x7FFFFFFF;  //2147483647
    16 const int nMos = 0x80000000;  //-2147483648
    17 const int N=2e5+5;
    18 
    19 int n,a,b,k;
    20 char g[N];
    21 int d[N];
    22 
    23 int main()
    24 {
    25     cin>>n>>a>>b>>k;
    26     scanf("%s",g+1);
    27 
    28     int cnt=0,m=0;
    29     for(int i=1;i<=n;i++)
    30     {
    31         if(g[i]=='0')
    32         {
    33             cnt++;
    34             if(cnt==b)
    35             {
    36                 cnt=0;
    37                 d[m++]=i;
    38             }
    39         }
    40         if(g[i]=='1')  cnt=0;
    41     }
    42     m-=(a-1);
    43     cout<<m<<endl;
    44     for(int i=0;i<m-1;i++)
    45         cout<<d[i]<<" ";
    46     cout<<d[m-1]<<endl;
    47 }
  • 相关阅读:
    Luogu P1396 营救
    Luogu P1339 热浪Heat Wave
    哈夫曼树学习笔记
    题解 CF1372C
    题解 CF 1372 B
    题解 CF 1372A
    题解 UVA1193 Radar Installation
    题解 洛谷 P2287 [USACO07NOV]Sunscreen G
    洛谷 P1080 国王游戏 题解
    牛客练习赛 66C公因子 题解
  • 原文地址:https://www.cnblogs.com/thunder-110/p/9280197.html
Copyright © 2011-2022 走看看