CodeForces - 567D One-Dimensional Battle Ships

E - One-Dimensional Battle Ships

CodeForces - 567D

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 354 8 6 1 11

Output

3

Input

5 1 321 5

Output

-1

Input

5 1 313

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

原文地址:https://www.cnblogs.com/cautx/p/11774368.html

时间: 2024-07-31 19:03:44

CodeForces - 567D One-Dimensional Battle Ships的相关文章

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 positio

hdu 5093 Battle ships 最大二分匹配

Battle ships Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 589    Accepted Submission(s): 233 Problem Description Dear contestant, now you are an excellent navy commander, who is responsible

ZOJ3623:Battle Ships(完全背包)

Battle Ships is a new game which is similar to Star Craft. In this game, the enemy builds a defense tower, which has L longevity. The player has a military factory, which can produce N kinds of battle ships. The factory takes tiseconds to produce the

Battle ships(二分图,建图,好题)

Battle ships Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1007    Accepted Submission(s): 353 Problem Description Dear contestant, now you are an excellent navy commander, who is responsible

zoj3623 Battle Ships

Battle Ships is a new game which is similar to Star Craft. In this game, the enemy builds a defense tower, which has L longevity. The player has a military factory, which can produce N kinds of battle ships. The factory takes ti seconds to produce th

zoj 3623 Battle Ships dp

 Description Battle Ships is a new game which is similar to Star Craft. In this game, the enemy builds a defense tower, which has L longevity. The player has a military factory, which can produce N kinds of battle ships. The factory takes ti second

Battle Ships(复习泛化物品**)

传送门Battle Ships Time Limit: 2 Seconds      Memory Limit: 65536 KB Battle Ships is a new game which is similar to Star Craft. In this game, the enemy builds a defense tower, which has L longevity. The player has a military factory, which can produce N

ZOJ3623 Battle Ships (完全背包)

Battle Ships Time Limit: 2 Seconds      Memory Limit: 65536 KB Battle Ships is a new game which is similar to Star Craft. In this game, the enemy builds a defense tower, which has L longevity. The player has a military factory, which can produce N ki

Battle Ships(完全背包)

Battle ShipsCrawling in process... Crawling failed Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Description Battle Ships is a new game which is similar to Star Craft. In this game, the enemy builds a defens