Description

An undergraduate student, realizing that he needs to do research to improve his chances of being accepted to graduate school, decided that it is now time to do some independent research. Of course, he has decided to do research in the most important domain:
the requirements he must fulfill to graduate from his undergraduate university. First, he discovered (to his surprise) that he has to fulfill 5 distinct requirements: the general institute requirement, the writing requirement, the science requirement, the
foreign-language requirement, and the field-of-specialization requirement. Formally, a requirement is a fixed number of classes that he has to take during his undergraduate years. Thus, for example, the foreign language requirement specifies that the student
has to take 4 classes to fulfill this requirement: French I, French II, French III, and French IV. Having analyzed the immense multitude of the classes that need to be taken to fulfill the different requirements, our student became a little depressed about
his undergraduate university: there are so many classes to take…

Dejected, the student began studying the requirements of other universities that he might have chosen after high school. He found that, in fact, other universities had exactly the same 5 requirements as his own university. The only difference was that different
universities had different number of classes to be satisfied in each of the five requirement.

Still, it appeared that universities have pretty similar requirements (all of them require a lot of classes), so he hypothesized that no two universities are very dissimilar in their requirements. He defined the dissimilarity of two universities X and Y as
|x1 − y1| + |x2 − y2| + |x3 − y3| + |x4 − y4| + |x5 − y5|,
where an xi (yi) is the number of classes in the requirement i of university X (Y) multiplied by an appropriate factor that measures hardness of the corresponding requirement at the corresponding
university.

Input

The first line of the input file contains an integer N (1 ≤ N ≤ 100 000), the number of considered universities. The following N lines each describe the requirements of a university. A university X is described by the
five non-negative real numbers x1 x2 x3 x4 x5.

Output

On a single line, print the dissimilarity value of the two most dissimilar universities. Your answer should be rounded to exactly two decimal places.

Sample Input

3
2 5 6 2 1.5
1.2 3 2 5 4
7 5 3 2 5

Sample Output

12.80

题意:在五维坐标系下求n个点中两个点的最大曼哈顿距离。

思路:以二维坐标系为例,(x1,y1)(x2,y2)之间的距离为|x1-x2|+|y1-y2|,可能取±(x1-x2)±(y1-y2),且其他情况下算出来的关于这两个点的最大距离肯定比正确算曼哈顿距离的值小(去掉绝对值符号了)。我们可以把同一个坐标的放在一起,变为(±x1±y1)-(±x2±y2)。(注:这里x1和x2前面的符号是一致的,y1和y2前面的符号是一致的,这样才能保证对应值仍保持相减关系)。所以我们只要枚举每一维的分量前面的符号即可,然后求出每一个符号分量状态的最大值和最小值的差,更新最大值就行。

下面给出n维的模板(时间复杂度为O(n*dem*2^dem)

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 100050
#define dem 5
struct node{
double p[dem+1];
}a[maxn];
double maxx[1<<dem],minx[1<<dem]; int main()
{
int n,m,i,j,state,t;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++){
for(j=1;j<=dem;j++){
scanf("%lf",&a[i].p[j]);
}
}
for(state=0;state<(1<<dem);state++){
maxx[state]=-inf;
minx[state]=inf;
}
double ans=0;
for(state=0;state<(1<<dem);state++){
for(i=1;i<=n;i++){
double cnt=0;
for(t=1;t<=dem;t++){
if(state&(1<<(t-1)) ){
cnt+=a[i].p[t];
}
else{
cnt-=a[i].p[t];
}
}
maxx[state]=max(maxx[state],cnt);
minx[state]=min(minx[state],cnt);
}
ans=max(ans,maxx[state]-minx[state]);
}
printf("%.2f\n",ans);
}
return 0;
}

poj2926Requirements (曼哈顿距离)的更多相关文章

  1. Hdu4311 || 4312Meeting point-1/-2 n个点中任意选一个点使得其余点到该点曼哈顿距离之和最小

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  2. Atitti knn实现的具体四个距离算法 欧氏距离、余弦距离、汉明距离、曼哈顿距离

    Atitti knn实现的具体四个距离算法  欧氏距离.余弦距离.汉明距离.曼哈顿距离 1. Knn算法实质就是相似度的关系1 1.1. 文本相似度计算在信息检索.数据挖掘.机器翻译.文档复制检测等领 ...

  3. 【POJ 3241】Object Clustering 曼哈顿距离最小生成树

    http://poj.org/problem?id=3241 曼哈顿距离最小生成树模板题. 核心思想是把坐标系转3次,以及以横坐标为第一关键字,纵坐标为第二关键字排序后,从后往前扫.扫完一个点就把它插 ...

  4. 【HDU 4311】Meeting point-1(前缀和求曼哈顿距离和)

    题目链接 正经解法: 给定n个点的坐标,找一个点,到其他点的曼哈顿距离之和最小.n可以是100000.大概要一个O(nlogn)的算法.算曼哈顿距离可以把x和y分开计算排好序后计算前缀和就可以在O(1 ...

  5. hdu4666 Hyperspace ——曼哈顿距离

    link:http://acm.hdu.edu.cn/showproblem.php?pid=4666 这题学会了怎么处理曼哈顿距离. 比如维数是k,那么每个点有2^k个状态,求出在每个状态下,所有点 ...

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

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

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

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

  8. 某个点到其他点的曼哈顿距离之和最小(HDU4311)

    Meeting point-1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. HDU 4539 郑厂长系列故事――排兵布阵(曼哈顿距离)

    这虽然是中文题,然而没看懂,不懂的地方,就是在曼哈顿距离这块,网上搜索了一下,写了个程序,是测试曼哈顿距离的. 曼哈顿距离:两点(x1,y1)(x2,y2)的曼哈顿距离为|x1-x2|+|y1-y2| ...

随机推荐

  1. 【JavaWeb】i18n 国际化

    i18n 国际化 什么是 i18n 国际化(Internationalization)指的是同一个网站可以支持多种不同的语言,以方便不同国家,不同语种的用户访问. 希望相同的一个网站,不同人访问的时候 ...

  2. C语言实现蛇形矩阵

    今天大一考试C语言的时候看见了这道题,下面是我转载的一个大佬的博客,自认为分析的很清楚,特来分享一下. **原文地址: https://blog.csdn.net/jack22333/article/ ...

  3. Docker Harbor 高可用 1.7.5版本(七)

    环境说明: node1 10.10.5.135 仓库 1 node2 10.10.5.136 仓库 2 node3 10.10.5.137 客户端 实验内容: Harbor 可以在两台主机之间相互同步 ...

  4. 超详细 安装VMware Workstation,并安装WIN10操作系统连接外网 步骤指导

    首先下载VMware Workstation15.1版本,我保存在迅雷链接里面,下载速度非常可观. 链接:https://pan.xunlei.com/s/VMRSt6hHMZXEmPZCm6gJcG ...

  5. Electron入门Demo之桌面应用计算器笔记(二)

    码文不易啊,转载请带上本文链接呀,感谢感谢 https://www.cnblogs.com/echoyya/p/14307996.html 在之前总结了一篇自学笔记,通过之前学习到的方法和知识,完成了 ...

  6. Gulp4.0入门和实战

    gulp4.0入门和实战 我最近遇到需要优化web的性能的任务,然后就捣鼓了一些对资源文件优化压缩的方案.由于之前的项目中有使用到gulp,所以在需要处理的web项目中也优先使用这个技术. 先聊聊gu ...

  7. STL_deque容器

    一.deque简介 deque是"double-ended queue"的缩写,和vector一样都是STL的容器,deque是双端数组,而vector是单端的. deque在接口 ...

  8. 《UML与设计原则》--第四小组

    关于设计模式与原则 一.设计模式简介 设计模式描述了软件设计过程中某一类常见问题的一般性的解决方案.而面向对象设计模式描述了面向对象设计过程中特定场景下.类与相互通信的对象之间常见的组织关系. 二.G ...

  9. Windows 2008server部署pxe启动安装windows系统

    前期准备: 需安装的角色有:AD域-DHCP服务器-DNS服务器-Windows部署服务,我是将这几个服务都安装在一台vps上,C盘50G,D盘100G 安装好后角色会列出所安装的服务,如下图: 1. ...

  10. MySQL调优性能监控之performance schema

    一.performance_schema的介绍 performance:性能 schema:图(表)示,以大纲或模型的形式表示计划或理论. MySQL的performance schema 用于监控M ...