bzoj-3170 3170: [Tjoi 2013]松鼠聚会(计算几何)
2016-08-24 18:26
351 查看
题目链接:
下面N行,每行给出x,y表示其家的坐标。
-10^9<=x,y<=10^9
-4 -1
-1 -2
2 -4
0 2
0 3
5 -2
题意:
给出n个点,选出一个点,求其他点到这一点的切比雪夫距离和的最小值;
思路:
切比雪夫距离可以转化成曼哈顿距离,把原来的坐标系旋转45度,
原来的距离为max{|x1-x2|,|y1-y2|};
旋转后的坐标可以表示为(x1-y1,x1+y1)
2*max{|x1-x2|,|y1-y2|}=|x1-y1-(x2-y2)|+|x1+y1-(x2+y2)|;
曼哈顿距离和的最小值可以x,y排序前缀和;这样就解决了;
AC代码:
3170: [Tjoi 2013]松鼠聚会
Time Limit: 10 Sec Memory Limit: 128 MBDescription
有N个小松鼠,它们的家用一个点x,y表示,两个点的距离定义为:点(x,y)和它周围的8个点即上下左右四个点和对角的四个点,距离为1。现在N个松鼠要走到一个松鼠家去,求走过的最短距离。Input
第一行给出数字N,表示有多少只小松鼠。0<=N<=10^5下面N行,每行给出x,y表示其家的坐标。
-10^9<=x,y<=10^9
Output
表示为了聚会走的路程和最小为多少。Sample Input
6-4 -1
-1 -2
2 -4
0 2
0 3
5 -2
Sample Output
20题意:
给出n个点,选出一个点,求其他点到这一点的切比雪夫距离和的最小值;
思路:
切比雪夫距离可以转化成曼哈顿距离,把原来的坐标系旋转45度,
原来的距离为max{|x1-x2|,|y1-y2|};
旋转后的坐标可以表示为(x1-y1,x1+y1)
2*max{|x1-x2|,|y1-y2|}=|x1-y1-(x2-y2)|+|x1+y1-(x2+y2)|;
曼哈顿距离和的最小值可以x,y排序前缀和;这样就解决了;
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <bits/stdc++.h> #include <stack> #include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++) #define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) { char CH; bool F=false; for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar()); for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar()); F && (num=-num); } int stk[70], tp; template<class T> inline void print(T p) { if(!p) { puts("0"); return; } while(p) stk[++ tp] = p%10, p/=10; while(tp) putchar(stk[tp--] + '0'); putchar('\n'); } const LL mod=1e9+7; const double PI=acos(-1.0); const LL inf=1e18; const int N=1e5+10; const int maxn=1e3+20; const double eps=1e-12; struct node { int id; LL x,y; }po ; int cmp(node a,node b) { return a.x<b.x; } int cmp1(node a,node b) { return a.y<b.y; } LL pre ,nex ,sumx ,sumy ; int main() { int n; LL x,y; read(n); For(i,1,n) { read(x);read(y); po[i].x=x-y; po[i].y=x+y; po[i].id=i; } sort(po+1,po+n+1,cmp); for(int i=1;i<=n;i++)pre[i]=pre[i-1]+(i-1)*(po[i].x-po[i-1].x); for(int i=n;i>0;i--)nex[i]=nex[i+1]+(n-i)*(po[i+1].x-po[i].x); for(int i=1;i<=n;i++)sumx[po[i].id]=pre[i]+nex[i]; sort(po+1,po+n+1,cmp1); for(int i=1;i<=n;i++)pre[i]=pre[i-1]+(i-1)*(po[i].y-po[i-1].y); for(int i=n;i>0;i--)nex[i]=nex[i+1]+(n-i)*(po[i+1].y-po[i].y); for(int i=1;i<=n;i++)sumy[po[i].id]=pre[i]+nex[i]; LL ans=inf; for(int i=1;i<=n;i++)ans=min(ans,sumx[i]+sumy[i]); cout<<ans/2<<endl; return 0; }
相关文章推荐
- BZOJ 3170 Tjoi 2013 松鼠聚会 计算几何
- bzoj 3170: [Tjoi 2013]松鼠聚会 计算几何
- BZOJ_3170_[Tjoi2013]松鼠聚会_切比雪夫距离
- bzoj 3170: [Tjoi 2013]松鼠聚会
- bzoj3170【TJOI2013】松鼠聚会
- BZOJ 3170 TJOI 2013 松鼠聚会 切比雪夫距离
- 【BZOJ3170】[Tjoi 2013]松鼠聚会 旋转坐标系
- BZOJ 3170: [Tjoi 2013]松鼠聚会 切比雪夫距离
- BZOJ 3170 [Tjoi 2013]松鼠聚会 切比雪夫距离-->曼哈顿距离
- BZOJ 3170([Tjoi 2013]松鼠聚会-Manhattan距离单点最近)
- 【bzoj3170】[Tjoi 2013]松鼠聚会 旋转坐标系
- bzoj 3170 Tjoi 2013 松鼠聚会 曼哈顿距离&&切比雪夫距离
- [BZOJ 3170][Tjoi 2013]松鼠聚会
- BZOJ 3170: [Tjoi 2013]松鼠聚会(切比雪夫距离)
- 【bzoj3170】[Tjoi 2013]松鼠聚会
- BZOJ 3170: [Tjoi 2013]松鼠聚会
- BZOJ 3170([Tjoi 2013]松鼠聚会-Manhattan距离单点最近)
- BZOJ3170: [Tjoi 2013]松鼠聚会
- BZOJ 3170 [Tjoi 2013]松鼠聚会
- BZOJ3170[TJOI2013]松鼠聚会