"bzoj3210" flower-watering gathering of the gods

Source: Internet
Author: User

Converting (x, y) to (x+y,x-y) converts the Chebyshev distance into a Manhattan distance (push it yourself) A, B's Chebyshev distance is half the distance from a ' and B ' Manhattan. Then you can separate x, Y, and sort the median. Note that if the last selected optimal x and Y return is not an integer, choose an optimal scheme around the top and bottom of it.
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < Cstdio> #include <cmath>using namespace std; typedef long Long LL; #define N 100010 int n;int x, y; int x[n],y[n]; ll check (int x,int y) {    ll res (0);    for (int i=1;i<=n;i++)        res+=abs (X-x[i]) +abs (Y-y[i]);    return res;} int main () {    scanf ("%d", &n);    for (int i=1;i<=n;i++)    {        scanf ("%d%d", &x,&y);        X[i]=x+y;        y[i]=x-y;    }    Sort (x+1,x+n+1);    Sort (y+1,y+n+1);    x=x[n+1>>1];    y=y[n+1>>1];    if (~ (x ^ y) & 1)        printf ("%lld\n", check (x, y) >>1);    else        printf ("%lld\n", Min (min (check (x+1,y), check (x-1,y)), Min (check (x,y+1), check (x,y-1))), >>1);    return 0;}

  

"bzoj3210" flower-watering gathering of the gods

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.