Hyperspace

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 314    Accepted Submission(s): 155

Problem Description
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 × 107). 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
 
Source
 
Recommend
zhuyuanchen520

题意:给定一些操作(0代表添加一个点,1代表删除一个点),求这些点的最远曼哈顿距离。

可先参考POJ  2926  Requirements:http://poj.org/problem?id=2926

POJ该题思路:

以二维平面为例:

设距离最远的两点为 i, j,可知所求的最大距离必定有以下四种形式之一:

(xi-xj)+(yi-yj), (xj-xi)+(yi-yj), (xi-xj)+(yj-yi), (xj-xi)+(yj-yi) 变形一下,把相同点的坐标放到一起,

即 (xi+yi)-(xj+yj), (-xi+yi)-(-xj+yj), (xi-yi)-(xj-yj), (-xi-yi)-(-xj-yj),可以发现即去绝对值之后把同一点的坐标放在一起,对应坐标符号相同。

假如我们用0表示符号,用1表示正号,那么 (xi+yi) 可以表示为 11。

那么要表示一个维数为 dem 的所有状态,只需要用 0 ~ (2^dem-1) 的所有二进制就可以了。

于是只要对所有的点 (xi,yi),依次计算出 (xi+yi), (xi-yi), (-xi+yi), (-xi-yi)这四种形式,然后把每个点i算出来的这四种情况的最大值、最小值分别记录(更新)到数组 max[] 和 min[] 中,然后枚举每一种去绝对值的组合,组合后的最大值即为 answer。

代码:

#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; const int N=;
const double INF=1e20; int n;
double num[N][],minx[<<],maxx[<<]; int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d",&n)){
for(int i=;i<n;i++)
for(int j=;j<;j++)
scanf("%lf",&num[i][j]);
for(int i=;i<(<<);i++){ //共有(1<<5)种状态,存储每种状态下的最大最小值
minx[i]=INF;
maxx[i]=-INF;
}
double sum;
int tmp;
for(int i=;i<n;i++)
for(int j=;j<(<<);j++){ //枚举每种状态
tmp=j;
sum=;
for(int k=;k<;k++){
if(tmp&)
sum+=num[i][k];
else
sum-=num[i][k];
tmp>>=;
}
if(maxx[j]<sum)
maxx[j]=sum;
if(minx[j]>sum)
minx[j]=sum;
}
double ans=-INF;
for(int i=;i<(<<);i++)
if(maxx[i]-minx[i]>ans)
ans=maxx[i]-minx[i];
printf("%.2f\n",ans);
}
return ;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<set> using namespace std; const int N=;
const double INF=1e20; int n,m,num[N][];
//map<int,int,greater<int> > mp[1<<5];
multiset<int> mst[<<];
multiset<int>::iterator it1,it2; int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d%d",&n,&m)){
for(int i=;i<(<<);i++)
mst[i].clear();
int od,x;
for(int i=;i<=n;i++){
scanf("%d",&od);
if(od==){
for(int j=;j<m;j++)
scanf("%d",&num[i][j]);
for(int j=;j<(<<m);j++){
int sum=;
for(int k=;k<m;k++){
if(j&(<<k))
sum+=num[i][k];
else
sum-=num[i][k];
}
mst[j].insert(sum);
}
}else{
scanf("%d",&x);
for(int j=;j<(<<m);j++){
int sum=;
for(int k=;k<m;k++){
if(j&(<<k))
sum+=num[x][k];
else
sum-=num[x][k];
}
it1=mst[j].find(sum);
mst[j].erase(it1);
}
}
int ans=;
//map<int,int>::iterator it1,it2;
for(int j=;j<(<<m);j++){
it1=mst[j].end();
it1--;
it2=mst[j].begin();
ans=max(ans,(*it1)-(*it2));
}
printf("%d\n",ans);
}
}
return ;
}

HDU 4666 Hyperspace (最远曼哈顿距离)的更多相关文章

  1. [HDU 4666]Hyperspace[最远曼哈顿距离][STL]

    题意: 许多 k 维点, 求这些点之间的最远曼哈顿距离. 并且有 q 次操作, 插入一个点或者删除一个点. 每次操作之后均输出结果. 思路: 用"疑似绝对值"的思想, 维护每种状态 ...

  2. HDU 4666 最远曼哈顿距离

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4666 关于最远曼哈顿距离的介绍: http://blog.csdn.net/taozifish/ar ...

  3. hdu 4666:Hyperspace(最远曼哈顿距离 + STL使用)

    Hyperspace Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Tota ...

  4. HDU 4666 Hyperspace (2013多校7 1001题 最远曼哈顿距离)

    Hyperspace Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Tota ...

  5. 多校联赛7 1001 hdu 4666(最远哈曼顿距离+优先队列)

    吐个糟,尼玛今天被虐成狗了,一题都没搞出来,这题搞了N久居然还是搞不出来,一直TLE,最后还是参考别人代码才领悟的,思路就这么简单, 就是不会转弯,看着模板却不会改,艹,真怀疑自己是不是个笨蛋题意:求 ...

  6. 2018 Multi-University Training Contest 10 CSGO(HDU - 6435)(最远曼哈顿距离)

    有 n 种主武器,m 种副武器.每种武器有一个基础分数k种属性值 X[i] . 选出一种主武器 mw 和一种副武器 sw,使得两种武器的分数和 + 每个属性的差值尽量大.(参考下面的式子) 多维的最远 ...

  7. poj 2926:Requirements(最远曼哈顿距离,入门题)

    Requirements Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3908   Accepted: 1318 Desc ...

  8. POJ-2926 Requirements 最远曼哈顿距离

    题目链接:http://poj.org/problem?id=2926 题意:求5维空间的点集中的最远曼哈顿距离.. 降维处理,推荐2009武森<浅谈信息学竞赛中的“0”和“1”>以及&l ...

  9. Codeforces 491B. New York Hotel 最远曼哈顿距离

    最远曼哈顿距离有两个性质: 1: 对每一个点(x,y)  分别计算  +x+y , -x+y , x-y , -x-y 然后统计每种组合的最大值就能够了, 不会对结果产生影响 2: 去掉绝对值 , 设 ...

随机推荐

  1. 如何将自己的Image镜像Push到Docker Hub

    首先需要一个docker官方账号 这里我添加了一个AspNetCore程序 通过创建了一个镜像(前面提过使用Dockerfile处理了) docker build -t dockertest . 首先 ...

  2. 【算法】后缀自动机(SAM) 例题

    算法介绍见:http://www.cnblogs.com/Sakits/p/8232402.html 广义SAM资料:https://www.cnblogs.com/phile/p/4511571.h ...

  3. [NOI2012]骑行川藏(未完成)

    题解: 满分又是拉格朗日啥的 以后再学 自己对于n=2猜了个三分 然后对拍了一下发现是对的

  4. day15--JavaScript语言

    JavaScript JavaScript是一门单独的编程语言.浏览器内置JavaScript的解释器. 独立的语言,浏览器具有js解释器. 存在与HTML中,在HTML中写JavaScript,存在 ...

  5. 百度未授权使用地图API

    百度地图管理员的回复:这是KEY服务升级的问题, 给您造成的不便,非常抱歉.但我们昨日已修复,你可以再审核一番.若不可以,请提供一下您的系统ak,邮箱或qq发送至(wangwenhai@baidu.c ...

  6. python全栈开发day37-html

    web准备总结: 结构标准:相当于人的身体.html就是用来制作网页的. 表现标准: 相当于人的衣服.css就是对网页进行美化的. 行为标准: 相当于人的动作.JS就是让网页动起来,具有生命力的 1. ...

  7. Codeforces 251C Number Transformation

    Number Transformation 我们能发现这个东西是以2 - k的lcm作为一个循环节, 然后bfs就好啦. #include<bits/stdc++.h> #define L ...

  8. Pig和Hive的对比

    Pig Pig是一种编程语言,它简化了Hadoop常见的工作任务.Pig可加载数据.表达转换数据以及存储最终结果.Pig内置的操作使得半结构化数据变得有意义(如日志文件).同时Pig可扩展使用Java ...

  9. 3.Django| 视图层| 模板层

    1.视图函数 文件在view_demo 一个视图函数简称视图,是一个简单的Python 函数,它接受Web请求并且返回Web响应.响应可以是一张网页的HTML内容,一个重定向,一个404错误,一个XM ...

  10. Educational Codeforces Round 47 (Div 2) (A~G)

    目录 Codeforces 1009 A.Game Shopping B.Minimum Ternary String C.Annoying Present D.Relatively Prime Gr ...