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。

代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4.  
  5. using namespace std;
  6.  
  7. const int N=;
  8. const double INF=1e20;
  9.  
  10. int n;
  11. double num[N][],minx[<<],maxx[<<];
  12.  
  13. int main(){
  14.  
  15. //freopen("input.txt","r",stdin);
  16.  
  17. while(~scanf("%d",&n)){
  18. for(int i=;i<n;i++)
  19. for(int j=;j<;j++)
  20. scanf("%lf",&num[i][j]);
  21. for(int i=;i<(<<);i++){ //共有(1<<5)种状态,存储每种状态下的最大最小值
  22. minx[i]=INF;
  23. maxx[i]=-INF;
  24. }
  25. double sum;
  26. int tmp;
  27. for(int i=;i<n;i++)
  28. for(int j=;j<(<<);j++){ //枚举每种状态
  29. tmp=j;
  30. sum=;
  31. for(int k=;k<;k++){
  32. if(tmp&)
  33. sum+=num[i][k];
  34. else
  35. sum-=num[i][k];
  36. tmp>>=;
  37. }
  38. if(maxx[j]<sum)
  39. maxx[j]=sum;
  40. if(minx[j]>sum)
  41. minx[j]=sum;
  42. }
  43. double ans=-INF;
  44. for(int i=;i<(<<);i++)
  45. if(maxx[i]-minx[i]>ans)
  46. ans=maxx[i]-minx[i];
  47. printf("%.2f\n",ans);
  48. }
  49. return ;
  50. }
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<set>
  5.  
  6. using namespace std;
  7.  
  8. const int N=;
  9. const double INF=1e20;
  10.  
  11. int n,m,num[N][];
  12. //map<int,int,greater<int> > mp[1<<5];
  13. multiset<int> mst[<<];
  14. multiset<int>::iterator it1,it2;
  15.  
  16. int main(){
  17.  
  18. //freopen("input.txt","r",stdin);
  19.  
  20. while(~scanf("%d%d",&n,&m)){
  21. for(int i=;i<(<<);i++)
  22. mst[i].clear();
  23. int od,x;
  24. for(int i=;i<=n;i++){
  25. scanf("%d",&od);
  26. if(od==){
  27. for(int j=;j<m;j++)
  28. scanf("%d",&num[i][j]);
  29. for(int j=;j<(<<m);j++){
  30. int sum=;
  31. for(int k=;k<m;k++){
  32. if(j&(<<k))
  33. sum+=num[i][k];
  34. else
  35. sum-=num[i][k];
  36. }
  37. mst[j].insert(sum);
  38. }
  39. }else{
  40. scanf("%d",&x);
  41. for(int j=;j<(<<m);j++){
  42. int sum=;
  43. for(int k=;k<m;k++){
  44. if(j&(<<k))
  45. sum+=num[x][k];
  46. else
  47. sum-=num[x][k];
  48. }
  49. it1=mst[j].find(sum);
  50. mst[j].erase(it1);
  51. }
  52. }
  53. int ans=;
  54. //map<int,int>::iterator it1,it2;
  55. for(int j=;j<(<<m);j++){
  56. it1=mst[j].end();
  57. it1--;
  58. it2=mst[j].begin();
  59. ans=max(ans,(*it1)-(*it2));
  60. }
  61. printf("%d\n",ans);
  62. }
  63. }
  64. return ;
  65. }

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. 开始写博客,学习Linq(2)

    linq的功能是什么? 它将极大地改变应用程序或组件处理数据的方式.这是第一个功能. LINQ to Objects.LINQ to SQL和LINQ to XML,是LINQ三大主要功能,当然LIN ...

  2. CSS3实现整屏切换效果

    页面结构 实现思路与大众方法类似,如图 每个section就是一页内容,它的大小充满了屏幕(红色区域),一个Container由多个section构成,我们通过改变container的位置,来达到页面 ...

  3. 【noip模拟赛8】魔术棋子

    描述 在一个M*N的魔术棋盘中,每个格子中均有一个整数,当棋子走进这个格子中,则此棋子上的数会被乘以此格子中的数.一个棋子从左上角走到右下角,只能向右或向下行动,请问此棋子走到右下角后,模(mod)K ...

  4. 8.Django-form组件

    1.form组件的校验功能 文件formsdemo models from django.db import models # Create your models here. class UserI ...

  5. 013 JstlView

    一:InternalResourceViewResolver 1.InternalResourceViewResolver JSP是常见的视图技术,可以使用InternalResourceViewRe ...

  6. 基于Java的REST架构风格及接口安全性设计的讨论

    1.REST即表现层状态传递(Representational [,rɛprɪzɛn'teʃnl] State Transfer,简称REST). (1)REST名词解释: 通俗来讲就是资源在网络中以 ...

  7. hadoop分布式集群完全安装(非HA)

    一.各节点基础环境配置(最好每台都配置) 先输入su获取root权限 1修改主机名 输入vim /etc/sysconfig/network 改成: NETWORKING=yes HOSTNAME=m ...

  8. ASP.net MVC把Html Table导出Excel

    [HttpPost] public ActionResult ExportExcel(FormCollection form) { string strHtml = form["hHtml& ...

  9. 20172319 2018.10.19《Java程序设计教程》第7周课堂实践(补写博客)

    20172319 2018.10.19 <Java程序设计教程>第7周课堂实践 课程:<程序设计与数据结构> 班级:1723 学生:唐才铭 学号:20172319 指导老师:王 ...

  10. Oracle 删除重复数据只留一条(转)

    转自:http://www.cnblogs.com/252e/archive/2012/09/13/2682817.html 查询及删除重复记录的SQL语句   1.查找表中多余的重复记录,重复记录是 ...