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

即是切比雪夫距离。而切比雪夫距离与曼哈顿距离的转换却很巧妙。
把平面坐标所有点绕原点逆向旋转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的更多相关文章
- HDU 4312 Meeting point-2(切比雪夫距离转曼哈顿距离)
http://acm.hdu.edu.cn/showproblem.php?pid=4312 题意:在上一题的基础上,由四个方向改为了八个方向. 思路: 引用自http://blog.csdn.net ...
- HDU 5045 Contest(状压DP)
Problem Description In the ACM International Collegiate Programming Contest, each team consist of th ...
- hdu - 5045 - Contest(国家压缩dp)
意甲冠军:N个人M通过主打歌有自己的期望,每个问题发送人玩.它不能超过随机播放的次数1,追求最大业绩预期 (1 ≤ N ≤ 10,1 ≤ M ≤ 1000). 主题链接:pid=5045" ...
- [ACM] hdu 5045 Contest (减少国家Dp)
Contest Problem Description In the ACM International Collegiate Programming Contest, each team consi ...
- HDU 4311 Meeting point-1 && HDU 4312 Meeting point-2
这俩个题 题意::给出N(<1e5)个点求找到一个点作为聚会的地方,使每个点到达这里的距离最小.4311是 曼哈顿距离 4312是 切比雪夫距离: 曼哈顿距离 :大家都知道 对于二维坐标系a( ...
- HDU–5988-Coding Contest(最小费用最大流变形)
Coding Contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- hdu 5045 Contest(状态压缩DP)
题解:我们使用一个二位数组dp[i][j]记录进行到第i个任务时,人组合为j时的最大和(这里的j我们用二进制的每位相应一个人). 详细见代码: #include <iostream> #i ...
- HDU 5045 Contest
pid=5045">主题链接~~> 做题感悟:比赛时这题后来才写的,有点小尴尬.两个人商议着写写了非常久才写出来,I want to Powerful ,I believe me ...
- HDU 4335 Contest 4
利用降幂公式..呃,还是自己去搜题解吧.知道降幂公式后,就不难了. #include <iostream> #include <cstdio> #include <alg ...
随机推荐
- 洛谷—— P1457 城堡 The Castle
https://www.luogu.org/problem/show?pid=1457 题目描述 我们憨厚的USACO主人公农夫约翰(Farmer John)以无法想象的运气,在他生日那天收到了一份特 ...
- Unity3D 人形血条制作小知识
这几天用Unity3D做个射击小游戏,想做个人形的血条.百思不得其解,后来问了网上的牛牛们,攻克了,事实上挺简单的,GUI里面有个函数DrawTextureWithTexCoords就能够实现图片的裁 ...
- 使用rsync同步数据(by quqi99)
作者:张华 发表于:2015-12-28版权声明:能够随意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 ( http://blog.csdn.net/quqi99 ) 急需使 ...
- 2015.04.21,外语,读书笔记-《Word Power Made Easy》 11 “如何辱骂敌人” SESSION 31
1.no reverence iconoclast([ai'kɔnәklæst] n. 毁坏宗教神像的人, 提倡打破旧习的人)藐视传统.在青年的反叛期很容易出现iconoclasm([ai'kɔnә ...
- hdoj--1201--18岁生日(模拟)
18岁生日 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- 4.QList
#include "mainwindow.h" #include <QApplication> #include <QLabel> #include < ...
- Session会在浏览器关闭后消失吗?
转 http://blog.csdn.net/rongwenbin/article/details/51784310 Cookie的两种类型 在项目开发中我们时常将需要在客户端(浏览器)缓存的数 ...
- c++ set_union set_intersection使用
自定义类型也可以构造set,但同样必须定义“小于”运算符,set中的元素从小到大排列好了 #include<iostream>#include<string>#include ...
- 转:Redis介绍及常用命令大全
一 Redis介绍 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开发 ...
- 转:如何在Ubuntu 14.04中安装最新版Eclipse
想必很多开发人员都知道,Ubuntu 软件源中提供的并不是最新版本的 Eclipse,本教程就教大家如何在 Ubuntu 14.04 中快速安装 Eclipse 官方发布的最新版本. 到目前为止,Ec ...