当前位置: 首页 > 编程日记 > 正文

【Codeforces 738D】Sea Battle(贪心)

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;
}

相关文章:

【POJ】3617 Best Cow Line (字典序 字符串)

http://poj.org/problem?id3617 给定长度为N(1≤N≤2000)的字符串S&#xff0c;要构造一个长度为N的字符串T。期初&#xff0c;T是一个空串&#xff0c;随后反复进行下列任意操作。 从S的头部删除一个字符&#xff0c;加到T的尾部 从S的尾部删除一个字符&#xff0c;加到T的…

python数据池连接PG

发现网上都是mysql&#xff0c;后面发现PG跟mysql差不多&#xff0c;记录下来&#xff0c;怕忘了 import psycopg2 from DBUtils.PooledDB import PooledDB import psycopg2.extraspool PooledDB(psycopg2, 10,database"boatdb", user"postgres",password…

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

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

Git学习记录(一)

git-book 全面资料 git 用树形查看 &#xff08;git 命令代替gitk查看节点树part two 日常使用只要记住下图6个命令即可&#xff0c;但是学海无涯啊 ​ 常用 Git 命令清单。几个专用名词的译名如下。 Workspace&#xff1a;工作区Index / Stage&#xff1a;暂存区Reposito…

【牛客】CSL 的字符串 (stack map)

https://ac.nowcoder.com/acm/contest/551/D 这个题怎么说&#xff0c;data用来存储这个字母在字符串中最后一次出现的位置&#xff0c;vis则用来记录该字母是否在栈中。 当栈为空的时候&#xff0c;直接将s[i]放入栈中 如果不为空则需要比较栈顶元素和当前s[i]的那个元素&a…

Python:UTF-8编码转换成GBK编码

2019独角兽企业重金招聘Python工程师标准>>> #!/usr/bin/env python # -*- coding:utf-8 -*- #UTF-8转换成GBK编码 #temp #decode #encode #原理就是把UTF-8转换成万国码&#xff0c;再给万国码进行编码转换成GBK&#xff0c;在python 2.x里面这么用 ""&q…

三维重建【三】-------------------(三维重建资料收集)

Major学者的个人主页汇总&#xff1a; 1.陈宝权&#xff1a;http://web.siat.ac.cn/~baoquan/ 2.南亮亮&#xff1a;http://web.siat.ac.cn/~liangliang/ 3.mueller&#xff1a;http://people.ee.ethz.ch/~pascmu/publications.html 4.Yasutaka Furukawa:http://homes.cs.washi…

在 Linux 中查看时区

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

【HDU】1237 简单计算器 (stack)

http://acm.hdu.edu.cn/showproblem.php?pid1237 题目很好理解&#xff0c;一开始想用优先队列&#xff0c;但好像有点难实现&#xff0c;用stack比较好实现&#xff0c;遇到“ * ” 或者" / " 就进行操作&#xff0c;遇到“ - ” 就把它的相反数加进stack&#xf…

关于 synchronizeOnSession

本文为[原创]文章&#xff0c;转载请标明出处。原文链接&#xff1a;https://weyunx.com/2019/01/22...原文出自微云的技术博客 最近在维护一个老项目&#xff0c;发现了一个问题。我们新增了一个耗时较久的复杂查询的功能&#xff0c;页面采用了 ajax 异步请求数据&#xff0c…

用python操作mysql数据库(之“更新”操作)

#!/usr/bin/env python # -*- coding: utf-8 -*-import MySQLdb#建立连接 conn MySQLdb.connect(host127.0.0.1,userroot,passwd1qaz#EDC,dbtest_db) cur conn.cursor()#对数据进行操作 sql "update user set name%s where id7" #定义sql语句&#xff0c;用于修改…

OpenCV【零】—————cv::Mat——Mat对象创建方法

OpenCV (一)——Mat对象创建方法 目录 OpenCV (一)——Mat对象创建方法 1. cv::Mat优点及原理&#xff08;本质类&#xff09; 2. Mat类拷贝及对象的创建方法 3. Mat 对象元素的高效访问 4. 存储方法 5. 显式创建Mat对象 6. 与其他语言对比的方式 7. Mat操作实例 1. c…

在Vmware中安装Ubuntu

转载自&#xff1a;https://blog.csdn.net/stpeace/article/details/78598333 不是每一个程序员都必须玩过linux&#xff0c;只是博主觉得现在的很多服务器都是linux系统的&#xff0c;而自己属于那种前端也搞&#xff0c;后台也搞&#xff0c;对框架搭建也感兴趣&#xff0c;…

C++回声服务器_5-多进程版本

服务器和客户端都是用多进程来接收和发送数据。 服务器代码 #include <cstdio> #include <cstdlib> #include <cstring> #include <unistd.h> #include <csignal> #include <sys/wait.h> #include <arpa/inet.h> #include <sys/s…

OpenCV 【一】—— OpenCV中数组指针、图像分块计算、指针取像素值与MatToEigen方法,内存对齐

{ Topic1: 高效开辟内存&#xff0c;使适用于大型数组。//开辟新数组&#xff0c;或者开辟新的0或者某一数值的数组/Mat或者Map直接使用memset //大数组操作效率较高 举例1&#xff1a;cv::Mat cv_ncc_temp(cv_input_32f.rows, cv_input_32f.cols, CV_8UC1);memset(cv_ncc_temp…

【Java】类与对象 - 参数传值

目录 面向过程语言简介 面向对象语言简介 面向对象编程的三个特性 参数传值 传值机制 基本数据类型的参数的传值 引用类型参数的传值 可变参数 面向过程语言简介 核心是编写解决问题某个问题的代码块&#xff0c;代码块是程序执行时产生的一种行为。面向过程语言缺少一…

新闻发布项目——业务逻辑层(newsTbService)

package bdqn.newsManageServlet.Service;import java.util.List;import bdqn.newsManageServlet.entity.newsTb;/*** 新闻业务逻辑层的接口* author Administrator**/ public interface newsTbService {//分页查询public List<newsTb>getPagingNews(int pagesize,int pa…

使用阿里云发布分布式网站,开发时候应该注意什么?

虽然之前写过关于负载均衡的文章&#xff0c;但是似乎大家都对负载均衡这个标题很陌生。今天就换个角度&#xff0c;从分布式网站发布角度说一下 首先&#xff0c;网站发布一定离不开服务器&#xff0c;就是阿里云的云服务器ECS。最近发现&#xff0c;老用户也有机会购买特价服…

【Java】类与对象 - 对象的组合

一个类的成员变量可以是Java允许的任何数据类型&#xff0c;因此&#xff0c;一个类可以把某个对象作为自己的成员变量&#xff0c;也就是说&#xff0c;该对象将其他对象作为自己的组成部分。 组合和复用 如果一个对象a组合了对象b&#xff0c;那么对象a就可以委托对象b调用…

CMakeLists.txt学习记录

一、Cmake 学习地址与作用 cmake详细见&#xff1a;https://gitlab.kitware.com/cmake/community/-/wikis/home 是一个跨平台、开源的构建系统。它是一个集软件构建、测试、打包于一身的软件。它使用与平台和编译器独立的配置文件来对软件编译过程进行控制。 二、常用命令 …

20145223《信息安全系统设计》 实验四 驱动程序设计

20145223杨梦云《信息安全系统设计》实验四实验报告 一、配置开发环境&#xff08;同实验一&#xff09; 二、阅读和理解源代码 进入/arm2410cl/exp/drivers/01_demo&#xff0c;使用vi编辑器或其他编辑器阅读理解源代码。 三、编译驱动模块及测试程序 上面介绍了在 Makefile 中…

Android屏幕适配框架-(今日头条终极适配方案)

2019独角兽企业重金招聘Python工程师标准>>> 在Android开发中,屏幕适配是一个非常头痛的问题,因而为了去进行屏幕适配,作为程序员,是呕心沥血,历经磨难,哈哈 我们之前做屏幕适配一般都会用到一下两种方式: 我们之前做屏幕适配一般都会用到一下两种方式: 第一种就是宽…

OpenCV 【三】————contours便利删除操作方法

int cmin 100;int cmax 1000;vector<vector<Point>>::const_iterator itc contours.begin();while (itc ! contours.end()){if (itc->size() < cmin || itc->size() > cmax)itc contours.erase(itc);elseitc;}

解决myeclipse中新建javaweb工程,无法使用Web App Libraries问题

在myeclipse中新建的java web工程&#xff0c;lib中的jar包无法自动加载工程&#xff0c;不能像eclipse那样使用Web App Libraries。即使添加了Web App Libraries这个libraries&#xff0c;jar包还是如法加入。解决方法&#xff1a;在.project文件中&#xff0c;修改<nature…

企业分布式微服务云SpringCloud SpringBoot mybatis - 服务消费者(Feign)

一、Feign简介 Feign是一个声明式的伪Http客户端&#xff0c;它使得写Http客户端变得更简单。使用Feign&#xff0c;只需要创建一个接口并注解。它具有可插拔的注解特性&#xff0c;可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon&…

OpenCV 【四】————Watershed Algorithm(图像分割)——分水岭算法的原理及实现

分水岭算法实现&#xff08;C、opencv&#xff09; 1.作用&#xff1a; 通常用于分割图像&#xff0c;主要实现以临近像素间的相似性作为重要的参考依据&#xff0c;从而将在空间位置上相近并且灰度值相近的像素点互相连接起来构成一个封闭的轮廓&#xff0c;封闭性是分水岭算…

SQL脚本--有关压缩数据库日志

/*--压缩数据库的通用存储过程 压缩日志及数据库文件大小 因为要对数据库进行分离处理 所以存储过程不能创建在被压缩的数据库中 --邹建 2004.03(引用请保留此信息)--*/ /*--调用示例 exec p_compdb test--*/ use master --注意,此存储过程要建在master数据库中Go if exists …

【POJ】1308 Is It A Tree?((并查集 + set)or (map))

http://poj.org/problem?id1308 这个题数组开到200就可以了&#xff0c;但题目中貌似没有说呢&#xff1f; 读入每一对顶点&#xff0c;看看他们是否在同一个集合中&#xff0c;如果是的话&#xff0c;肯定成环&#xff0c;不是一棵树。 用set容器保存节点&#xff0c;最后…

Java程序员修炼之路(一)我们为什么选择Java

我们为什么选择Java大多数人选择Java可能只是因为听说Java前景好、Java比较好找工作、Java语言在TIOBE排行榜上一直位于前三等等之类的原因&#xff0c;但是Java具体好在哪里&#xff0c;心里却是没有什么概念的。其实我选择Java也是出于以上的原因&#xff0c;但是现在确实真正…

iOS10 权限崩溃问题

iOS10 权限崩溃问题 今天 手机升级了 iOS10然后用正在开发的项目 装了个ipa包&#xff0c;发现点击有关 权限访问 直接Crash了&#xff0c;并在控制台输出了一些信息&#xff1a; This app has crashed because it attempted to access privacy-sensitive data without a usage…