zoukankan      html  css  js  c++  java
  • HDU 4311 前缀和

    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)
     
    题意:n人在不同的地方,选择其中一个人的屋子,使得其他人到这个地方去的距离之和最短
     
    题解:题目要求的距离是曼哈顿距离|x1-x2|+|y1-y2|;所以可以考虑将x,y,分开计算,最后取和的max;
     如何快速处理其他位置到i位置的距离dis[i]?先将坐标排个序 记录前缀和
     可以发现相邻位置的两个i,j=i+1的距离和的关系
    dis[j]=dis[i]+(j-2)*(sumx[j]-sumx[i])-(n-j)*(sumx[j]-sumx[i]) =dis[i]+(2*i-n)*(sumx[j]-sumx[i])
     
     1 /******************************
     2 code by drizzle
     3 blog: www.cnblogs.com/hsd-/
     4 ^ ^    ^ ^
     5  O      O
     6 ******************************/
     7 //#include<bits/stdc++.h>
     8 #include<iostream>
     9 #include<cstring>
    10 #include<cstdio>
    11 #include<map>
    12 #include<algorithm>
    13 #include<cmath>
    14 #define ll long long
    15 #define PI acos(-1.0)
    16 #define mod 1000000007
    17 int t;
    18 using namespace std;
    19 struct node
    20 {
    21     ll x,y,pos;
    22 } N[100005];
    23 ll sumx[100005];
    24 ll sumy[100005];
    25 ll ans1[100005];
    26 ll ans2[100005];
    27 int exm;
    28 bool cmp1(struct node aa,struct node bb)
    29 {
    30     return aa.x<bb.x;
    31 }
    32 bool cmp2(struct node aa,struct node bb)
    33 {
    34     return aa.y<bb.y;
    35 }
    36 int main()
    37 {
    38     while(~scanf("%d",&t))
    39     {
    40         for(int i=1; i<=t; i++)
    41         {
    42             scanf("%d",&exm);
    43             memset(N,0,sizeof(N));
    44             memset(ans1,0,sizeof(ans1));
    45             memset(ans2,0,sizeof(ans2));
    46             for(int j=1; j<=exm; j++)
    47             {
    48                 scanf("%I64d %I64d",&N[j].x,&N[j].y);
    49                 N[j].pos=j;
    50             }
    51             sort(N+1,N+1+exm,cmp1);
    52             sumx[1]=0;
    53             for(int j=2; j<=exm; j++)
    54             {
    55                 sumx[j]=sumx[j-1]+N[j].x-N[j-1].x;
    56                 ans1[N[1].pos]+=sumx[j];
    57             }
    58             for(int j=2; j<=exm; j++)
    59             {
    60 
    61                 ans1[N[j].pos]=ans1[N[j-1].pos]-(exm-j)*(sumx[j]-sumx[j-1])+(j-2)*(sumx[j]-sumx[j-1]);
    62             }
    63             sort(N+1,N+1+exm,cmp2);
    64             sumy[1]=0;
    65             for(int j=2; j<=exm; j++)
    66             {
    67                 sumy[j]=sumy[j-1]+N[j].y-N[j-1].y;
    68                 ans2[N[1].pos]+=sumy[j];
    69             }
    70             for(int j=2; j<=exm; j++)
    71             {
    72                 ans2[N[j].pos]=ans2[N[j-1].pos]-(exm-j)*(sumy[j]-sumy[j-1])+(j-2)*(sumy[j]-sumy[j-1]);
    73             }
    74             ll maxn=ans1[1]+ans2[1];
    75             for(int j=1; j<=exm; j++)
    76             {
    77                 if(ans1[j]+ans2[j]<maxn)
    78                 {
    79                     maxn=ans1[j]+ans2[j];
    80                 }
    81             }
    82             printf("%I64d
    ",maxn);
    83         }
    84     }
    85     return 0;
    86 }
  • 相关阅读:
    【网易官方】极客战记(codecombat)攻略-游戏开发1-漫游者危险
    【网易官方】极客战记(codecombat)攻略-游戏开发1-军事训练
    【网易官方】极客战记(codecombat)攻略-游戏开发1-取舍
    【网易官方】极客战记(codecombat)攻略-游戏开发1-碾碎它
    【网易官方】极客战记(codecombat)攻略-游戏开发1-斩首老鼠
    课程报名 | 5G时代的视频云服务关键技术与实践
    干货 | 京东云账号安全管理最佳实践
    这大概是今年介绍云原生最清晰明了的文章!
    “大促”背后的技术 | 当我们说促销的时候,我们在谈什么?
    技术沙龙 | 从高并发架构到企业级区块链探索零售创新
  • 原文地址:https://www.cnblogs.com/hsd-/p/5726546.html
Copyright © 2011-2022 走看看