Codeforces Round #Pi (Div. 2) D. One-Dimensional Battle Ships
2015-08-10 19:22
495 查看
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
output
input
output
input
output
1
这题可以先算出刚开始最大能摆放的木板数,用set维护起点和终点的idx(坐标)和num(即当前空位和下一个空位间最大能放的木板数),用sum记录总共能放的木板数。每次更新一个坐标a,找到和当前要更新的点最近的右边一点坐标b,然后更新当前点a的num和这一点左边b-1的num,然后重新调整sum,如果sum<k,那么这点就是答案,否则把a放入set里.
(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 32
1 5
output
-1
input
5 1 31
3
output
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; }
相关文章推荐
- 阿里巴巴、腾讯、百度的面试问题笔知识汇总(两)
- 推荐他们认为有用Sublime Text3小工具
- UVA 10986 Sending email SPFA
- 数据结构(8)平衡查找树之红黑树
- Android studio 使用心得(八)----测试程序单元测试
- iOS项目代码行数统计
- Python多线程爬取知乎获赞过千的答案链接
- 浅谈setInterval和setTimeout的区别
- Linked List Cycle II
- android 控制手机的体积的大小 切换音效模式
- 详解c++构造函数
- 线段树 fzu2187 回家种地
- 数据结构(7)平衡查找树之2-3树
- Spring异步任务处理,@Async的配置和使用
- 数据结构(6)二叉树
- C++面向对象之浅谈
- 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息--3.1 Index用户列表]
- 关于list.extend(iterable)
- UrlReWriter的使用
- R 安装xtsExtra包报错