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.

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 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

这题可以先算出刚开始最大能摆放的木板数,用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;
}


posted @ 2015-08-10 19:22  Herumw  阅读(88)  评论(0编辑  收藏  举报