【CodeForces】567D - One-Dimensional Battle Ships & 【51Nod】1521 - 一维战舰(STL - set & 二分)

来源:互联网 发布:游族网络最新消息预测 编辑:程序博客网 时间:2024/05/22 01:26

51Nod链接:点击打开链接

CodeForces链接:点击打开链接

1521 一维战舰
题目来源: CodeForces
基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题
 收藏
 关注

爱丽丝和鲍博喜欢玩一维战舰的游戏。他们在一行有n个方格的纸上玩这个游戏(也就是1×n的表格)。

在游戏开始的时候,爱丽丝放k个战舰在这个表格中,并不把具体位置告诉鲍博。每一只战舰的形状是 1×a 的长方形(也就是说,战舰会占据a个连续的方格)。这些战舰不能相互重叠,也不能相接触。

然后鲍博会做一系列的点名。当他点到某个格子的时候,爱丽丝会告诉他那个格子是否被某只战舰占据。如果是,就说hit,否则就说miss。

但是这儿有一个问题!爱丽丝喜欢撒谎。他每次都会告诉鲍博miss。

请你帮助鲍博证明爱丽丝撒谎了,请找出哪一步之后爱丽丝肯定撒谎了。


Input
单组测试数据。第一行有三个整数n,k和a(1≤n,k,a≤2*10^5),表示表格的大小,战舰的数目,还有战舰的大小。输入的n,k,a保证是能够在1×n的表格中放入k只大小为a的战舰,并且他们之间不重叠也不接触。第二行是一个整数m(1≤m≤n),表示鲍博的点名次数。第三行有m个不同的整数x1,x2,...,xm,xi是鲍博第i次点名的格子编号。格子从左到右按照1到n编号。
Output
输出一个整数,表示最早一次能够证明爱丽丝一定撒谎的点名编号。如果不能证明,输出-1。点名的编号依次从1到m编号。
Input示例
样例111 3 354 8 6 1 11样例25 1 321 5
Output示例
样例输出13样例输出2-1

D. 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: nk 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 nk 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 from1 to m in the order the were made. If the sought move doesn't exist, then print "-1".

Examples
input
11 3 354 8 6 1 11
output
3
input
5 1 321 5
output
-1
input
5 1 313
output
1


前面如果没有确定是撒谎,那么就按这个格子没有战舰处理,不能当数学题来单独处理每一次询问。


代码如下:

#include <cstdio>#include <cstring>#include <queue>#include <stack>#include <set>#include <algorithm>using namespace std;#define INF 0x3f3f3f3f#define CLR(a,b) memset(a,b,sizeof(a))#define LL long longint num[200000+5];int main(){int n,k,a;scanf ("%d %d %d",&n,&k,&a);int m;scanf ("%d",&m);for (int i = 1 ; i <= m ; i++)scanf ("%d",&num[i]);int ans = -1;int ant = (n+1) / (a+1);//可以放战舰的数量 set<int> d;d.insert(0);d.insert(n+1);//加入上下界 set<int>::iterator up,down;for (int i = 1 ; i <= n ; i++){up = d.upper_bound(num[i]);down = --up;up++;ant = ant - (*up - *down) / (a+1) + (num[i] - *down) / (a+1) + (*up - num[i]) / (a+1);d.insert(num[i]);if (ant < k){ans = i;break;}}printf ("%d\n",ans);return 0;}




0 0
原创粉丝点击