zoukankan      html  css  js  c++  java
  • MUTC 2 B Meeting point1 二分

    Meeting point-1

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


    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
     

    -----------------

    当前坐标*比当前坐标小的地点数-比当前坐标小的坐标之和+比当前坐标大的坐标之和-当前坐标*比当前坐标大的地点数

    -----------------

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    
    const int maxn=111111;
    const long long INF=1e18;
    
    struct POINT{
        int x;
        int y;
    }a[maxn];
    int x[maxn];
    int y[maxn];
    long long sumx[maxn];
    long long sumy[maxn];
    int n;
    long long ans;
    long long tmp;
    
    int fin(int q[],int key)
    {
        int l=1,r=n,m;
        while (l<=r)
        {
            m=(l+r)/2;
            if (q[m]==key) return m;
            if (q[m]<key) l=m+1;
            else r=m-1;
        }
        return -1;
    }
    
    int main()
    {
        int T;
        scanf("%d",&T);
        while (T--)
        {
            sumx[0]=sumy[0]=0;
            scanf("%d",&n);
            for (int i=1;i<=n;i++)
            {
                scanf("%d%d",&x[i],&y[i]);
                a[i].x=x[i];
                a[i].y=y[i];
            }
            sort(x+1,x+n+1);
            sort(y+1,y+n+1);
            for (int i=1;i<=n;i++)
            {
                sumx[i]=sumx[i-1]+x[i];
                sumy[i]=sumy[i-1]+y[i];
            }
            ans=INF;
            for (int i=1;i<=n;i++)
            {
                int idx=fin(x,a[i].x);
                int idy=fin(y,a[i].y);
                tmp=0;
                tmp+=(1LL*x[idx]*(idx-1)-sumx[idx-1]) + (sumx[n]-sumx[idx]-1LL*x[idx]*(n-idx));
                tmp+=(1LL*y[idy]*(idy-1)-sumy[idy-1]) + (sumy[n]-sumy[idy]-1LL*y[idy]*(n-idy));
                if (tmp<ans) ans=tmp;
            }
            printf("%I64d\n",ans);
        }
        return 0;
    }
    






  • 相关阅读:
    10-索引优化分析(2)
    09-索引优化分析(1)
    08-优化 SQL 步骤
    XHR 框架与 Dojo( xhrGet,xhrPut,xhrDelete)
    Win7窗口操作
    SQLServer游标详解
    Graham算法—二维点集VC++实现
    SQlserver表连接
    字符数组中将空格移到最后java实现
    笛卡儿积的java实现
  • 原文地址:https://www.cnblogs.com/cyendra/p/3226368.html
Copyright © 2011-2022 走看看