Codeforences Round #PI(div2) D. One-Dimensional Battle Ships

来源:互联网 发布:微信 为知笔记 编辑:程序博客网 时间:2024/05/28 17:07
Alice and Bob love playing one-dimensional battle ships. They play on the field in the form of a line consisting ofn 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 a1 × a rectangle (that is, it occupies a sequence ofa 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 putk 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 integersx1, x2, ..., xm, wherexi is the number of the cell where Bob made thei-th shot. The cells are numbered from left to right from1 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

【题意】

对于lie 的判断应该基于能放的船的个数,能放的船的个数是随着射的点数的增加而减少的,射完每个点后更新能放的船的个数,如果这个时候已经无法放下k条船了,说明lie了,如果所有都射完也没发生,那么就-1
【解题思路】
由于船与串不能相邻,除了最后一条船,每条船实际占的size 应该为a+1,那么很容易知道对于长度为l的区间,能放的船的个数为(l+1)/(a+1),这是初始能放的船的个数,为最大值,当射了点b之后,破坏的是b所在的一段最大的没有被射过点的区间的连续性,做法是找到距离b点最近的左端和右端的被射过的点,可以用set 搞,找的时upper_bound,记得初始化的时候把 0点和 n+1 点当成射过的.
.【AC代码】
#include <bits/stdc++.h>using namespace std;int n,k,a,m,b;set<int>s;set<int>::iterator it;int solve(int x,int y)//计算射了b之后减少的房船的个数{    return (y-x)/(a+1)-(y-b)/(a+1)-(b-x)/(a+1);}int main(){    ios::sync_with_stdio();    cin.tie(0);    cin>>n>>k>>a;    cin>>m;    int sum = (n+1)/(a+1);//当前能放船的个数//    cout<<sum<<endl;    s.clear();    s.insert(0);    s.insert(n+1);    int ans = -1;    bool flag = false;    for(int i=1; i<=m; i++)    {        cin>>b;        if(flag)continue;        it = s.upper_bound(b);        int x = *it;        int y = *(--it);        sum -= solve(y,x);        if(sum<k)        {            ans = i;            flag=true;        }        s.insert(b);    }    cout<<ans<<endl;    return 0;}


1 0
原创粉丝点击