CodeForces - 567D 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: 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

OJ-ID:
CodeForces-567D

author:
Caution_X

date of submission:
20191031

tags:
模拟

description modelling:
给定1×N的地图,地图上有k条小船,每条小船占用空间1×a,现在输入m个数,每一个数xi表示检查第xi位置是否一定有船。输出:若第i次检查一定能够检查出船,那么输出i

major steps to solve it:
(1)第一次放:给定一个区间[1,N],设该区间最多可以放的船数x,则x*a+(x-1)=N
=> x=(N+1)/(a+1)
(2)不是第一次放:设检查第x个位置,那么我们可以放船的区间就在[1,x-1)和(x+1,N],
记sum为检查到第i步可以放的船数,那么执行了第x次检查之后sum = sum - (r-l)/(a+1) +(x-l)/(a+1) + (r-x)/(a+1),如果sum<k,那么第i步检查一定会发现船,输出i

AC code:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
using namespace std;

set<int> s;

int main()
{
    int m, n, a, k, x;
    while (scanf("%d%d%d", &n, &k, &a) != EOF) {
        scanf("%d", &m);
        s.clear();
        s.insert(0), s.insert(n+1);   // 实现虚拟长度的小技巧
        int sum = (n+1) / (a+1); 
        int ans = -1, f = 0;
        for (int i = 1; i <= m; i++) {
            scanf("%d", &x);
            set<int>::iterator it = s.upper_bound(x);
            int r = *it;
            int l = *(--it);
            sum = sum - (r-l)/(a+1) +(x-l)/(a+1) + (r-x)/(a+1);

            if (sum < k && !f) {
                ans = i;
                f = 1;
            }
            s.insert(x);
        }
        printf("%d\n", ans);
    }
    return 0;
}
扫描二维码关注公众号,回复: 7702943 查看本文章

猜你喜欢

转载自www.cnblogs.com/cautx/p/11774368.html