[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 ...
随机推荐
- python_class21
#!/usr/bin/env python # -*- coding: utf-8 -*- """ @version: 3.5 @author: morgana @lic ...
- 10-09C#语言基础
10-09C#语言基础 第一课 一.新项目的建立:打开Visual studio2012,单击“文件→新建项目→模板isualC# Windows 控制台应用程序→确定”即可. 在新建的项目中,首 ...
- 问题:HttpContext.Current.Session;结果:Session与HttpContext.Current.Session到底有什么区别呢?
我在做练习的时候遇到了这样一个问题,在母版页页面中写入登录和密码修改的js代码,在登录的方法中写 入 HttpContext.Current.Session.Add("UserPwd&quo ...
- DAY18-Django之form表单
构建一个表单 假设你想在你的网站上创建一个简单的表单,以获得用户的名字.你需要类似这样的模板: <form action="/your-name/" method=" ...
- 转载-你应该知道的 RPC 原理
在校期间大家都写过不少程序,比如写个hello world服务类,然后本地调用下,如下所示.这些程序的特点是服务消费方和服务提供方是本地调用关系. 而一旦踏入公司尤其是大型互联网公司就会发现,公司的系 ...
- 基于Flask框架的Python web程序的开发实战 <一> 环境搭建
最近在看<Flask Web开发基于Python的Web应用开发实战>Miguel Grinberg著.安道译 这本书,一步步跟着学习Flask框架的应用,这里做一下笔记 电脑只安装一个P ...
- ubuntu16部署gitlab
一.gitlab的安装 1. 安装依赖包 $ sudo apt-get update #如无ssh还需安装openssh-server $ sudo apt-get install postfix c ...
- sqlplus--spool命令参数详解
sqlplus--SPOOL参数详解 Spool是Oracle快速导出数据的工具,是sqlplus的指令,不是sql语法里的东西 一.Spool常用的设置set arraysize 5000; // ...
- var_dump — 打印变量的相关信息
<?php $a = array( 1 , 2 , array( "a" , "b" , "c" )); var_dump ( $a ...
- Python 安装 django框架
1.安装 pip install django 2.创建项目 d:/www/django文件夹下右键->打开dos窗口 输入: python C:\ProgramData\Miniconda3\ ...