Codeforces 567D:One-Dimensional Battle Ships(二分)

time limit per test : 1 secondios

memory limit per test : 256 megabytesc++

input : standard inputspa

output : standard outputcode

Problem Description

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).orm

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

But here's the problem! Alice like to cheat. May be that is why she responds to each Bob's move with a "miss".ip

Help Bob catch Alice cheating — find Bob's first move, such that after it you can be sure that Alice cheated.ci

Input

The first line of the input contains three integers: \(n, k\) and \(a (1 ≤ n, k, a ≤ 2·10^5)\) — 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.input

The second line contains integer \(m (1 ≤ m ≤ n)\) — the number of Bob's moves.

The third line contains \(m\) distinct integers \(x_1, x_2, ..., x_m\), where \(x_i\) 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

題意

一個長爲\(1\times n\)的區域內有\(k\)個戰艦,每一個戰艦的長度均爲\(a\),Bob射擊\(m\)個不一樣的位置,對於每次射擊,Alice都會告訴Bob是否擊中。

可是由於Alice會撒謊,因此當Bob擊中的時候,Alice也會說沒有擊中

問Bob最先能夠在第幾回射擊的時候發現Alice在說謊

思路

由於隨着射擊次數的增多,Bob發現Alice說謊的可能性越大,因此能夠利用二分來解決

\([1,mid]\)區間的位置進行排序,而後計算這些位置若是所有沒有擊中,能夠放下多少戰艦,若是超過\(k\)個,那麼當前的\(mid\)是可行的,查詢前半部分,不然,查詢後半部分

代碼

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1e6+10;
const int mod=1e9+7;
const int maxm=1e3+10;
using namespace std;
int x[maxn];
int b[maxn];
int n,k,a;
bool check(int mid)
{
    for(int i=1;i<=mid;i++)
        b[i]=x[i];
    sort(b+1,b+1+mid);
    int cnt=0;
    for(int i=1;i<=mid;i++)
        cnt+=(b[i]-b[i-1])/(a+1);   // b[i]和b[i-1]之間能夠放多少戰艦
    // b[mid]->最後一個位置能夠放多少戰艦
    cnt+=(n-b[mid]+1)/(a+1);
    if(cnt>=k)
        return true;
    return false;
}
int main(int argc, char const *argv[])
{
    #ifndef ONLINE_JUDGE
        freopen("/home/wzy/in", "r", stdin);
        freopen("/home/wzy/out", "w", stdout);
        srand((unsigned int)time(NULL));
    #endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>k>>a;
    int m;
    cin>>m;
    for(int i=1;i<=m;i++)
    {
        cin>>x[i];
    }
    int l=1,r=m,ans=inf;
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(check(mid))
            l=mid+1;
        else
            r=mid-1,ans=min(ans,mid);
    }
    if(ans==inf)
        cout<<-1<<endl;
    else
        cout<<ans<<endl;
    #ifndef ONLINE_JUDGE
        cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl;
    #endif
    return 0;
}
相關文章
相關標籤/搜索