# 松鼠聚会（洛谷-P3964）

## 题目描述

30%的数据，0 ≤ N ≤ 1000

100%的数据，0 ≤ N ≤ 100000; −10^9 ≤ x, y ≤ 10^9

## 输入输出样例

6
-4 -1
-1 -2
2 -4
0 2
0 3
5 -2

20

6
0 0
2 0
-5 -2
2 -2
-1 2
4 0

15

$max(\Delta x_1,\Delta y_1)+max(\Delta x_2,\Delta y_2)+...+max(\Delta x_n,\Delta y_n)$

$\sum_{i=1}^ndis(i,j)=dis(1,j)+dis(2,j)+...+dis(n,j)$

$\sum_{i=1}^n\Delta x=|x_1-x_j|+|x_2-x_j|+...+|x_j-x_j|+|x_{j+1}-x_j|+...+|x_n-x_j|$

$\sum_{i=1}^j(x_j-x_i)+\sum_{i=j+1}^n(x_i-x_j)$

• 坐标变换：O(n)
• 前缀和处理：O(n)
• 枚举每个点：O(n)
• 计算一个点的答案：O(log n)

## 源代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL multMod(LL a,LL b,LL mod){ a%=mod; b%=mod; LL res=0; while(b){if(b&1)res=(res+a)%mod; a=(a<<=1)%mod; b>>=1; } return res%mod;}
LL quickMultPowMod(LL a, LL b,LL mod){ LL res=1,k=a; while(b){if((b&1))res=multMod(res,k,mod)%mod; k=multMod(k,k,mod)%mod; b>>=1;} return res%mod;}
LL quickPowMod(LL a,LL b,LL mod){ LL res=1; while(b){if(b&1)res=(a*res)%mod; a=(a*a)%mod; b>>=1; } return res; }
LL getInv(LL a,LL mod){ return quickPowMod(a,mod-2,mod); }
LL GCD(LL x,LL y){ return !y?x:GCD(y,x%y); }
LL LCM(LL x,LL y){ return x/GCD(x,y)*y; }
const double EPS = 1E-6;
const int MOD = 1000000000+7;
const int N = 100000+5;
const int dx[] = {0,0,-1,1,1,-1,1,1};
const int dy[] = {1,-1,0,0,-1,1,-1,1};
using namespace std;

struct Node {
LL x, y;
} node[N];
int n, x[N], y[N];
LL sum1[N], sum2[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int a, b;
scanf("%d%d", &a, &b);
x[i] = node[i].x = a + b;
y[i] = node[i].y = a - b;
}
sort(x + 1, x + n + 1);
sort(y + 1, y + n + 1);

for (int i = 1; i <= n; i++){
sum1[i] = sum1[i - 1] + x[i];
sum2[i] = sum2[i - 1] + y[i];
}

LL res=1ll << 62;
for (int i = 1; i <= n; i++) {
int pos = lower_bound(x + 1, x + n + 1, node[i].x) - x;
LL sum = sum1[n] - sum1[pos] - node[i].x * (n - pos) + node[i].x * pos - sum1[pos];
pos = lower_bound(y + 1, y + n + 1, node[i].y) - y;
sum += sum2[n] - sum2[pos] - node[i].y * (n - pos) + node[i].y * pos - sum2[pos];
res = min(res, sum);
}
printf("%lld\n", res / 2);
return 0;
}

• 点赞 2
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

Alex_McAvoy

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
10-24
03-11 1万+

07-03 1282
03-20 5万+
12-17 2337
01-26
08-16 285
12-18 33万+
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客