[TJOI2013]松鼠聚会 BZOJ 3170
题目描述
草原上住着一群小松鼠,每个小松鼠都有一个家。时间长了,大家觉得应该聚一聚。但是草原非常大,松鼠们都很头疼应该在谁家聚会才最合理。
每个小松鼠的家可以用一个点x,y表示,两个点的距离定义为点(x,y)和它周围的8个点(x-1,y)(x+1,y),(x,y-1),(x,y+1).(x-1,y+1),(x-1,y-1),(x+1,y+1),(x+1,y-1)距离为1。
输入输出格式
输入格式:
第一行是一个整数N,表示有多少只松鼠。接下来N行,第i行是两个整数x和y,表示松鼠i的家的坐标
输出格式:
一个整数,表示松鼠为了聚会走的路程和最小是多少。
输入输出样例
说明
样例解释
在第一个样例中,松鼠在第二只松鼠家(-1,-2)聚会;在第二个样例中,松鼠在第一只松鼠家(0.0)聚会。
数据范围
30%的数据,0 ≤ N ≤ 1000
100%的数据,0 ≤ N ≤ 100000; −10^9 ≤ x, y ≤ 10^9
首先我们要求的是 切比雪夫距离。
也就是 dis=max( |dx|,|dy|);
看样子可能不太好求解;
想办法转换为 曼哈顿距离;
在(x,y)坐标系中进行变换----> ( (x+y)/2,(x-y)/2 );
可以发现原坐标系中的 切比雪夫距离 就是新坐标系中的 曼哈顿距离 ;
(推一下即可);
那么我们考虑用 曼哈顿距离求解:
∑Mdis(i,k) 即该值最小;
将其变为有序方便处理(不妨设为升序);
即 Mdis(1,i)+Mdis(2,i)+...+Mdis(n,i)
现以X坐标为例:
即 x[ i ]-x[ 1 ]+x[ i ]-x[ 2 ]+...+x[ i+1 ]-x[ i ]+...x[ n ]-x[ i ]
= i*x[ i ]- sum[ i ]+sum[ n ]-sum[ i ]+(n-i)*x[ i ];
那么就可以用前缀和进行维护了;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream> //#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 300005
#define inf 0x3f3f3f3f
#define INF 9999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++) inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ ll qpow(ll a, ll b, ll c) {
ll ans = 1;
a = a % c;
while (b) {
if (b % 2)ans = ans * a%c;
b /= 2; a = a * a%c;
}
return ans;
} int n;
int x[maxn], y[maxn];
ll ans, tmp, sumx[maxn], sumy[maxn];
struct node {
ll x, y;
}a[maxn]; int main()
{
//ios::sync_with_stdio(0);
rdint(n);
for (int i = 1; i <= n; i++) {
int xx, yy;
rdint(xx); rdint(yy);
x[i] = a[i].x = xx + yy; y[i] = a[i].y = xx - yy;
}
sort(x + 1, x + 1 + n); sort(y + 1, y + 1 + n);
for (int i = 1; i <= n; i++)
sumx[i] = sumx[i - 1] + x[i], sumy[i] = sumy[i - 1] + y[i];
ans = 100000000000000000;
for (int i = 1; i <= n; i++) {
int pos = lower_bound(x + 1, x + 1 + n, a[i].x) - x;
tmp = sumx[n] - sumx[pos] - a[i].x*(n - pos) + pos * a[i].x - sumx[pos];
pos = lower_bound(y + 1, y + 1 + n, a[i].y) - y;
tmp += sumy[n] - sumy[pos] - a[i].y*(n - pos) + a[i].y*pos - sumy[pos];
ans = min(ans, tmp);
}
cout << ans / 2 << endl;
return 0;
}
[TJOI2013]松鼠聚会 BZOJ 3170的更多相关文章
- 【bzoj3170】[Tjoi2013]松鼠聚会
3170: [Tjoi2013]松鼠聚会 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1670 Solved: 885[Submit][Statu ...
- BZOJ_3170_[Tjoi2013]松鼠聚会_切比雪夫距离+前缀和
BZOJ_3170_[Tjoi2013]松鼠聚会_切比雪夫距离+前缀和 题意:有N个小松鼠,它们的家用一个点x,y表示,两个点的距离定义为:点(x,y)和它周围的8个点即上下左右四个点和对角的四个点, ...
- [TJOI2013]松鼠聚会 曼哈顿距离
[TJOI2013]松鼠聚会 luogu P3964 首先容易得到两点间距离是\(max(|x_1-x_2|, |y_1-y_2|)\)(即切比雪夫距离) 然后有个套路:原\((x,y)\)求曼哈顿距 ...
- [TJOI2013]松鼠聚会(枚举)
[TJOI2013]松鼠聚会 题目描述 草原上住着一群小松鼠,每个小松鼠都有一个家.时间长了,大家觉得应该聚一聚.但是草原非常大,松鼠们都很头疼应该在谁家聚会才最合理. 每个小松鼠的家可以用一个点x, ...
- 洛谷P3964 [TJOI2013]松鼠聚会 [二分答案,前缀和,切比雪夫距离]
题目传送门 松鼠聚会 题目描述 草原上住着一群小松鼠,每个小松鼠都有一个家.时间长了,大家觉得应该聚一聚.但是草原非常大,松鼠们都很头疼应该在谁家聚会才最合理. 每个小松鼠的家可以用一个点x,y表示, ...
- BZOJ.3170.[TJOI2013]松鼠聚会(切比雪夫距离转曼哈顿距离)
题目链接 将原坐标系每个点的坐标\((x,y)\)变为\((x+y,x-y)\),则原坐标系中的曼哈顿距离等于新坐标系中的切比雪夫距离. 反过来,将原坐标系每个点的坐标\((x,y)\)变为\((\f ...
- BZOJ 3170 [Tjoi2013]松鼠聚会
题解:切比雪夫距离转化为曼哈顿距离 枚举源点,横纵坐标互不影响,分开考虑,前缀和优化 横纵分开考虑是一种解题思路 #include<iostream> #include<cstdio ...
- 3170: [Tjoi2013]松鼠聚会
Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1804 Solved: 968[Submit][Status][Discuss] Descript ...
- BZOJ3170: [Tjoi2013]松鼠聚会(切比雪夫距离转曼哈顿距离)
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1524 Solved: 803[Submit][Status][Discuss] Descripti ...
随机推荐
- leetcode429
这道题目是属于树的层次遍历,使用两层的队列非空判断. class Solution { public: vector<vector<int>> levelOrder(Node* ...
- 转载 : JSP取得绝对路径
转自:https://www.aliyun.com/jiaocheng/770177.html 转自:http://www.cnblogs.com/xdp-gacl/p/3707243.html 在J ...
- java,js判断全角半角
function chkHalf(str){ for(var i=0;i { strCode=str.charCodeAt(i); if((strCode>65248)||(strCode==1 ...
- 使用ffmpeg转换视频格式
命令: ffmpeg -i infile -ab 128 -acodec libmp3lame -ac 1 -ar 22050 -r 29.97 -qscale 6 -y outfile 说明: ...
- Ros学习——录制与回放
mkdir ~/bagfiles cd ~/bagfiles rosbag record -a 录制完成后,查看文件: rosbag info <your bagfile> 回放:在终端中 ...
- php返回文件路径
1 basename — 返回路径中的文件名部分 如果文件名为test.php,路径为www/hj/test.php echo basename($_SERVER['PHP_SELF']); 输出为: ...
- java中静态方法和非静态方法调用的一点小困扰,已解决。
public static void main(String[] args) { // TODO Auto-generated method stub SimpleGui1B gui=new Simp ...
- docker卸载与安装
sudo apt remove docker.io 报错如下: Reading package lists... Done Building dependency tree Reading state ...
- Umbraco中如何找到home node
在一个Umbraco项目中,我们经常会出现需要找到这个项目的home node的情况, 那么如何来找到项目的home node呢 方法如下: 1. 在View中 @inherits Umbraco.W ...
- kaggle Titanic
# coding: utf-8 # In[19]: # 0.78468 # In[20]: import numpy as np import pandas as pd import warnings ...