codeforces(567D)--D. One-Dimensional Battle Ships

来源:互联网 发布:淘宝点击率多少算正常 编辑:程序博客网 时间:2024/05/22 14:24

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

Sample test(s)
input
11 3 354 8 6 1 11
output
3
input
5 1 321 5
output
-1
input
5 1 313
output
1

题目大意:有一个1*n的长方形由n个1*1的小块组成,Alice在上面放了k个1*a的小长方形代表船,船不能相互重叠和相邻,现在Bob每次询问第i个小块那是不是有船,但是Alice都答不是,现在让你判断一下当第几次询问后,就可以判断出Alice在撒谎,如果都不能判断出,那么输出-1

对于每一次询问都相当于将一段连续的区间截成了两个小区间 ,在区间中可以计算区间的程度和区间最多可以放下多少1*a的小正方形,当没有询问时,可以放的是最多的,每当一次询问后,区间变小,都会有可能使能放的个数减少,找到最初使总数小于k的那次询问并输出。

#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <cmath>#include <algorithm>using namespace std ;#define LL __int64#define INF 0x3f3f3f3f#define PI acos(-1.0)LL cl1[800010] , cl2[800010] ;LL s[200010] ;void push_up1(LL rt) {    cl1[rt] = max(cl1[rt<<1],cl1[rt<<1|1]) ;}void push_up2(LL rt) {    cl2[rt] = min(cl2[rt<<1],cl2[rt<<1|1]) ;}void update(LL k,LL l,LL r,LL rt) {    if( l == r ) {        cl1[rt] = cl2[rt] = l ;        return ;    }    if( k <= (l+r)/2 )        update(k,l,(l+r)/2,rt<<1) ;    else        update(k,(l+r)/2+1,r,rt<<1|1) ;    push_up1(rt) ;    push_up2(rt) ;}LL query1(LL ll,LL rr,LL l,LL r,LL rt) {    if( ll > r || rr < l ) return -1 ;    if( ll <= l && rr >= r ) return cl1[rt] ;    LL t1 = query1(ll,rr,l,(l+r)/2,rt<<1) , t2 = query1(ll,rr,(l+r)/2+1,r,rt<<1|1) ;    if( t1 >= t2 ) return t1 ;    return t2 ;}LL query2(LL ll,LL rr,LL l,LL r,LL rt) {    if( ll > r || rr < l ) return INF ;    if( ll <= l && rr >= r ) return cl2[rt] ;    LL t1 = query2(ll,rr,l,(l+r)/2,rt<<1) , t2 = query2(ll,rr,(l+r)/2+1,r,rt<<1|1) ;    if( t1 <= t2 ) return t1 ;    return t2 ;}void create(LL l,LL r,LL rt) {    cl1[rt] = -1 ;    cl2[rt] = INF ;    if( l == r ) {        return ;    }    create(l,(l+r)/2,rt<<1) ;    create((l+r)/2+1,r,rt<<1|1) ;}int main() {    LL n , k , a , m , i , j , num , l , r , len ;    scanf("%I64d %I64d %I64d", &n, &k, &a) ;    create(0,n+1,1) ;    scanf("%I64d", &m) ;    for(i = 1 ; i <= m ; i++)        scanf("%I64d", &s[i]) ;    if( a*k+k-1 > n ) {        printf("0\n") ;        return 0;    }    update(0,0,n+1,1) ;    update(n+1,0,n+1,1) ;    num = (n+1)/(a+1) ;    for(i = 1 ; i <= n ; i++) {        l = query1(0,s[i],0,n+1,1) ;        r = query2(s[i],n+1,0,n+1,1) ;        if( l == -1 ) l = 0 ;        if( r == INF ) r = n+1 ;        len = r - l - 1 ;        if( len < a ) continue ;        num -= (len+1)/(a+1) ;        num += ( s[i]-l )/(a+1) ;        num += (r-s[i])/(a+1) ;        if( num < k ) break ;        update(s[i],0,n+1,1) ;    }    if( i <= n )        printf("%I64d\n", i) ;    else        printf("-1\n") ;    return 0 ;}



1 0
原创粉丝点击