【CodeForces】 729D Sea Battle

传送门

题目描述

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.

题目翻译

代码

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 char s[200050];
 7 int p[200050];
 8 int n,a,b,k,cnt=0;
 9 int main(){
10     scanf("%d%d%d%d",&n,&a,&b,&k);
11     scanf("%s",s);
12     int sz=strlen(s);
13     for(register int i=0;i<sz;i++){
14         if(s[i]=='1')p[++cnt]=i+1;
15     }
16     p[0]=0,p[++cnt]=sz+1;
17     int ans=0;
18     int cn=0;
19     for(register int i=0;i<cnt;i++){
20         int cha=p[i+1]-p[i]-1;
21         ans+=cha/b;
22     }
23     ans-=(a-1);
24     cout<<ans<<endl;
25     for(register int i=0;i<cnt;i++){
26         for(register int j=p[i]+b;j<p[i+1];j+=b){
27             if(cn==ans){cout<<endl;return 0;}
28             printf("%d ",j);
29             cn++;
30         }
31     }
32 }

猜你喜欢

转载自www.cnblogs.com/Fang-Hao/p/9091925.html