CF 567D (One-dimen1_battle Ships-binary), battleships

Source: Internet
Author: User

CF 567D (One-dimen1_battle Ships-binary), battleships

D. One-dimen1_battle Shipstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

Alice and Bob love playing one-dimen1_battle ships. They play on the field in the form of a line consistingNSquare cells (that is, on a 1 hour × hourNTable ).

At the beginning of the game Alice putsKShips on the field without telling their positions to Bob. Each ship looks as a 1 minute × hourARectangle (that is, it occupies a sequenceAConsecutive 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.


The first line of the input contains three integers:N,KAndA(1 digit ≤ DigitN, Bytes,K, Bytes,ALimit ≤ limit 2. 105)-the size of the field, the number of the ships and the size of each ship. It is guaranteed thatN,KAndAAre such that you can putKShips of sizeAOn the field, so that no two ships intersect or touch each other.

The second line contains integerM(1 digit ≤ DigitMLimit ≤ limitN)-The number of Bob's moves.

The third line containsMDistinct integersX1, bytes,X2, middle..., middle ,...,XM, WhereXIIs the number of the cell where Bob madeI-Th shot. The cells are numbered from left to right from 1N.


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 from1MIn the order the were made. If the sought move doesn't exist, then print "-1 ".

Sample test (s) input
11 3 354 8 6 1 11
5 1 321 5
5 1 313

Bare binary

#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<functional>#include<iostream>#include<cmath>#include<cctype>#include<ctime>using namespace std;#define For(i,n) for(int i=1;i<=n;i++)#define Fork(i,k,n) for(int i=k;i<=n;i++)#define Rep(i,n) for(int i=0;i<n;i++)#define ForD(i,n) for(int i=n;i;i--)#define RepD(i,n) for(int i=n;i>=0;i--)#define Forp(x) for(int p=pre[x];p;p=next[p])#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  #define Lson (x<<1)#define Rson ((x<<1)+1)#define MEM(a) memset(a,0,sizeof(a));#define MEMI(a) memset(a,127,sizeof(a));#define MEMi(a) memset(a,128,sizeof(a));#define INF (2139062143)#define F (100000007)#define MAXN (200000+10)typedef long long ll;ll mul(ll a,ll b){return (a*b)%F;}ll add(ll a,ll b){return (a+b)%F;}ll sub(ll a,ll b){return (a-b+(a-b)/F*F+F)%F;}void upd(ll &a,ll b){a=(a%F+b%F)%F;}int n,k,a,m;int x[MAXN],x2[MAXN];bool check(int m){For(i,m) x2[i]=x[i];sort(x2+1,x2+1+m);int l=1,tot=0;For(i,m) {int len=x2[i]-l+1;tot+=len/(a+1);l=x2[i]+1;}tot+=(n+1-l+1)/(a+1);if (tot>=k) return 1;return 0;}int main(){//freopen("","r",stdin);//freopen(".out","w",stdout);cin>>n>>k>>a>>m;For(i,m) scanf("%d",&x[i]);int l=1,r=m,ans=INF;while(l<=r){int m=(r+l)/2;if (check(m)) l=m+1;else r=m-1,ans=min(ans,m);}if (ans==INF) ans=-1;cout<<ans<<endl;return 0;}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.