Codeforces Round #Pi (Div. 2) D. One-Dimensional Battle Ships
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.
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.
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".
11 3 3
5
4 8 6 1 11
3
5 1 3
2
1 5
-1
5 1 3
1
3
1
这题可以先算出刚开始最大能摆放的木板数,用set维护起点和终点的idx(坐标)和num(即当前空位和下一个空位间最大能放的木板数),用sum记录总共能放的木板数。每次更新一个坐标a,找到和当前要更新的点最近的右边一点坐标b,然后更新当前点a的num和这一点左边b-1的num,然后重新调整sum,如果sum<k,那么这点就是答案,否则把a放入set里.
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define maxn 200050
struct node{
int num,idx;
}a,temp1,temp2,b1,b2;
int b[maxn];
bool operator <(node a,node b){
return a.idx<b.idx;
}
set<node>myset;
set<node>::iterator it;
int shumu(int l,int m)
{
int i,j,num,sheng;
num=l/(m+1);
sheng=l%(m+1);
if(sheng==m)num++;
return num;
}
int main()
{
int n,m,k,i,j,h,num,flag,cnt;
while(scanf("%d%d%d",&n,&k,&m)!=EOF)
{
myset.clear();
num=shumu(n,m);
a.idx=0;a.num=num;
myset.insert(a);
a.idx=n+1;a.num=0;
myset.insert(a);
scanf("%d",&h);
for(i=1;i<=h;i++){
scanf("%d",&b[i]);
}
flag=1;cnt=0;
for(i=1;i<=h;i++){
a.idx=b[i];a.num=0;
it=myset.lower_bound(a);
temp2=*it;
it--;
temp1=*it;
num-=temp1.num;
b1.idx=temp1.idx;b1.num=shumu(b[i]-temp1.idx-1,m);
b2.idx=b[i];b2.num=shumu(temp2.idx-b[i]-1,m);
num+=b1.num+b2.num;
if(num<k){
flag=0;cnt=i;break;
}
myset.erase(temp1);
myset.insert(b1);
myset.insert(b2);
}
if(flag==0){
printf("%d\n",i);continue;
}
else printf("-1\n");
}
return 0;
}
Codeforces Round #Pi (Div. 2) D. One-Dimensional Battle Ships的更多相关文章
- map Codeforces Round #Pi (Div. 2) C. Geometric Progression
题目传送门 /* 题意:问选出3个数成等比数列有多少种选法 map:c1记录是第二个数或第三个数的选法,c2表示所有数字出现的次数.别人的代码很短,思维巧妙 */ /***************** ...
- 构造 Codeforces Round #Pi (Div. 2) B. Berland National Library
题目传送门 /* 题意:给出一系列读者出行的记录,+表示一个读者进入,-表示一个读者离开,可能之前已经有读者在图书馆 构造:now记录当前图书馆人数,sz记录最小的容量,in数组标记进去的读者,分情况 ...
- Codeforces Round #Pi (Div. 2) D. One-Dimensional Battle Ships set乱搞
D. One-Dimensional Battle ShipsTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/con ...
- Codeforces Round #Pi (Div. 2) D. One-Dimensional Battle Ships set区间分解
D. One-Dimensional Battle ShipsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/co ...
- Codeforces Round #Pi (Div. 2)(A,B,C,D)
A题: 题目地址:Lineland Mail #include <stdio.h> #include <math.h> #include <string.h> #i ...
- codeforces Round #Pi (div.2) 567ABCD
567A Lineland Mail题意:一些城市在一个x轴上,他们之间非常喜欢写信交流.送信的费用就是两个城市之间的距离,问每个城市写一封信给其它城市所花费的最小费用和最大的费用. 没什么好说的.直 ...
- Codeforces Round #Pi (Div. 2) C. Geometric Progression map
C. Geometric Progression Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...
- Codeforces Round #Pi (Div. 2) B. Berland National Library set
B. Berland National LibraryTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...
- Codeforces Round #Pi (Div. 2) A. Lineland Mail 水
A. Lineland MailTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/567/proble ...
- Codeforces Round #Pi (Div. 2) E. President and Roads 最短路+桥
题目链接: http://codeforces.com/contest/567/problem/E 题意: 给你一个带重边的图,求三类边: 在最短路构成的DAG图中,哪些边是必须经过的: 其他的(包括 ...
随机推荐
- 【ORA】ORA-01033,ORA-09968,ORA-01102
[oracle@oracle ~]$ imp xxxx/user file=/usr/local/src/666.dmp full=y buffer=40960000 Import: Release ...
- JavaScript中的事件委托机制跟深浅拷贝
今天聊下JavaScript中的事件委托跟深浅拷贝 事件委托 首先呢,介绍一下事件绑定 //方法一:通过onclick <button onclick="clickEvent()&qu ...
- CTFshow萌新赛-千字文
打开靶机 下载完成后,为一张二维码图片 使用StegSolve 解出隐写图像 保存后使用PS或其他工具去除白边 然后使用脚本分割这个图像(25*25) from PIL import Image im ...
- LuoguP5748 集合划分计数
题意 一个有\(n\)个元素的集合,将其分为任意个非空子集,求方案数.集合之间是无序的,\(\{\{1,2\},\{3\}\}=\{\{3\},\{1,2\}\}\). 设\(f_n\)表示用\(n\ ...
- 干电池升压IC
1, 干电池升压IC 升压输出3V,3,3V,5V等3V-5V可调 2, 单节锂电池升压IC 升压输出4.2 ...
- numpy模块(详解)
重点 索引和切片 级联 聚合操作 统计操作 矩阵 什么是数据分析 是把隐藏在一些看似杂乱无章的数据背后的信息提炼出来,总结出所研究对象的内在规律 数据分析是用适当的方法对收集来的大量数据进行分析,帮助 ...
- JVM(七)字符串详解
常量池: 我们前面也一直说常量池有三种: 1:class文件中的常量池,前面我们解析class文件的时候解析的就是,这是静态常量池.在硬盘上. 2:运行时常量池.可以通过HSDB查看,是Instan ...
- Redis持久化之父子进程与写时复制
之所以将Linux底层的写时复制技术放在Redis篇幅下,是因为Redis进行RDB持久化时,BGSAVE(后面称之为"后台保存")会开辟一个子进程,将数据从内存写进磁盘,这儿我产 ...
- Django Admin后台添加用户时出现报错:1452
如果在使用Django Admin后台添加用户时出现报错: (1452, 'Cannot add or update a child row: a foreign key constraint fai ...
- LOJ10019生日蛋糕
Mr.W 要制作一个体积为 N*π 的 M 层生日蛋糕,每层都是一个圆柱体. 设从下往上数第 i 蛋糕是半径为 R_i,高度为 H_i 的圆柱.当 i<M 时,要求 R_i>R_{i+1} ...