Solution to a problem P3964 [[TJOI2013] squirrel gathering]

Topic Link

Solution [TJOI2013] squirrel gathering

Title effect: in \ (n-\) to identify points in a point, so that all the other points to it and cut the minimum distance Chebyshev

Chebyshev distance, Manhattan distance


analysis:

First, in the trellis diagram, if went around a point \ (8 \) cost of the lattice are \ (1 \) , then so is the distance between two points Chebyshev distance

Then into each other on Chebyshev distance and Manhattan distance

A point cut than the coordinates of the Schiff coordinate system \ ((x, y) \ ) , then it coordinates the Manhattan coordinate is \ ((\ frac {x + y} {2}, \ frac { xy} {2}) \)

A point coordinate in the Manhattan distance is \ ((x, y) \ ) , then it is cut than the Chebyshev coordinates coordinate system is \ ((x + y, xy ) \)

So this question we can Chebyshev distance converted into Manhattan distance to do, because the conversion process may result in floating point precision errors occur, so we coordinate points multiplied by \ (2 \) last except \ (2 \) to

#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 100;
inline ll read(){
    ll x = 0,f = 1;char c = getchar();
    while(!isdigit(c))f = c == '-' ? -1 : f,c = getchar();
    while(isdigit(c))x = x * 10 + c - '0',c = getchar();
    return x * f;
}
struct Node{ll x,y,id;}val[maxn];
ll dis[maxn],sum[maxn],n,ans = 0x7fffffffffffffff;
inline ll query(int a,int b){return sum[b] - sum[a - 1];}
inline void getdis(){
    sort(val + 1,val + 1 + n,[](const Node &a,const Node &b){return a.x < b.x;});
    for(int i = 1;i <= n;i++)
        sum[i] = sum[i - 1] + val[i].x;
    for(int i = 1;i <= n;i++)
        dis[val[i].id] += (ll)i * val[i].x - query(1,i),dis[val[i].id] += query(i + 1,n) - ll(n - i) * val[i].x;
    sort(val + 1,val + 1 + n,[](const Node &a,const Node &b){return a.y < b.y;});
    for(int i = 1;i <= n;i++)
        sum[i] = sum[i - 1] + val[i].y;
    for(int i = 1;i <= n;i++)
        dis[val[i].id] += (ll)i * val[i].y - query(1,i),dis[val[i].id] += query(i + 1,n) - ll(n - i) * val[i].y;
}
int main(){
    n = read();
    for(int i = 1;i <= n;i++)val[i].x = read(),val[i].y = read(),val[i].id = i;
    for(int i = 1;i <= n;i++){  
        ll nx = val[i].x + val[i].y,ny = val[i].x - val[i].y;
        val[i].x = nx,val[i].y = ny;
    }
    getdis();
    for(int i = 1;i <= n;i++)
        ans = min(ans,dis[i]);
    printf("%lld\n",ans / 2);
    return 0;
}

Guess you like

Origin www.cnblogs.com/colazcy/p/11800783.html