HDU 4666 Hyperspace【最远曼哈顿距离+优先队列】

来源:互联网 发布:美国零售数据最新 编辑:程序博客网 时间:2024/04/30 06:40

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

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


#include <cstdio>#include <algorithm>#include <queue>#include <cstring>#include <iostream>using namespace std;#define N 60010#define inf 0x3fffffffint 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\n", ans);        }    }    return 0;}


原创粉丝点击