Bzoj 3170 Tjoi 2013 Squirrel Party Compute geometry

Source: Internet
Author: User

The main idea: N points on a given plane, to find the minimum of one point in the N point to the Chebyshev distance of the n points

Chebyshev, that is, the maximum of the absolute value of each coordinate difference

First of all, if we want to convert the Manhattan distance to Chebyshev distance then we have to turn the point (x, y) into a new point (x+y,x-y) where the Chebyshev distance between the points is the Manhattan distance between the origin.

Similarly, we can convert the Chebyshev distance to Manhattan distance (x, y) into ((X+y)/2, (x-y)/2).

The horizontal ordinate sort is then maintained with the prefix and the Manhattan distance of the horizontal ordinate is discussed separately

Avoid double, finally calculate distance and divide by 2

#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include < algorithm> #define M 100100#define EPS 1e-7using namespace Std;typedef long long ll;struct point{ll x,y;void Read () {int X,Y;SCANF ("%d%d", &x,&y);p oint::x=x+y;point::y=x-y;}} Points[m];int n;ll x[m],y[m],ans=1ll<<62;ll sum_x[m],sum_y[m];int main () {int i,pos;cin>>n;for (i=1;i< =n;i++) {Points[i]. Read (); x[i]=points[i].x; Y[I]=POINTS[I].Y;} Sort (x+1,x+n+1); sort (y+1,y+n+1); for (i=1;i<=n;i++) {sum_x[i]=sum_x[i-1]+x[i];sum_y[i]=sum_y[i-1]+y[i];} for (i=1;i<=n;i++) {ll temp=0;pos=lower_bound (x+1,x+n+1,points[i].x)-x;temp+= (Points[i].x*pos-sum_x[pos]) + (( Sum_x[n]-sum_x[pos])-points[i].x* (n-pos));p Os=lower_bound (Y+1,Y+N+1,POINTS[I].Y)-y;temp+= (points[i].y*pos-sum_ Y[pos]) + ((Sum_y[n]-sum_y[pos])-points[i].y* (N-pos)); Ans=min (ans,temp);} Cout<<ans/2<<endl;return 0;}


Bzoj 3170 Tjoi 2013 Squirrel Party compute geometry

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.