zoukankan      html  css  js  c++  java
  • Hdu 4311-Meeting point-1 曼哈顿距离,前缀和

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4311

    Meeting point-1

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 3426    Accepted Submission(s): 1131


    Problem Description
    It has been ten years since TJU-ACM established. And in this year all the retired TJU-ACMers want to get together to celebrate the tenth anniversary. Because the retired TJU-ACMers may live in different places around the world, it may be hard to find out where to celebrate this meeting in order to minimize the sum travel time of all the retired TJU-ACMers. 
    There is an infinite integer grid at which N retired TJU-ACMers have their houses on. They decide to unite at a common meeting place, which is someone's house. From any given cell, only 4 adjacent cells are reachable in 1 unit of time.
    Eg: (x,y) can be reached from (x-1,y), (x+1,y), (x, y-1), (x, y+1).
    Finding a common meeting place which minimizes the sum of the travel time of all the retired TJU-ACMers.
     
    Input
    The first line is an integer T represents there are T test cases. (0<T <=10)
    For each test case, the first line is an integer n represents there are n retired TJU-ACMers. (0<n<=100000), the following n lines each contains two integers x, y coordinate of the i-th TJU-ACMer. (-10^9 <= x,y <= 10^9)
     
    Output
    For each test case, output the minimal sum of travel times.
     
    Sample Input
    4 6 -4 -1 -1 -2 2 -4 0 2 0 3 5 -2 6 0 0 2 0 -5 -2 2 -2 -1 2 4 0 5 -5 1 -1 3 3 1 3 -1 1 -1 10 -1 -1 -3 2 -4 4 5 2 5 -4 3 -1 4 3 -1 -2 3 4 -2 2
     
    Sample Output
    26 20 20 56
    Hint
    In the first case, the meeting point is (-1,-2); the second is (0,0), the third is (3,1) and the last is (-2,2)
     
    Author
    TJU
     
    Source
     
    Recommend
    zhuyuanchen520   |   We have carefully selected several similar problems for you:  4315 4318 4310 4313 4314 
     
    题意:给你n个点的坐标,让你找其中一个坐标,使得所有点到那个点的曼哈顿距离和最小。
    题解:
    曼哈顿距离+前缀和
    把x和y分别排序,然后去维护分别排序后的前缀和,还有前缀和的前缀和。
    注意开long long
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define MAXN 100010
     4 #define INF 10000000000000000LL
     5 #define LL long long
     6 LL qx[MAXN],qy[MAXN],qx1[MAXN],qy1[MAXN];
     7 LL ans[MAXN];
     8 struct node
     9 {
    10     int a,id;
    11 }x[MAXN],y[MAXN];
    12 int read()
    13 {
    14     int s=0,fh=1;char ch=getchar();
    15     while(ch<'0'||ch>'9'){if(ch=='-')fh=-1;ch=getchar();}
    16     while(ch>='0'&&ch<='9'){s=s*10+(ch-'0');ch=getchar();}
    17     return s*fh;
    18 }
    19 bool cmp(node aa,node bb)
    20 {
    21     return aa.a<bb.a;
    22 }
    23 int main()
    24 {
    25     int T,n,i;
    26     LL MN,sum,sum1;
    27     T=read();
    28     while(T--)
    29     {
    30         n=read();
    31         for(i=1;i<=n;i++){x[i].a=read(),y[i].a=read();x[i].id=i;y[i].id=i;}
    32         sort(x+1,x+n+1,cmp);
    33         sort(y+1,y+n+1,cmp);
    34         qx[1]=0;qy[1]=0;qx1[1]=0;qy1[1]=0;
    35         for(i=2;i<=n;i++)
    36         {
    37             qx[i]=qx[i-1]+(LL)(x[i].a-x[i-1].a);
    38             qx1[i]=qx1[i-1]+qx[i];
    39             qy[i]=qy[i-1]+(LL)(y[i].a-y[i-1].a);
    40             qy1[i]=qy1[i-1]+qy[i];
    41         }
    42         memset(ans,0,sizeof(ans));
    43         for(i=1;i<=n;i++)
    44         {
    45             sum=qx[i]*(i-1)-qx1[i-1]+qx1[n]-qx1[i]-qx[i]*(n-i);
    46             sum1=qy[i]*(i-1)-qy1[i-1]+qy1[n]-qy1[i]-qy[i]*(n-i);
    47             ans[x[i].id]+=sum;
    48             ans[y[i].id]+=sum1;
    49         }
    50         MN=INF;
    51         for(i=1;i<=n;i++)MN=min(MN,ans[i]);
    52         printf("%lld
    ",MN);
    53     }
    54     return 0;
    55 }
  • 相关阅读:
    Mac配置docker阿里云加速器
    Docker初学笔记
    Mac下载安装Tomcat
    MySQL
    monkey
    Git基本使用
    yaml语法
    PAT_B数素数 (20)
    PAT_B1002数字分类
    PAT基础编程练习
  • 原文地址:https://www.cnblogs.com/Var123/p/5355984.html
Copyright © 2011-2022 走看看