[Two points + Manhattan distance] 51nod1671 goods Transportation __ Two points

Source: Internet
Author: User

It's just a second answer, and then consider how to validate.
Meet the Bi−ai≤mid b_i-a_i \le mid point on the tube, set the transmission point built in X,y X,y, the remaining points need to meet:
| x−ai|+| Y−bi|≤mid | x-a_i|+| Y-b_i|\le mid
This is difficult to do if we find that we consider enumerating an endpoint. Can be transformed, note that the constraint above is a form of a Manhattan distance, that is to say (X,y) (X,y), (Ai,aj) (A_i,a_j) as a point, and then just ask some congruent squares whether there is a turn on the line.

#include <cstdio> #include <cstring> #include <algorithm> #define FIR a #define SEC second #define MP
(x,y) Make_pair (x,y) using namespace std;
const int maxn=500005;
int N,m,mid,ans;

Pair<int,int> A[MAXN];
    BOOL Check () {int t1=1e9,t2=-1e9,t3=1e9,t4=-1e9; for (int i=1;i<=m;i++) if (A[i]. Sec-a[i]. Fir>mid) {t1=min (T1,a[i]. Fir+a[i].
        SEC+MID); T2=max (T2,a[i]. Fir+a[i].
        SEC-MID); T3=min (T3,a[i]. Sec-a[i].
        FIR+MID); T4=max (T3,a[i]. Sec-a[i].
    FIR-MID);
Return t2<=t1&&t4<=t3;
    int main () {freopen ("51nod1671.in", "R", stdin);
    Freopen ("51nod1671.out", "w", stdout);
    scanf ("%d%d", &n,&m); for (int i=1;i<=m;i++) {scanf ("%d%d", &a[i). Fir,&a[i].
        SEC); if (a[i). Fir>a[i]. SEC) Swap (a[i). Fir,a[i].
        SEC); if (a[i). Fir==a[i].
    SEC) I--, m--;
    } if (!m) return printf ("0"), 0;
    int l=1,r=1000000;
        while (l<=r) {mid= (l+r) >>1; if (check ()) R=mid-1, Ans=mid; 
    else l=mid+1;
    printf ("%d\n", ans);
return 0;  }

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.