题目要求两点间的最大值作为距离即:

即是切比雪夫距离。而切比雪夫距离与曼哈顿距离的转换却很巧妙。

把平面坐标所有点绕原点逆向旋转45度后,所得点的曼哈顿距离之和除以√2,即是切雪比夫距离。旋转点的公式是

提取无理数,即每个新坐标可以是(x-y,x+y)。计算其曼哈顿距离后除以2即可。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define LL __int64
using namespace std; struct point{
int x,y;
int num;
}acm[100005];
int n; bool cmpx(point a, point b){
if(a.x<b.x) return true;
return false;
}
bool cmpy(point a,point b){
if(a.y<b.y) return true;
return false;
} LL distx[100005],disty[100005]; LL mint(LL a,LL b){
if(a<b) return a;
return b;
} void init(){
for(int i=0;i<n;i++)
distx[i]=disty[i]=0;
} int main(){
int T,a,b;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
init();
for(int i=0;i<n;i++){
scanf("%d%d",&a,&b);
acm[i].x=a-b; acm[i].y=a+b;
acm[i].num=i;
}
LL tmp;
sort(acm,acm+n,cmpx);
tmp=0;
for(int i=0;i<n;i++){
if(!i)
tmp=distx[acm[i].num]=0;
else{
distx[acm[i].num]=tmp=tmp+(LL)i*((LL)acm[i].x-(LL)acm[i-1].x);
}
}
tmp=0;
for(int i=n-1;i>=0;i--){
if(i==n-1){
tmp=0;
distx[acm[i].num]+=tmp;
}
else {
tmp=tmp+(LL)(n-1-i)*((LL)acm[i+1].x-(LL)acm[i].x);
distx[acm[i].num]+=tmp;
}
}
sort(acm,acm+n,cmpy);
for(int i=0;i<n;i++){
if(!i)
disty[acm[i].num]=tmp=0;
else{
disty[acm[i].num]=tmp=tmp+(LL)i*((LL)acm[i].y-(LL)acm[i-1].y);
}
}
for(int i=n-1;i>=0;i--){
if(i==n-1){
tmp=0;
disty[acm[i].num]+=tmp;
}
else {
tmp=tmp+(LL)(n-1-i)*((LL)acm[i+1].y-(LL)acm[i].y);
disty[acm[i].num]+=tmp;
}
}
LL ans=distx[0]+disty[0];
for(int i=1;i<n;i++)
ans=mint(ans,distx[i]+disty[i]);
printf("%I64d\n",ans/2);
}
return 0;
}

  

HDU 4312 Contest 2的更多相关文章

  1. HDU 4312 Meeting point-2(切比雪夫距离转曼哈顿距离)

    http://acm.hdu.edu.cn/showproblem.php?pid=4312 题意:在上一题的基础上,由四个方向改为了八个方向. 思路: 引用自http://blog.csdn.net ...

  2. HDU 5045 Contest(状压DP)

    Problem Description In the ACM International Collegiate Programming Contest, each team consist of th ...

  3. hdu - 5045 - Contest(国家压缩dp)

    意甲冠军:N个人M通过主打歌有自己的期望,每个问题发送人玩.它不能超过随机播放的次数1,追求最大业绩预期 (1 ≤ N ≤ 10,1 ≤ M ≤ 1000). 主题链接:pid=5045" ...

  4. [ACM] hdu 5045 Contest (减少国家Dp)

    Contest Problem Description In the ACM International Collegiate Programming Contest, each team consi ...

  5. HDU 4311 Meeting point-1 && HDU 4312 Meeting point-2

    这俩个题  题意::给出N(<1e5)个点求找到一个点作为聚会的地方,使每个点到达这里的距离最小.4311是 曼哈顿距离 4312是 切比雪夫距离: 曼哈顿距离 :大家都知道 对于二维坐标系a( ...

  6. HDU–5988-Coding Contest(最小费用最大流变形)

    Coding Contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  7. hdu 5045 Contest(状态压缩DP)

    题解:我们使用一个二位数组dp[i][j]记录进行到第i个任务时,人组合为j时的最大和(这里的j我们用二进制的每位相应一个人). 详细见代码: #include <iostream> #i ...

  8. HDU 5045 Contest

    pid=5045">主题链接~~> 做题感悟:比赛时这题后来才写的,有点小尴尬.两个人商议着写写了非常久才写出来,I want to Powerful ,I believe me ...

  9. HDU 4335 Contest 4

    利用降幂公式..呃,还是自己去搜题解吧.知道降幂公式后,就不难了. #include <iostream> #include <cstdio> #include <alg ...

随机推荐

  1. linux:共享内存

    #include <sys/ipc.h> #include <sys/shm.h> #include <string.h> #include <stdio.h ...

  2. linux删除多行

    光标放到行dd:删除所在行 光标放到行Ndd: 删除所在行下的N行

  3. Ubuntu+caffe训练cifar-10数据集

    1. 下载cifar-10数据库 ciffar-10数据集包含10种物体分类,50000张训练图片,10000张测试图片. 在终端执行指令下载cifar-10数据集(二进制文件): cd ~/caff ...

  4. DB-MySQL:MySQL 正则表达式

    ylbtech-DB-MySQL:MySQL 正则表达式 1.返回顶部 1. MySQL 正则表达式 在前面的章节我们已经了解到MySQL可以通过 LIKE ...% 来进行模糊匹配. MySQL 同 ...

  5. kafka windows安装 命令行下使用测试

    1.zookeeper安装:   (https://zookeeper.apache.org/releases.html) ①进入zookeeper的相关设置所在的文件目录,例如本文的:D:\bigd ...

  6. 在64位linux上编译32位程序 for i386 intel

    编辑中 # ld -V GNU ld version 2.15.92.0.2 20040927 Supported emulations: elf_x86_64 elf_i386 i386linux ...

  7. Vue模拟酷狗APP问题总结

    一.NewSongs.vue中的38行  this.$http.get('/proxy/?json=true')   里面这个路径的获取 二.router文件夹中的index.js  中的  comp ...

  8. python 3.x 学习笔记17(协程以及I/O模式)

    1.协程(微线程)协程是一种用户态的轻量级线程.协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来的时候,恢复先前保存的寄存器上下文和栈.因此: 协程能保留上一 ...

  9. Function 和 eval 知识点总结

    1 Function 1.1 函数的创建方式 1 函数声明 2 函数表达式 3 new Function // 1 function foo() {} // 2 var foo = function( ...

  10. zeromq-4.1.2在windows下的编译

    作者:朱金灿 来源:http://blog.csdn.net/clever101 zeromq是一个最近比较火的跨平台消息中间件,最近准备研究它,故下载它的源码编译了一下.我是使用VS2008编译的, ...