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图中,哪些边是必须经过的: 其他的(包括 ...
随机推荐
- JDBC入门程序总结
JDBC本质 只是一个接口 每个数据库的规范 就是实现类的接口 其实是官方 定义的一套操作所有关系型数据库的规则,就是接口,各个数据库厂商去实现这套接口,提供数据库驱动jar包, 我们可以使用这套接口 ...
- LeetCode454. 四数相加 II
题目 给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0. 分析 关键是如何想到用 ...
- linux下安装zsh和p10k的详细过程
目录 下载zsh 下载oh-my-zsh 切换shell 下载p10k 下载zsh sudo apt-get install zsh sudo apt-get install git 下载oh-my- ...
- 从零搭建一个IdentityServer——项目搭建
本篇文章是基于ASP.NET CORE 5.0以及IdentityServer4的IdentityServer搭建,为什么要从零搭建呢?IdentityServer4本身就有很多模板可以直接生成一个可 ...
- 向HDFS中指定的文件追加内容,由用户指定内容追加到原有文件的开头或结尾。
1 import java.io.FileInputStream; 2 import java.io.IOException; 3 import java.text.SimpleDateFormat; ...
- https://github.com/golang/crypto/blob/master/bcrypt/bcrypt.go
https://github.com/golang/crypto/blob/master/bcrypt/bcrypt.go
- Python_1生成器(下)之单线并行--生产着消费者模型
1 import time 2 def consumer(name): 3 print('%s准备吃包子了!' %name) 4 while True: 5 baozi = yield 6 print ...
- 主题模型(Topic)
消息队列 RocketMQ - 打造金融级消息服务 - 阿里云 https://www.aliyun.com/product/rocketmq 主题模型(Topic) 发布/订阅(Pub/Sub) 一 ...
- go语言rpc学习
rpc 就是 远程过程调用 指的是调用远端服务器上的程序的方法整个过程. rpc 理论 RPC技术在架构设计上有四部分组成,分别是:客户端.客户端存根.服务端.服务端存根. 客户端:服务调用发 ...
- vim 查找并替换多个匹配字符
通常我们在使用vim的使用需要查找文档中是否含有需要的字符 1.vim 1.txt进入文档编辑 2.输入/键,再输入需要查找的字符,或者输入?键再输入需要查找的字符 3.查找到后可以enter进去,再 ...