zoukankan      html  css  js  c++  java
  • HDU 4666 Hyperspace【最远曼哈顿距离+优先队列】

    这个题是动态的求最远曼哈顿距离。做法和POJ 2926 Requirements一样,都是通过二进制枚举符号的情况。

    每插入一个节点都要询问最大值和最小值,因此用一个优先队列或者堆维护就可以了。


    #include <cstdio>
    #include <algorithm>
    #include <queue>
    #include <cstring>
    #include <iostream>
    using namespace std;
    #define N 60010
    #define inf 0x3fffffff
    int q, k, a[5];
    bool vis[N];
    struct node1 {
        int x, id;
        bool operator<(const node1& a) const {
            return x < a.x;
        }
    }t1;
    struct node2 {
        int x, id;
        bool operator<(const node2& a) const {
            return x > a.x;
        }
    }t2;
    priority_queue<node1> q1[1<<5];
    priority_queue<node2> q2[1<<5];
    
    int main() {
        while (scanf("%d%d", &q, &k) == 2) {
            int t;
            memset(vis, false, sizeof(vis));
            for (int i=0; i<(1<<k); i++) {
                while (!q1[i].empty()) q1[i].pop();
                while (!q2[i].empty()) q2[i].pop();
            }
            int c, ans, mi, mx;
            for (int p=1; p<=q; p++) {
                scanf("%d", &t);
                if (t == 0) {
                    for (int i=0; i<k; i++) scanf("%d", &a[i]);
                    for (int s=0; s<(1<<k); s++) {
                        c = 0;
                        for (int i=0; i<k; i++)
                            if ((1<<i) & s) c += a[i];
                            else c -= a[i];
                        t1.x = t2.x = c;
                        t1.id = t2.id = p;
                        q1[s].push(t1);
                        q2[s].push(t2);
                    }
                } else {
                    scanf("%d", &c);
                    vis[c] = true;
                }
                ans = 0;
                for (int s=0; s<(1<<k); s++) {
                    while (true) {
                        t1 = q1[s].top();
                        if (!vis[t1.id]) break;
                        q1[s].pop();
                    }
                    while (true) {
                        t2 = q2[s].top();
                        if (!vis[t2.id]) break;
                        q2[s].pop();
                    }
                    ans = max(ans, t1.x-t2.x);
                }
                printf("%d
    ", ans);
            }
        }
        return 0;
    }
    


  • 相关阅读:
    CCNA 第二章 以太网回顾
    CCNA 第一章 网络互联
    solidworks中 toolbox调用出现未配置的解决方法
    linux之df命令
    linux之du命令
    linux之pid文件
    linux之mysql启动问题
    linux之使用cron,logrotate管理日志文件
    wordpress(一)wordpress环境的搭建
    phpwind8.7升级9.0.1过程(四)20130207升级到20141228
  • 原文地址:https://www.cnblogs.com/bbsno1/p/3258052.html
Copyright © 2011-2022 走看看