One-Dimensional Battle Ships cf567D


题目

题面

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: 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 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”.

Examples

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

大意

有k条船,每一条船都是一个1xa的矩形放在1Xn的范围内(不能相邻),bob进行m次射击,输出第几次无论船怎么排列,bob一定会击中一艘船,如果不能确定则输出‘-1’。

思路

这是一个二分答案,不断二分步数,当那个步骤时是否满足情况,最终得出答案

代码

#include<stdio.h>
#include<algorithm>
using namespace std;
int n, k, a, m;
struct point
{
    int id, distance;
}c[200010];
bool cmp(point x, point y)
{
    return x.id < y.id;
}
int solve(int x)
{
    int pr[200010] = { 0 };
    for (int i = 1; i <= x; i++)
    {
        pr[i] = c[i].distance;
    }
    pr[x + 1] = n + 1;
    sort(pr + 1, pr + x + 2);
    int t = k;
    for (int i = 1; i <= x + 1; i++)
    {
        int temp = pr[i] - pr[i - 1] - 1;
        int xx = (temp + 1) / (a + 1);
        t -= xx;
    }
    if (t > 0)
        return 0;
    else
        return 1;
}
int dimidate(int l, int r)//第l次射击时无法击中,第R次已经击中
{
    int mid = (l + r) / 2;
    if (mid == l)
    {
        int f = mid;
        if (solve(mid) == 1)
            f = -1;
        if (f == -1 && mid != 1 && solve(mid - 1) == 0)
            f = mid - 1;
        if (f == -1 && mid != m && solve(mid + 1) == 0)
            f = mid + 1;
        return f;
    }
    int f = solve(mid);
    if (f)
    {
        return dimidate(mid + 1, r);
    }
    else
    {
        return dimidate(l, mid);
    }
}
int main()
{
    //freopen("F:/y.txt", "r", stdin);
    scanf("%d %d %d", &n, &k, &a);
    scanf("%d", &m);
    for (int i = 1; i <= m; i++)
    {
        scanf("%d", &c[i].distance);
        c[i].id = i;
    }
    printf("%d", dimidate(1, m));
    return 0;
}

文章作者: xucanxx
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 xucanxx !
  目录