Hdu-4666 Hyperspace(曼哈顿距离)

xiaoxiao2021-02-28  61

The great Mr.Smith has invented a hyperspace particle generator. The device is very powerful. The device can generate a hyperspace. In the hyperspace, particle may appear and disappear randomly. At the same time a great amount of energy was generated.  However, the device is in test phase, often in a unstable state. Mr.Smith worried that it may cause an explosion while testing it. The energy of the device is related to the maximum manhattan distance among particle.  Particles may appear and disappear any time. Mr.Smith wants to know the maxmium manhattan distance among particles when particle appears or disappears. Input The input contains several test cases, terminated by EOF.  In each case: In the first line, there are two integer q(number of particle appear and disappear event, ≤60000) and k(dimensions of the hyperspace that the hyperspace the device generated, ≤5). Then follows q lines. In each line, the first integer ‘od’ represents the event: od = 0 means this is an appear  event. Then follows k integer(with absolute value less then 4 × 10  7). od = 1 means this is an disappear event. Follows a integer p represents the disappeared particle appeared in the pth event. Output Each test case should contains q lines. Each line contains a integer represents the maximum manhattan distance among paticles. Sample Input 10 2 0 208 403 0 371 -180 1 2 0 1069 -192 0 418 -525 1 5 1 1 0 2754 635 0 -2491 961 0 2954 -2516 Sample Output 0 746 0 1456 1456 1456 0 2512 5571

8922

分析:一开始以为要上KD-TREE,后来看了题解发现有个很巧妙的做法,两点间的曼哈顿距离等于各个坐标的绝对值差之和,绝对值等于大数减小数,但是因为这道题的k很小,所以我们可以枚举每一位的情况然后暴力统计,假如维度为2,A(x1,y1) 和 B(x2,y2)间的距离 可能为 x1-x2+y1-y2,x1-x2+y2-y1,x2-x1+y1-y2,x2-x1+y2-y1这四种,移项后就是(x1+y1)+(-x2-y2),(x1-y1)+(-x2+y2),(-x1+y1)+(x2-y2),(-x1-y1)+(x2+y2),

A点每个坐标的符号正好和B相反,所以我们只需要用set去维护每种组合的最大值,然后再枚举所有组合和其对应组合相加就可以求出来答案了。

#include <bits/stdc++.h> using namespace std; typedef long long ll; int q,k,num,op,a[60005][6]; multiset<int> s[1<<5]; int got(int x,int y) { int tot = 0; for(int i = 1;i <= k;i++) if(y & (1<<(i-1))) tot += a[x][i]; else tot -= a[x][i]; return tot; } int got_max(int x) { if(s[x].size() == 0) return 0; set<int> :: iterator it = s[x].end(); it--; return *it; } int main() { cin.sync_with_stdio(false); while(cin>>q>>k) { for(int i = 0;i < (1<<5);i++) s[i].clear(); for(int i = 1;i <= q;i++) { cin>>op; if(!op) { for(int j = 1;j <= k;j++) cin>>a[i][j]; for(int j = 0;j < (1<<k);j++) s[j].insert(got(i,j)); } else { cin>>num; for(int j = 0;j < (1<<k);j++) { multiset<int> :: iterator it = s[j].find(got(num,j)); s[j].erase(it); } } int ans = 0; for(int j = 0;j < (1<<k);j++) ans =max(got_max(j) + got_max((1<<k)-j-1),ans); cout<<ans<<endl; } } }

转载请注明原文地址: https://www.6miu.com/read-46051.html

最新回复(0)