Codeforces #380 div2 D(729D) Sea Battle
1 second
256 megabytes
standard input
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.
The first line contains four positive integers n, a, b, k (1 ≤ n ≤ 2·105, 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.
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.
5 1 2 1
00100
2
4 2
13 3 2 3
1000000010001
2
7 11
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.
题目大意:有a个长为b的船,藏在连续的0里,求至少在0里射击枪,一定能击中至少一艘船,并输出射击的位置
思路:一开始把题想复杂了,以为要对b=1的情况进行特判,其实用一种就行。
建立结构体z,表示每一段连续的0的左端点l,右端点r,其中0的个数len,以及能放的船数num=len/b,将所有的num+起来为sum,与a比较,由于只要确保能击中就行,因此只需要攻击sum-a+1次,就一定能攻击到;对每一个z,从他的l+b-1处开始攻击(这个位置的前面不能藏一艘船),每次+b攻击,直到攻击够sum-a+1次为止。
代码:
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
#define N 200005
#define inf 1e18+5
typedef long long ll;
#define rep(i,n) for(i=0;i<n;i++)
using namespace std;
int i,j,k,m,n,t,cc,a,b,ans;
int s[N],h[N];
struct z{
int l;
int r;
int len;
int num;
}z[N];
int main()
{
while(scanf("%d%d%d%d",&n,&a,&b,&k)!=EOF){
ans=;
k=n-k;
j=;
m=;
cc=;
s[]=-;
getchar();
for(i=;i<=n;i++){
scanf("%c",&s[i]);
if(s[i]==''&&s[i-]!=''){
z[j].l=i;
}
if(s[i]==''&&s[i-]==''){
z[j].r=i-;
z[j].len=z[j].r-z[j].l+;
z[j].num=z[j].len/b;
cc+=z[j].len/b;
j++;
}
if(i==n&&s[i]==''){
z[j].r=i;
z[j].len=z[j].r-z[j].l+;
z[j].num=z[j].len/b;
cc+=z[j].len/b;
j++;
}
}
/* if(b==1){
if(k==cc){
for(i=0;i<j;i++){
for(int i1=z[i].l;i1<=z[i].r;i1++){
h[m]=i1;
m++;
}
}
ans=m;
}
else{
ans=k-a+1;
for(i=0;i<j;i++){
for(int i1=z[i].l;i1<=z[i].r;i1++){
h[m]=i1;
m++;
if(m==ans) break;
}
if(m==ans) break;
}
}
}
else */ {
/*if(a==cc){
for(i=0;i<j;i++){
if(z[i].num){
for(int i1=z[i].l+b-1;i1<=z[i].r+b-1;i1+=b){
h[m]=i1;
m++;
}
}
}
ans=m;
}
else */{
ans=cc-a+;
for(i=;i<j;i++){
if(z[i].num){
for(int i1=z[i].l+b-;i1<=z[i].r;i1+=b){
h[m]=i1;
m++;
if(m==ans) break;
}
}
if(m==ans) break;
}
}
}
// for(i=0;i<j;i++) printf("%d %d \n",z[i].l,z[i].r); printf("%d\n",ans);
for(i=;i<m;i++){
if(i!=m-) printf("%d ",h[i]);
else printf("%d\n",h[i]);
}
} return ;
}
Codeforces #380 div2 D(729D) Sea Battle的更多相关文章
- Codeforces Round #380 (Div. 2)/729D Sea Battle 思维题
Galya is playing one-dimensional Sea Battle on a 1 × n grid. In this game a ships are placed on the ...
- Codeforces 729D Sea Battle(简单思维题)
http://codeforces.com/contest/738/problem/D https://www.cnblogs.com/flipped/p/6086615.html 原 题意:海战 ...
- Codeforces #380 div2 E(729E) Subordinates
E. Subordinates time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- Codeforces #380 div2 C(729C) Road to Cinema
C. Road to Cinema time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Codeforces #380 div2 B(729B) Spotlights
B. Spotlights time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces Round #380 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 2) D. Sea Battle 模拟
D. Sea Battle time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces Round #380 (Div. 2)D. Sea Battle
D. Sea Battle time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- Codeforces 738D. Sea Battle 模拟
D. Sea Battle time limit per test: 1 second memory limit per test :256 megabytes input: standard inp ...
随机推荐
- 微软软件开发技术二十年回顾-COM、OLE、ActiveX及COM+篇
本文摘自:http://www.job168.com/info/read_100394.html 微软的许多技术,如OLE.ActiveX.以及DirectX等都是基于COM技术而建立起来的.微软本身 ...
- june 14
Thank you for your applying for employment with our company. Your application is now being processed ...
- Eclipse里面的一些常规设置
一.Eclipse里面的默认编码是GBK,但是Android开发的编码都是UTF-8,所以一定要修改自己的工程的编码,不要坑队友哦~ (1)选中当前的工程,点击右键,如图选择(这只是改变当前工程的编码 ...
- 第十周PSP
第十周PSP 工作周期:11.17-11.24 本周PSP: C类型 C内容 S开始时间 ST结束时间 I中断时间 T净时间(分) 文档 写随笔(PSP) 16:20min 16:50min 0 ...
- Jquery div展开收缩
<html> <meta http-equiv="Content-Type" content="text/html; charset=gb2312&qu ...
- javascript学习第五课this、call、apply
js函数与其它 高级语言相比有一个特点.没有返回值,一个简单函数就是function关键字+函数名字构成 this 对象是在运行中基于函数的执行环境绑定的,在全局函数中,this等于window,而当 ...
- Java Web2
JavaBean组件. 定义方法:(假设有一个JavaBean的类名为CounterBean,它有一个count属性.) //在JSP文件中分别定义4种范围内的JavaBean对象的语法 //in p ...
- MYSQL使用mysqldump导出某个表的部分数据
命令格式如下: mysqldump -u用户名 -p密码 数据库名 表名 --where="筛选条件" > 导出文件路径 例子: 从meteo数据库的sdata表中导出sen ...
- ServiceStack.OrmLite中的一些"陷阱"(2)
注:此系列不是说ServiceStack.OrmLite的多个陷阱,这仅仅个人认为是某一个陷阱(毕竟我踩坑了)而引发的思考. 前文说到了项目需要使用两种不同的数据库语言,虽说前文问题已基本解决了,但是 ...
- pd name与comment互换,或者code互换,总之互换
1 PowerDesigner中批量根据对象的name生成comment的脚本 执行方法:Open PDM -- Tools -- Execute Commands -- Run Script Opt ...