Codeforce 567D
One-Dimensional Battle Ships
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Alice and Bob love playing one-dimensional battle ships. They play on the field in the form of a line consisting of n square cells (that is, on a 1 × n table).
At the beginning of the game Alice puts k ships on the field without telling their positions to Bob. Each ship looks as a 1 × a rectangle (that is, it occupies a sequence of a consecutive squares of the field). The ships cannot intersect and even touch each other.
After that Bob makes a sequence of “shots”. He names cells of the field and Alice either says that the cell is empty (“miss”), or that the cell belongs to some ship (“hit”).
But here’s the problem! Alice like to cheat. May be that is why she responds to each Bob’s move with a “miss”.
Help Bob catch Alice cheating — find Bob’s first move, such that after it you can be sure that Alice cheated.
Input
The first line of the input contains three integers: n, k and a (1 ≤ n, k, a ≤ 2·105) — the size of the field, the number of the ships and the size of each ship. It is guaranteed that the n, k and a are such that you can put k ships of size a on the field, so that no two ships intersect or touch each other.
The second line contains integer m (1 ≤ m ≤ n) — the number of Bob’s moves.
The third line contains m distinct integers x1, x2, …, xm, where xi is the number of the cell where Bob made the i-th shot. The cells are numbered from left to right from 1 to n.
Output
Print a single integer — the number of such Bob’s first move, after which you can be sure that Alice lied. Bob’s moves are numbered from 1 to m in the order the were made. If the sought move doesn’t exist, then print “-1”.
Sample test(s)
Input
11 3 3
5
4 8 6 1 11
Output
3
Input
5 1 3
2
1 5
Output
-1
Input
5 1 3
1
3
Output
1
题意:大意是给你k个长度为a的船,以及一个长度为n的地方,船不能相邻或相交,再给你m个箭,,这个箭射的位置不能有船,输出到哪个位置一定会有船,否则输出-1.
二分答案,每次2分箭射的地方,找到上界和下界
#include <iostream>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
typedef long long LL;
const int Max = 1e5+100;
int n,k,a,m;
int boat[2*Max];
bool Judge(int x)
{
int len=0;
int ans=0;
for(int i=1;i<=n;i++)
{
len++;
if(boat[i]&&boat[i]<=x)
{
len=0;
}
if(len>=a)
{
len=-1;
ans++;
}
if(ans>=k)
{
return false;
}
}
return true;
}
int main()
{
scanf("%d %d %d %d",&n,&k,&a,&m);
int data;
for(int i=1;i<=m;i++)
{
scanf("%d",&data);
boat[data]=i;
}
int L=1,R=m;
int ans=-1;
while(L<=R)
{
int mid=(L+R)>>1;
if(Judge(mid))
{
ans=mid;
R=mid-1;
}
else
{
L=mid+1;
}
}
printf("%d\n",ans);
return 0;
}
Codeforce 567D的更多相关文章
- Codeforce - Street Lamps
Bahosain is walking in a street of N blocks. Each block is either empty or has one lamp. If there is ...
- Codeforce Round #216 Div2
e,还是写一下这次的codeforce吧...庆祝这个月的开始,看自己有能,b到什么样! cf的第二题,脑抽的交了错两次后过了pretest然后system的挂了..脑子里还有自己要挂的感觉,果然回头 ...
- Codeforce 水题报告(2)
又水了一发Codeforce ,这次继续发发题解顺便给自己PKUSC攒攒人品吧 CodeForces 438C:The Child and Polygon: 描述:给出一个多边形,求三角剖分的方案数( ...
- codeforce 375_2_b_c
codeforce 375_2 标签: 水题 好久没有打代码,竟然一场比赛两次卡在边界条件上....跪 b.题意很简单...纯模拟就可以了,开始忘记了当字符串结束的时候也要更新两个值,所以就错了 #i ...
- codeforce 367dev2_c dp
codeforce 367dev2_c dp 标签: dp 题意: 你可以通过反转任意字符串,使得所给的所有字符串排列顺序为字典序,每次反转都有一定的代价,问你最小的代价 题解:水水的dp...仔细想 ...
- 三维dp&codeforce 369_2_C
三维dp&codeforce 369_2_C 标签: dp codeforce 369_2_C 题意: 一排树,初始的时候有的有颜色,有的没有颜色,现在给没有颜色的树染色,给出n课树,用m种燃 ...
- 强连通分量&hdu_1269&Codeforce 369D
强连通分量 标签: 图论 算法介绍 还记得割点割边算法吗.回顾一下,tarjan算法,dfs过程中记录当前点的时间戳,并通过它的子节点的low值更新它的low,low值是这个点不通过它的父亲节点最远可 ...
- 【树状数组】区间出现偶数次数的异或和(区间不同数的异或和)@ codeforce 703 D
[树状数组]区间出现偶数次数的异或和(区间不同数的异或和)@ codeforce 703 D PROBLEM 题目描述 初始给定n个卡片拍成一排,其中第i个卡片上的数为x[i]. 有q个询问,每次询问 ...
- 解题报告:codeforce 7C Line
codeforce 7C C. Line time limit per test1 second memory limit per test256 megabytes A line on the pl ...
随机推荐
- 导出查询结果到excle
实现功能 输入查询结果 点击导出查询结果 导出到excle表.
- jquery ui bootstrap日期插件
http://blog.csdn.net/php_897721669/article/details/7404527 搜索“jquery ui日期插件怎么显示年份”? $("#datepic ...
- ubuntu安装Python环境以及科学计算环境
参考:http://blog.csdn.net/a1311543690/article/details/ 1.sudo apt-get install python-pip pip是Python的一个 ...
- Web前端开发基础 第三课(与浏览者交互)
来自慕课网,整理 语法: <form method="传送方式" action="服务器文件"> 讲解: 1.<form> :<f ...
- String的hashcode(java)
hashCode就是我们所说的散列码,使用hashCode算法可以帮助我们进行高效率的查找,例如HashMap,说hashCode之前,先来看看Object类. Java程序中所有类的直接或间接父类, ...
- 建造者模式(Builder)
GOF:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 类图:
- IOS第18天(6,CAKeyframeAnimation关键帧动画)
******* #import "HMViewController.h" @interface HMViewController () @property (weak, nonat ...
- php如何遍历多维的stdClass Object 对象,php的转换成数组的函数只能转换外面一丛数组
php如何遍历多维的stdClass Object 对象,php的转换成数组的函数只能转换外面一丛数组 (2012-09-10 19:58:49) 标签: 杂谈 分类: 网页基础知识 php如何遍历多 ...
- NGUI 渲染流程深入研究 (UIDrawCall UIGeometry UIPanel UIWidget)
上图是一个简要的NGUI的图形工作流程,UIGeometry被UIWidget实例化之后,通过UIWidget的子类,也就是UISprit,UILabel等,在OnFill()函数里算出所需的Geom ...
- Nginx日志切割,以及脚本上传nginx的切割日志
一:日志切割步骤 命令都在root下进行 1.创建目录 mkdir -p /etc/opt/modules/bin ## 创建文件夹 2.上传cut 3.观察目录 4.修改的cut文件 5.检测 需要 ...