zoukankan      html  css  js  c++  java
  • Codeforces555 B. Case of Fugitive

    Codeforces题号:#310B

    出处: Codeforces

    主要算法:贪心+优先队列

    难度:4.6

    思路分析:

           这道题乍一看没有思路……

      考虑贪心的做法。首先预处理出每两座相邻的桥之间边界相差的min和max(即题目要求的),存在b数组中。将桥的长度从小到大排序。将b数组按照min从小到大排序。

      这样做有什么好处呢?我们枚举每一座桥,然后按顺序选出它适合放置的那些区间。由于这些区间都是适合放这座桥的,所以我们自然要选择差距最小的,也就是max最小的。这其实是一个贪心:让当前这座桥利用的区间尽量小,让别的更长的桥有更大的空间——这就是为什么b数组要排序。

      那么具体如何来实现呢?我们可以维护一个优先队列。在这些桥的长度都>=min的情况下,我们需要max最小。因此我们可以用优先队列维护,max最小的作为堆顶。然后每一次都选出第一个区间来安置当前这座桥。如果发现无法安置,那么也就是说在所有适合当前桥的区间都已近用完了,也就无解了。

    代码注意点:

      变量名不要打错。

    Code

    /** This Program is written by QiXingZhi **/
    #include <cstdio>
    #include <queue>
    #include <cstring>
    #include <algorithm>
    #define  Max(a,b)  (((a)>(b)) ? (a) : (b))
    #define  Min(a,b)  (((a)<(b)) ? (a) : (b))
    using namespace std;
    typedef long long ll;
    #define int ll
    const int N = 200010;
    const int M = 200010;
    const int INF = 1061109567;
    inline int read(){
        int x = 0; int w = 1; register int c = getchar();
        while(c ^ '-' && (c < '0' || c > '9')) c = getchar();
        if(c == '-') w = -1, c = getchar();
        while(c >= '0' && c <= '9') x = (x << 3) +(x << 1) + c - '0', c = getchar();
        return x * w;
    }
    struct Island{
        int l,r;
    }a[N];
    struct Dist{
        int min,max,idx;
        friend bool operator < (Dist a, Dist b){
            return a.max > b.max;
        }
    }b[N];
    struct Bridge{
        int len,idx;
    }bri[M];
    int n,m,top,cnt;
    int ans[N];
    priority_queue <Dist> q;
    inline bool comp_dist(Dist& a, Dist& b){
        if(a.min != b.min) return a.min < b.min;
        return a.max < b.max;
    }
    inline bool comp_bridge(Bridge& a, Bridge& b){
        return a.len < b.len;
    }
    #undef int
    int main(){
    #define int ll
    //    freopen(".in","r",stdin);
        n = read(), m = read();
        for(int i = 1; i <= n; ++i){
            a[i].l = read();
            a[i].r = read();
        }
        for(int i = 1; i <= m; ++i){
            bri[i].len = read();
            bri[i].idx = i;
        }
        for(int i = 1; i < n; ++i){
            b[i].max = a[i+1].r - a[i].l;
            b[i].min = a[i+1].l - a[i].r;
            b[i].idx = i;
        }
        sort(b+1,b+n,comp_dist);
        sort(bri+1,bri+m+1,comp_bridge);
        int lst = 1;
        for(int i = 1; i <= m; ++i){
            if(cnt >= n-1) break;
            while(lst < n && b[lst].min <= bri[i].len && bri[i].len <= b[lst].max){
                q.push(b[lst]);
                ++lst;
            }
            if(!q.size()) continue;
            Dist tmp = q.top();
            q.pop();
            if(bri[i].len <= tmp.max){
                ans[tmp.idx] = bri[i].idx;
                ++cnt;
            }
            else{
                printf("No");
                return 0;
            }
        }
        if(cnt < n-1){
            printf("No");
            return 0;
        }
        printf("Yes
    ");
        for(int i = 1; i < n; ++i){
            printf("%lld ", ans[i]);
        }
        return 0;
    }

     

  • 相关阅读:
    安卓任意两个或多个Fragment之间的交互与刷新界面
    内存溢出和内存泄漏
    求直方图围成的最大矩形面积
    判断一个字符串是否是由另2个字符串交错组成的
    矩阵的旋转
    求滑动窗口的最大值
    面向过程和面向对象的区别
    关于丑数
    求连续子数组的最大和
    多数投票算法(Majority Vote Algorithm)
  • 原文地址:https://www.cnblogs.com/qixingzhi/p/9303876.html
Copyright © 2011-2022 走看看