P3964 [TJOI2013]松鼠聚会【切比雪夫距离】

P3964 [TJOI2013]松鼠聚会

题意:给出 n n 个点 ( x i , y i ) (x_i,y_i) ,找到某个点,使得所有点到该点的切比雪夫距离和最小。

  • 一、
    A ( x 1 , y 1 ) , B ( x 2 , y 2 ) A(x_1, y_1), B(x_2, y_2)
    三种距离:
    ①欧几里得距离: ( x 2 x 1 ) 2 + ( y 2 y 1 ) 2 \sqrt{(x_2-x_1)^2 + (y_2-y_1)^2}
    ②曼哈顿距离: x 2 x 1 + y 2 y 1 |x_2-x_1|+|y_2-y_1|
    ③切比雪夫距离: m a x ( x 2 x 1 , y 2 y 1 ) max(|x_2-x_1|,|y_2-y_1|)
  • 二、
    相同距离
    曼哈顿距离 ( x , y ) (x,y) 转化为切比雪夫距离: ( x + y , x y ) (x+y,x-y)
    切比雪夫距离 ( x , y ) (x,y) 转化为曼哈顿距离: ( x + y 2 , x y 2 ) (\displaystyle \frac{x + y}{2}, \frac{x-y}{2})
  • 三、
    对于曼哈顿距离:
    当前枚举到点   i : ( x i , y i ) \ i:(x_i,y_i) ,那么所有点到该点的距离: j = 1 n x i x j + j = 1 n y i y j \displaystyle \sum^{n}_{j=1}{|x_i-x_j|}+\displaystyle \sum^{n}_{j=1}{|y_i-y_j|}
  1. 如果我们将所有横坐标按照升序排序
    d i s x = j = 1 i ( x i x j ) + j = i + 1 n ( x j x i ) dis_x=\displaystyle \sum^{i}_{j=1}{(x_i-x_j)}+\displaystyle \sum^{n}_{j=i+1}{(x_j-x_i)}
  2. 同理,如果我们将所有纵坐标按照升序排序
    d i s y = j = 1 i ( y i y j ) + j = i + 1 n ( y j y i ) dis_y=\displaystyle \sum^{i}_{j=1}{(y_i-y_j)}+\displaystyle \sum^{n}_{j=i+1}{(y_j-y_i)}
    发现了啥?这不是前缀和吗?
    d i s x = ( i x i + p r e f i x [ x i ] ) + ( p r e f i x [ x n ] p r e f i x [ x i ] + ( n i ) x i ) dis_x=(i*x_i+prefix[x_i])+(prefix[x_n]-prefix[x_i]+(n-i)*x_i)
    d i s y = ( i y i + p r e f i x [ y i ] ) + ( p r e f i x [ y n ] p r e f i x [ y i ] + ( n i ) y i ) dis_y=(i*y_i+prefix[y_i])+(prefix[y_n]-prefix[y_i]+(n-i)*y_i)

由此可得AC代码~

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

ll read()
{
    ll x = 0, f = 1; char c = getchar();
    while(c < '0' || c > '9') { if(c == '-') f = -f; c = getchar(); }
    while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
    return x * f;
}

const int maxN = 100005;

struct node{
    ll x, y;
    int id;
    node() {}
    node(ll a, ll b, int c) : x(a), y(b), id(c) {}
}mp[maxN];

bool cmp_x(node n1, node n2) { return n1.x < n2.x; }
bool cmp_y(node n1, node n2) { return n1.y < n2.y; }

ll n;

ll ans[maxN];
ll sum[maxN];

int main()
{
    n = read();
    for(int i = 1; i <= n; ++ i )
    {
        mp[i].x = read(); mp[i].y = read();
        ll xx = mp[i].x + mp[i].y, yy = mp[i].x - mp[i].y;
        mp[i] = node(xx, yy, i);
    }
    sort(mp + 1, mp + n + 1, cmp_x);
    for(int i = 1; i <= n; ++ i )
        sum[i] = sum[i - 1] + mp[i].x;
    for(int i = 1; i <= n; ++ i )
        ans[mp[i].id] = (i * mp[i].x - sum[i] + sum[n] - sum[i] - (n - i) * mp[i].x);
    sort(mp + 1, mp + n + 1, cmp_y);
    for(int i = 1; i <= n; ++ i )
        sum[i] = sum[i - 1] + mp[i].y;
    for(int i = 1; i <= n; ++ i )
        ans[mp[i].id] += (i * mp[i].y - sum[i] + sum[n] - sum[i] - (n - i) * mp[i].y);
    ll res = ans[1];
    for(int i = 2; i <= n; ++ i )
        res = min(res, ans[i]);
    cout << (res >> 1) << endl;
    return 0;
}
发布了273 篇原创文章 · 获赞 76 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_44049850/article/details/105206411