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

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2025-01-12 15:48:21

Codeforces Round #Pi (Div. 2) D. One-Dimensional Battle Ships的相关文章

map Codeforces Round #Pi (Div. 2) C. Geometric Progression

题目传送门 1 /* 2 题意:问选出3个数成等比数列有多少种选法 3 map:c1记录是第二个数或第三个数的选法,c2表示所有数字出现的次数.别人的代码很短,思维巧妙 4 */ 5 /************************************************ 6 * Author :Running_Time 7 * Created Time :2015-8-6 1:07:18 8 * File Name :C.cpp 9 *************************

构造 Codeforces Round #Pi (Div. 2) B. Berland National Library

题目传送门 1 /* 2 题意:给出一系列读者出行的记录,+表示一个读者进入,-表示一个读者离开,可能之前已经有读者在图书馆 3 构造:now记录当前图书馆人数,sz记录最小的容量,in数组标记进去的读者,分情况讨论一下 4 */ 5 /************************************************ 6 * Author :Running_Time 7 * Created Time :2015-8-6 0:23:37 8 * File Name :B.cpp 9

Codeforces Round #Pi (Div. 2) (STL专场)

Codeforces Round #Pi (Div. 2) A - Lineland Mail 水题,拼手速. /* * @author Novicer * language : C++/C */ #include<iostream> #include<sstream> #include<fstream> #include<vector> #include<list> #include<deque> #include<queue

Codeforces Round #Pi (Div. 2) (ABCD题解)

比赛链接:http://codeforces.com/contest/567 听说Round #Pi的意思是Round #314... A. Lineland Mail time limit per test:3 seconds memory limit per test:256 megabytes All cities of Lineland are located on the Ox coordinate axis. Thus, each city is associated with it

Codeforces Round #Pi (Div. 2)(A,B,C,D)

A题: 题目地址:Lineland Mail #include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <sstream> #include <algorithm> #include <set> #include <queue> #include

Codeforces Round #Pi (Div. 2) E. President and Roads (最短路+强连通求割边)

题目地址:codeforces #pi (DIV2) E 题目很水..就是先求两边最短路,然后把可能为最短路的边挑出来,然后判断是否yes只需要转化成无向图跑一遍tarjan,找出割边,割边就是yes,然后剩下的边就让它的值为最短路-1就行了,如果-1后变成了非正数,就是no. 但是!!!居然卡spfa!!那是不是说cf以后就不能用可以卡的算法了..完全可以出组数据来卡这些算法...比如spfa,isap... 于是为了这题,又看了一遍迪杰斯特拉算法.. 代码如下: #include <cstd

Codeforces Round #Pi (Div. 2) B Berland National Library

思路:对于这道题,一开始并没有什么思路,但后来想了下,其实就是维护一个集合,每一个人都是不同的元素,满足了集合的互异性,而要求这个图书馆最小的容纳量,其实就是求这个集合的最大的元素量,假设在某个时刻集合里存在M个元素,是集合从开始到结束出现过的元素个数的最大值,那么就是这个图书馆的最小容纳量,如果最小容纳量比M小,那怎么容得下M个人?对于+,   他之前肯定是没进或者之前出来股,无论怎样,都要加进集合. 对于一个集合的操作,在cf种时间为上的比赛,自然选用stl的set,如果各位有更好的思路或实

Codeforces Round #Pi (Div. 2) —— C-Geometric Progression

题意: 现在有n个数,然后给出一个数k(代表的是等比数列中的那个公比),然后第二行给出n个数,代表的是这个序列. 最后的问题是叫你找出在这个序列中满足公比为k的三个数有几种,并输出方案总数. 思路: 这道题很巧妙啊,用到了map. 首先我们先记录下每种数出现过了几次,这里因为数太大了,直接用数组存会爆掉,所以改用map. 我们需要两个map,分别记为mp1,mp2. 然后在for的过程中,我们是以当前的那个数为第二项,然后去寻找它的mp1[a[i]*k](也就是第三项),寻找它的mp2[a[i]

Codeforces Round #Pi (Div. 2) —— D One-Dimensional Battle Ships

题目的意思是: 现在有一个长度为n,宽为1的方格,在上面可以放大小为1*a船,然后输入为n,k,a:分别为平地的大小,船的数量,船的长度. 一个叫alice的人已经在地图上摆好了船的位置. 然后bob总共可以有m次攻击的机会,然后他每次攻击的点为xi,但是alice并不会告诉它有没有打中(也就是说每次都认为他是miss的),问你,bob可以在第几次攻击的时候推测出alice在撒谎,如果推测不出来则输出-1. 这里每两条相邻的船不能相互接壤. 思路: 用set来维护每一段的区间. 首先,我们最多可

Codeforces Round #Pi (Div. 2)——set——Berland National Library

Berland National Library has recently been built in the capital of Berland. In addition, in the library you can take any of the collected works of Berland leaders, the library has a reading room. Today was the pilot launch of an automated reading roo