zoukankan      html  css  js  c++  java
  • CF 567D(One-Dimensional Battle Ships-二分)

    D. One-Dimensional Battle Ships
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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: nk 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 nk 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 from1 to m in the order the were made. If the sought move doesn't exist, then print "-1".

    Sample test(s)
    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

    裸二分





    #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("D.in","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;
    }
    





  • 相关阅读:
    【剑指Offer】面试题14- I. 剪绳子
    【剑指Offer】面试题56
    【LeetCode】202. 快乐数
    【LeetCode】1095. 山脉数组中查找目标值
    【LeetCode】260. 只出现一次的数字 III
    【剑指Offer】面试题56
    【LeetCode】33. 搜索旋转排序数组
    【LeetCode】23. 合并K个排序链表
    【LeetCode】46. 全排列
    mysql可视化工具下载地址2017.6.27
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/6906778.html
Copyright © 2011-2022 走看看