HDOJ 4311——並べ替え&数学式

5092 ワード

Meeting point-1
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2605    Accepted Submission(s): 814
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. (0For each test case, the first line is an integer n represents there are n retired TJU-ACMers. (0 
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
2012 Multi-University Training Contest 2
 
Recommend
zhuyuanchen520
 
それぞれx,yで並べ替えてdxとdyを求め,dx+dyの最小値を取ればよい.
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>

using namespace std;
//#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
//#define mid ((l + r) >> 1)
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 100000 + 50;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x3f3f3f3f;
const int IMIN = 0x80000000;
const double E = 2.718281828;
#define eps 1e-8
#define DEBUG 1
#define mod 100000007
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pi;
//    #pragma comment(linker, "/STACK:102400000,102400000")__int64 a[10050];
int n;
struct Node
{
    LL x , y , dx , dy;
}a[MAXN];
bool cmpx(Node a , Node b)
{
    if(a.x == b.x)return a.y < b.y;
    return a.x < b.x;
}
bool cmpy(Node a , Node b)
{
    if(a.y == b.y)return a.x < b.x;
    return a.y < b.y;
}

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        scanf("%d" , &n);
        FOR(i , 0 , n)
        {
            scanf("%I64d%I64d" , &a[i].x , &a[i].y);
            a[i].dx = a[i].dy = 0;
        }
        sort(a,a  + n, cmpx);
        FOR(i , 1 , n)a[0].dx += a[i].x - a[0].x;
        FOR(i, 0 , n -1)a[i + 1].dx = a[i].dx - (n - 2 * i - 2) * (a[i + 1].x - a[i].x);
        sort(a, a + n , cmpy);
        FOR(i , 1 , n)a[0].dy += a[i].y - a[0].y;
        FOR(i , 0 , n - 1)a[i + 1].dy = a[i].dy - (n - 2 * i - 2) * (a[i + 1].y - a[i].y);
        FOR(i ,0 ,n)a[i].dx += a[i].dy;
        LL ans  = a[0].dx;
        FOR(i ,1 , n)
        {

            if(ans > a[i].dx)ans = a[i].dx;
        }
        printf("%I64d
" , ans); } return 0; }