Railway Station Site Selection-Byte Beat Written Test

                                           Station location

This question is a question for Bytedance 2020.9.20

Title description:

City A plans to build a new railway station to facilitate citizens' travel.

The road here is very regular, and the distance of citizens from location (x1, y1) to location (x2, y2) is |x1-x2|+|y1-y2|

After preliminary inspections, M locations where railway stations can be built were initially selected.

In order to save as much time as possible for citizens to reach the railway station, the mayor hopes that the total distance between the newly-built station and each citizen is as small as possible.

Please help the mayor choose the best location for the new train station.

Enter description:

There are two integers N and M in the first line; they respectively represent the number of citizens and the location where the railway station can be built

In the next N lines, each line contains 2 integers x_i, y_i, indicating that the location of the i-th citizen is (x_i, y_i)

The next M lines, each line has 2 integers p_i, q_i, indicating that the candidate position of the i-th train waiting station is at (p_i, q_i)

1 <= N <= 100000

1 <= M <= 100000

-10000000 <= x_i , y_i , p_i , q_i <= 10000000

Output description:

Two integers p_i and q_i represent the most suitable location for the newly built railway station. If there are multiple answers, output the first one in the original data

Sample input:

4 3
-1 -1
-1 1
1 -1
1 1
3 2
1 0
0 0

Sample output:

1 0

Problem analysis:

First of all, let’s look at the data n and m are 1e5 on this question, and x, y, p, q are 1e7 respectively. Obviously we can’t do it with violence.

We thought of the original x_i and y_i, which are actually independent in actual calculations. What needs to be found is the final sum, so we can divide x and y into two arrays and sort them. So after we get the ordered x and y, if we are given a set of p and q, how to calculate the total cost?

Think of prefix and

For p, we find the position L1 that is smaller than p in x and the position L2 that is larger than p. Then the contribution smaller than p is (L1*p)-sum[L1];

sum is the prefix sum, and the contribution greater than p is sum[n]-sum[L2]-(n-L2)*p;

#include<bits/stdc++.h>
#define mod 1000000007
using namespace std;

int xx[100005],yy[10005];
long long sumx[100005],sumy[100005];
int x,y;
int main()
{
    long long minx=9223372036854775807;
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++){
        scanf("%d%d",&x,&y);
        xx[i]=x;
        yy[i]=y;
    }
    sort(xx,xx+n);
    sort(yy,yy+n);
    sumx[0]=xx[0];
    sumy[0]=yy[0];
    for(int i=1;i<n;i++){
        sumx[i]=sumx[i-1]+xx[i];
        sumy[i]=sumy[i-1]+yy[i];
    }
    int ansx,ansy;
    long long now;
    for(int i=0;i<m;i++){
        scanf("%d%d",&x,&y);
        now=0;
        int ll;
        ll=lower_bound(xx,xx+n,x)-xx;
        now+=(ll)*x-sumx[ll-1];
        ll=lower_bound(yy,yy+n,y)-yy;
        now+=(ll)*y-sumy[ll-1];
        ll=upper_bound(xx,xx+n,x)-xx;
        if(ll<n)now+=(sumx[n-1]-sumx[ll-1])-(n-ll)*x;
        ll=upper_bound(yy,yy+n,y)-yy;
        if(ll<n)now+=(sumy[n-1]-sumy[ll-1])-(n-ll)*y;
        if(now<minx){
            ansx=x;
            ansy=y;
            minx=now;
        }
    }
    cout<<ansx<<' '<<ansy<<endl;
    return 0;
}

 

Guess you like

Origin blog.csdn.net/qq_36909245/article/details/108699317