"HDU 4311" meeting point-1 (prefix and find Manhattan distance and)

Source: Internet
Author: User

Topic links

Serious solution:

Given the coordinates of n points, find a point, and the smallest distance to other points of Manhattan.
n can be 100000.
An O (NLOGN) algorithm is probably needed.
The Euclidean distance can be calculated by dividing X and y to calculate the prefix and to determine the distance from one point to another within the O (1) time.

#include <cstdio> #include <algorithm>using namespace std; #define LL Long long#define N 100005int t,n;ll ans,    sum[n],sx[n],sy[n];struct p{int i; ll x, y;} A[n];int cmpx (P a,p b) {return a.x<b.x;} int Cmpy (P a,p b) {return a.y<b.y;}    int main () {scanf ("%d", &t);        while (t--) {sx[0]=sy[0]=0;        scanf ("%d", &n);            for (int i=1; i<=n; i++) {scanf ("%lld%lld", &a[i].x,&a[i].y);        A[i].i=i;        } sort (A+1,A+N+1,CMPX);        for (int i=1; i<=n; i++) sx[i]=sx[i-1]+a[i].x;        for (int i=1;i<=n;i++) sum[a[i].i]=a[i].x* (i-1)-sx[i-1]+sx[n]-sx[i]-a[i].x* (n-i);        Sort (a+1,a+n+1,cmpy);        for (int i=1; i<=n; i++) sy[i]=sy[i-1]+a[i].y;            for (int i=1; i<=n; i++) {sum[a[i].i]+=a[i].y* (i-1)-sy[i-1]+sy[n]-sy[i]-a[i].y* (n-i);        Ans= (i==1) sum[a[1].i]:min (ans,sum[a[i].i]);    } printf ("%lld\n", ans); }}

Narrowing the scope method:

Another way, I thought so, but later did not dare to pay, I think it will WA.
Because it is not very reliable in itself.
After sorting by X, take the near point of the middle X to calculate (after I have repeatedly submitted, found that this range can not be less than ±220, this is the difficult point, why is about 250, the game on the basis of intuition value).

#include <cstdio> #include <algorithm>using namespace std; #define LL Long longstruct p{ll x, y;} A[100005];ll ABS (ll a) {    if (a<0) a=-a;    return A;} ll DIS (p a, p b) {    return ABS (a.x-b.x) +abs (A.Y-B.Y);} int CMP (P a,p b) {    return a.x<b.x;} int main () {    int t,n,s,e;    scanf ("%d", &t);    while (t--) {        scanf ("%d", &n);        for (int i=1;i<=n;i++)            scanf ("%lld%lld", &a[i].x,&a[i].y);        Sort (a+1,a+1+n,cmp);        s=n/2-223;        e=n/2+223;        if (s<1) S=1;        if (e>n) e=n;        ll ans=1ll<<60;        for (int z=s;z<=e;z++) {            ll s=0;            for (int i=1;i<=n;i++)                S+=dis (A[z],a[i]);            Ans=min (S,ans);        }        printf ("%lld\n", ans);}    }

  

HDU 4311 Meeting point-1 (prefix and find Manhattan distance and)

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.