zoukankan      html  css  js  c++  java
  • STL(set_pair)运用 CF#Pi D. 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: 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".

    这题我一开始用裸数组写的,写的很烦。对于我这种代码能力不太好能学习不少,有两份代码,第二份非常简洁,某位大神的。

     

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<set>
    #include<algorithm>
    #include<utility>
    using namespace std;
    #define PII pair<int,int>
    #define MP make_pair
    #define N 200005
    
    int k,len,m,n;
    int pri[N],num,ans;
    set<PII> myset;
    set<PII>::iterator it;
    
    int main(){
        int i,j,k;
        while(~scanf("%d",&n)){
            myset.clear();
            scanf("%d %d %d",&k,&len,&m);
            PII tp = MP(1,n);
            myset.insert(tp);
            ans=-1;
            num=(tp.second-tp.first+2) / (len+1); //计算船的最大数量 记住两个船之间不能接触然后加2是因为为第一条前补1
            bool flag=true;
    
            for(i=0;i<m;i++){
                scanf("%d",&pri[i]);
                if(flag){
                    it =myset.lower_bound(MP(pri[i],n + 1)); //找到所在区间
                    it--;
                    PII p =*it;
                    myset.erase(it);
                    num-= (p.second-p.first+2) / (len + 1);  //现将这个区间可能存在的船数剪掉
                    if(p.first <= pri[i]-1){
                        PII tp= MP(p.first , pri[i]-1);
                        num+= (tp.second-tp.first+2) / (len+1);
                        myset.insert(tp);
                    }
                    if(pri[i]+1 <= p.second){
                        PII tp= MP(pri[i]+1 , p.second);
                        num+= (tp.second-tp.first+2) / (len+1);
                        myset.insert(tp);
                    }
                    if(num<k){     //如果小于K就不成立了
                        ans=i+1;
                        flag=false;
                    }
                }
            }
    
            printf("%d
    ",ans);
        }
        return 0;
    }
    //令一种解法
    
    #include <bits/stdc++.h>
    
    using namespace std;
    
    int N, K, W, M;
    int A[200000];
    int B[200000];
    
    int solve(int X)
    {
        for(int i=0; i<=X; i++)
            B[i]=A[i];
        sort(B, B+X+1);
        int ret=0;
        if(B[0]>W)
            ret+=1+(B[0]-1-W)/(W+1);
        if(N-B[X]>=W)
            ret+=1+(N-B[X]-W)/(W+1);
        for(int i=1; i<=X; i++)
            if(B[i]-B[i-1]-1>=W)
                ret+=1+(B[i]-B[i-1]-1-W)/(W+1);
        return ret;
    }
    
    int main()
    {
        scanf("%d%d%d", &N, &K, &W);
        scanf("%d", &M);
        for(int i=0; i<M; i++)
            scanf("%d", A+i);
        int lo=0, mid, hi=M;
        while(lo<hi)
        {
            mid=(lo+hi)/2;
            if(solve(mid)<K)
                hi=mid;
            else
                lo=mid+1;
        }
        if(lo==M)
            printf("-1
    ");
        else
            printf("%d
    ", lo+1);
        return 0;
    }
    


  • 相关阅读:
    vue-cli3.X快速创建项目
    Oracle中exists替代in语句
    Java根据子节点递归父节点
    记一次拆分包裹的算法
    Ajax下载文件
    Spring Boot 调用 MongoRepository时报org.springframework.beans.factory.NoSuchBeanDefinitionException错误的解决办法
    ORACLE拼日期
    记一次wordpress安装过程中遇到的问题及解决办法
    bootstrap动态添加Tab标签页
    Intellij Idea 2016 配置Tomcat虚拟目录
  • 原文地址:https://www.cnblogs.com/Basasuya/p/8433777.html
Copyright © 2011-2022 走看看