【Codeforces 738D】Sea Battle(贪心)

news/2024/7/9 3:27:46

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

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.

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 to n, 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.

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

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

代码:

#include<cstdio>
#define N 200005
int a,b,n,k,d,ans,p[N];
char s[N];
int main(){scanf("%d%d%d%d%s",&n,&a,&b,&k,s);for(int i=0;s[i];i++){if(s[i]=='0')d++;if(s[i]=='1')d=0;if(d==b){d=0;ans++;p[ans]=i+1;}}ans-=a-1;printf("%d\n",ans);for(int i=1;i<=ans;i++)printf("%d ",p[i]);return 0;
}

 

  


http://lihuaxi.xjx100.cn/news/242014.html

相关文章

MySQL 的性能(下篇)—— 性能优化方法

简介 文中内容均为阅读前辈的文章所整理而来&#xff0c;参考文章已在最后全指明 本文分为上下两篇&#xff1a; 上篇&#xff1a;MySQL 的 SQL 执行分析下篇&#xff1a;MySQL 性能优化下面为下篇内容&#xff0c;分为以下部分&#xff1a; 一、创建表时的性能优化二、设计表时…

Python培训班一般学费是多少?

Python培训机构在市场上有很多&#xff0c;很多想要学习Python技术的人对于培训机构的选择都比较困扰&#xff0c;尤其是学费这方面&#xff0c;想要知道Python培训班一般学费是多少?那么来看看下面的详细介绍吧。 Python培训班一般学费是多少?目前IT培训机构Python人工智能培…

ASP.NET WebAPi之断点续传下载(下)

前言 上一篇我们穿插了C#的内容&#xff0c;本篇我们继续来讲讲webapi中断点续传的其他情况以及利用webclient来实现断点续传&#xff0c;至此关于webapi断点续传下载以及上传内容都已经全部完结&#xff0c;一直嚷嚷着把SQL Server和Oracle数据库再重新过一遍&#xff0c;这篇…

Python图片处理PIL/pillow/生成验证码/出现KeyError: 和The _imagingft C module is not installed...

近期在用Python开发自己的博客。须要用到Python生成验证码&#xff0c;当然肯定要用到Python的图形处理库PIL&#xff0c;由于我用的是windows。 所以在安装好pil之后就開始写&#xff0c;就依照题目所说出现了The _imagingft C module is not installed 错误&#xff0c;找了非…

UI设计培训分享:ui设计师如何培养设计思维?

作为一名UI设计师&#xff0c;工作上经常会遇到思维碰撞的问题&#xff0c;培养自己的设计思维是大部分UI设计师都需要的&#xff0c;那么ui设计师如何培养设计思维呢?来看看下面的详细介绍。 UI设计培训分享&#xff1a;ui设计师如何培养设计思维? 一、因舍而得 正如我们看到…

在 Linux 中查看时区

1.date date "%Z %z"或者 date -R2.timedatectl timedatectl|grep "Timezone"可以使用 timedatectl 来设置 Linux 时区 3.显示文件 /etc/timezone 的内容 cat /etc/timezone