【LightOJ】 1349 中位数

版权声明:抱最大的希望,为最大的努力,做最坏的打算。 https://blog.csdn.net/qq_37748451/article/details/86530567
Finally Aladdin reached home, with the great magical lamp. He was happier than ever. As he was a nice boy, he wanted to share the happiness with all people in the town. So, he wanted to invite all people in town in some place such that they can meet there easily. As Aladdin became really wealthy, so, number of people was not an issue. Here you are given a similar problem.

Assume that the town can be modeled as an m x n 2D grid. People live in the cells. Aladdin wants to select a cell such that all people can gather here with optimal overall cost. Here, cost for a person is the distance he has to travel to reach the selected cell. If a person lives in cell (x, y) and he wants to go to cell (p, q), then the cost is |x-p|+|y-q|. So, distance between (5, 2) and (1, 3) is |5-1|+|2-3| which is 5. And the overall cost is the summation of costs for all people.

So, you are given the information of the town and the people, your task to report a cell which should be selected by Aladdin as the gathering point and the overall cost should be as low as possible.

Input
Input starts with an integer T (≤ 20), denoting the number of test cases.

Each case starts with a blank line. Next line contains three integers: m, n and q (1 ≤ m, n, q ≤ 50000), m and n denote the number of rows and columns of the grid respectively. Each of the next q lines contains three integers u v w (1 ≤ u ≤ m, 1 ≤ v ≤ n, 1 ≤ w ≤ 10000), meaning that there are w persons who live in cell (u, v). You can assume that there are no people in the cells which are not listed. You can also assume that each of the q lines contains a distinct cell.

Output
For each case, print the case number and the row and column position of the cell where the people should be invited. There can be multiple solutions, any valid one will do.

Sample Input
2

 

5 1 1

2 1 10

 

5 5 4

1 1 1

2 2 1

4 4 1

5 5 1

Sample Output
Case 1: 2 1

Case 2: 3 3

Note
1.      This is a special judge problem; wrong output format may cause 'Wrong Answer'.

2.      Dataset is huge, use faster I/O methods.

题目大意:给一个mxn的平面,有q个位置,每个位置坐标为(u,v)有w人,求一个点在平面内使得所有人都到这个点的曼哈顿距离之和最小(如 (x, y) 到 (p, q),那曼哈顿距离就是|x-p|+|y-q|)。

解题思路:分别按横纵坐标排序找出中位数

#include<cstdio>
#include<algorithm>
using namespace std;
const int N=5e5+5;

struct node{
    int x,y,cnt;
}a[N];

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 T;
    scanf("%d",&T);
    int cas=0;
    while(T--){
        int m,n,q,x,y;
        scanf("%d%d%d",&m,&n,&q);
        int xsum=0,ysum=0,count=0;
        for(int i=1;i<=q;i++){
            scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].cnt);
            count+=a[i].cnt;
        }
        int mid=(count+1)/2;
        //按横坐标排序 
        sort(a+1,a+q+1,cmp1);
        int sum=0;
        for(int i=1;i<=q;i++){
            sum+=a[i].cnt;
            if(sum>=mid){            
                if(count&1)
                    x=a[i].x;
                else{
                    if(sum==mid)
                        x=(a[i].x+a[i+1].x)/2;
                    else
                        x=a[i].x;
                }            
                break;
            }
        }
        //按纵坐标排序 
        sort(a+1,a+q+1,cmp2);
        sum=0;
        for(int i=1;i<=q;i++){
            sum+=a[i].cnt;
            if(sum>=mid){
                if(count&1)
                    y=a[i].y;
                else{
                    if(sum==mid)
                        y=(a[i].y+a[i+1].y)/2;
                    else
                        y=a[i].y;
                }
                    
                break;
            }
        }    
        printf("Case %d: %d %d\n",++cas,x,y);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_37748451/article/details/86530567