zoukankan      html  css  js  c++  java
  • 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 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 from 1 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

    这题可以先算出刚开始最大能摆放的木板数,用set维护起点和终点的idx(坐标)和num(即当前空位和下一个空位间最大能放的木板数),用sum记录总共能放的木板数。每次更新一个坐标a,找到和当前要更新的点最近的右边一点坐标b,然后更新当前点a的num和这一点左边b-1的num,然后重新调整sum,如果sum<k,那么这点就是答案,否则把a放入set里.

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    using namespace std;
    #define maxn 200050
    struct node{
    	int num,idx;
    }a,temp1,temp2,b1,b2;
    int b[maxn];
    bool operator <(node a,node b){
    	return a.idx<b.idx;
    }
    set<node>myset;
    set<node>::iterator it;
    
    int shumu(int l,int m)
    {
    	int i,j,num,sheng;
    	num=l/(m+1);
    	sheng=l%(m+1);
    	if(sheng==m)num++;
    	return num;
    }
    
    int main()
    {
    	int n,m,k,i,j,h,num,flag,cnt;
    	while(scanf("%d%d%d",&n,&k,&m)!=EOF)
    	{
    		myset.clear();
    		num=shumu(n,m);
    		a.idx=0;a.num=num;
    		myset.insert(a);
    		a.idx=n+1;a.num=0;
    		myset.insert(a);
    
    		scanf("%d",&h);
    		for(i=1;i<=h;i++){
    			scanf("%d",&b[i]);
    		}
    		flag=1;cnt=0;
            for(i=1;i<=h;i++){
    			a.idx=b[i];a.num=0;
    			it=myset.lower_bound(a);
    			temp2=*it;
    			it--;
    			temp1=*it;
    			num-=temp1.num;
    			
    			b1.idx=temp1.idx;b1.num=shumu(b[i]-temp1.idx-1,m);
    			b2.idx=b[i];b2.num=shumu(temp2.idx-b[i]-1,m);
    			num+=b1.num+b2.num;
    			if(num<k){
    				flag=0;cnt=i;break;
    			}
    			myset.erase(temp1);
    			myset.insert(b1);
    			myset.insert(b2);
    		}
    		if(flag==0){
    			printf("%d
    ",i);continue;
    		}
    		else printf("-1
    ");
    	}
    	return 0;
    }


  • 相关阅读:
    管理表空间和数据文件——维护表空间——改变表空间的读写状态和改变表空间名称
    管理表空间和数据文件——数据库逻辑结构
    管理用户和PROFILE ——管理PROFILE——使用PROFILE管理口令
    管理对象空间——管理存储参数
    管理表空间和数据文件——显示表空间和数据文件信息
    oracle Wallet的使用
    管理表空间和数据文件——维护表空间——设置默认表空间和删除表空间和删除数据文件盒临时文件
    管理用户和PROFILE——用户方案和profile
    管理表空间和数据文件——建立表空间——建立临时表空间
    启动和停止数据库——停顿和暂停数据库
  • 原文地址:https://www.cnblogs.com/herumw/p/9464698.html
Copyright © 2011-2022 走看看