BZOJ 3170: [Tjoi 2013]松鼠聚会 切比雪夫距离
生活随笔
收集整理的這篇文章主要介紹了
BZOJ 3170: [Tjoi 2013]松鼠聚会 切比雪夫距离
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
3170: [Tjoi 2013]松鼠聚會
Time Limit: 1 Sec ?
Memory Limit: 256 MB
題目連接
http://www.lydsy.com/JudgeOnline/problem.php?id=3170Description
有N個小松鼠,它們的家用一個點x,y表示,兩個點的距離定義為:點(x,y)和它周圍的8個點即上下左右四個點和對角的四個點,距離為1?,F(xiàn)在N個松鼠要走到一個松鼠家去,求走過的最短距離。
Input
第一行給出數(shù)字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
20HINT
題意
?
題解:
題目給的切比雪夫距離,轉(zhuǎn)化成曼哈頓距離就好了
然后利用前綴和統(tǒng)計一下就行了
代碼:
//qscqesze #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <bitset> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define maxn 100010 #define eps 1e-9 int Num; //const int inf=0x7fffffff; //§ß§é§à§é¨f§3 const int inf=0x3f3f3f3f; inline ll read() {ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f; } //**************************************************************************************struct node {double x,y;int id; }p[maxn]; double ans[maxn]; bool cmp1(node a,node b) {return a.x<b.x; } bool cmp2(node a,node b) {return a.y<b.y; }int main() {int n=read();double sumx=0,sumy=0;for(int i=0;i<n;i++){double x,y;scanf("%lf%lf",&x,&y);p[i].x = (x+y)/2;p[i].y = (x-y)/2;sumx += p[i].x;sumy += p[i].y;p[i].id = i;}sort(p,p+n,cmp1);double tmp=0;for(int i=0;i<n;i++){ans[p[i].id]+=(i)*p[i].x - tmp;ans[p[i].id]+=(sumx-tmp)-(n-i)*p[i].x;tmp+=p[i].x;}sort(p,p+n,cmp2);tmp=0;for(int i=0;i<n;i++){ans[p[i].id]+=(i)*p[i].y - tmp;ans[p[i].id]+=(sumy-tmp)-(n-i)*p[i].y;tmp+=p[i].y;}double Ans = ans[0];for(int i=0;i<n;i++)Ans = min(Ans,ans[i]);printf("%.0lf\n",Ans); }?
總結(jié)
以上是生活随笔為你收集整理的BZOJ 3170: [Tjoi 2013]松鼠聚会 切比雪夫距离的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: idea如何导出maven项目
- 下一篇: Apache Spark 1.5发布,新