[Two points] Sort Jzoj P1792 The garden of the bishop

Source: Internet
Author: User
Tags abs

Description "Problem Background"
LHX has been plagued by many people who have come to worship him recently, so he added a barrier to his garden.

"Problem description"
The area near the leader's garden can be pumped into a network of square grids, each corresponding to a coordinate (all integers, possibly negative), if two meshes (x1, y1), (x2, y2) have |x1–x2| + |y1–y2| = 1, the two meshes are adjacent, otherwise they are not adjacent.
The leader sets a barrier on the grid at y = 0 for the entire line, which is a grid of all coordinates (x, 0). Of course, he also has to solve his own and internal personnel access problems, so the Bishop set n entrance A1, A2, ..., an accessible, that is, for all meshes on y = 0, only (A1, 0), (A2, 0), ..., (an, 0) can be passed, except that all grids with an ordinate of 0 cannot pass, And for a grid (x, y) with y not 0, it can be considered as random.
Now the leader wants to know that given m-point pairs (x1, y1), (x2, y2), and these points are not on the barrier, ask the shortest distance from one point to another point, each time only from one lattice to the adjacent lattice. the 1th behavior of input inputs is a positive integer n, which is the number of ingress on the barrier.
The 2nd line has n integers, a1, A2, ..., an, separated by a space for the horizontal axis of the N inlet.
The 3rd act is a positive integer m, which represents M queries.
Next m line, 4 integers per line x1, y1, x2, y2, y1 and y2 are not equal to 0, indicating a query from (x1, y1) to (x2, y2) the shortest path. The output outputs contain a total of M lines, and the first line of the output is the shortest distance from (x1, y1) to (x2, y2) of the I-query outputs. Sample Input
22-120 1 0-11 1 2 2
Sample Output
42
Hint "Data Size"
For 20% of the data, there is n,m≤10,ai,xi,yi absolute value of not more than 100;
For 40% of the data, there is n,m≤100,ai,xi,yi absolute value of not more than 1000;
For 60% of the data, there is n,m≤1000,ai,xi,yi absolute value of not more than 100000;
For 100% of the data, there is a n,m≤100000,ai,xi,yi absolute value of not more than 100000000. Exercises
    • The main idea is that in a planar Cartesian coordinate system, the x-axis is a barrier, but there are n openings, which require two points of distance
    • Obviously, this question can be discussed by category.
    • ① the y-coordinate of the two points, directly seeking the Manhattan distance output
    • If not, then two points in the middle of the opening
    • ② if the opening is in the middle of two points, two points are added to the open Manhattan distance and output
    • ③ Otherwise, determine which endpoint it is close to, and then go around it.
    • Although the POS value at this point is not necessarily accurate, but very close, the real value must appear in the pos-1,pos,pos+1, to find the minimum output is good
Code
1#include <cstdio>2#include <iostream>3#include <algorithm>4 using namespacestd;5 intn,m,a[100010];6 intAbsintA) {returna<0?-a:a;}7 intFindintx)8 {9     intL=1, r=n,k=N;Ten      while(l<=R) One     { A         intMid= (L+R)/2; -         if(a[mid]<x) l=mid+1;Elser=mid-1, k=mid; -     } the     returnK; - }  - intMain () - { +     intX1,x2,y1,y2; -scanf"%d",&n); +      for(intI=1; i<=n;i++) scanf ("%d",&a[i]); ASort (A +1, a+n+1); atscanf"%d",&m); -      for(intI=1; i<=m;i++) -     { -scanf"%d%d%d%d",&x1,&y1,&x2,&y2); -         if((y1<0&&y2<0)|| (y1>0&&y2>0)) -         { inprintf"%d\n", ABS (Y1-Y2) +abs (x1-x2)); -             Continue; to         } +         if(x1>x2) Swap (X1,X2); -         intPos=find ((X1+X2)/2); the         if(a[pos]>=x1&&a[pos]<=x2) printf ("%d\n", ABS (Y1-Y2) +abs (x1-x2)); *         Else $         {Panax Notoginseng             intT1=abs (a[pos]-x1) +abs (a[pos]-x2) +abs (y1-y2), t2=0x7fffffff, t3=0x7fffffff; -             if(pos>1) T2=abs (a[pos-1]-X1) +abs (a[pos-1]-X2) +abs (y1-y2); the             if(pos<n) T3=abs (a[pos+1]-X1) +abs (a[pos+1]-X2) +abs (y1-y2); +             if(T1&GT;T2) t1=T2; A             if(T1&GT;T3) t1=T3; theprintf"%d\n", T1); +         } -     } $}

[dichotomy] [sort] Jzoj P1792 's garden

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.