BZOJ 3170: [Tjoi 2013]松鼠聚会( sort )
2016-01-07 19:40
369 查看
题目的距离为max(|x1-x2|, |y1-y2|) (切比雪夫距离). 切比雪夫距离(x, y)->曼哈顿距离((x+y)/2, (x-y)/2) (曼哈顿(x, y)->切比雪夫(x+y, x-y)). 转成Manhattan distance后排序前缀和维护即可.
--------------------------------------------------------------------------
#include<cstdio>#include<cstring>#include<algorithm> using namespace std; #define X(o) x[rx[o]]#define Y(o) y[ry[o]] typedef long long ll; const int maxn = 100009; ll smx[maxn], smy[maxn], ans;int N, x[maxn], y[maxn], rx[maxn], ry[maxn]; bool cmpX(const int &l, const int &r) { return x[l] < x[r];} bool cmpY(const int &l, const int &r) { return y[l] < y[r];} int BS(int c[], int r[], int v) { int L = 0, R = N - 1; while(L <= R) { int m = (L + R) >> 1; if(c[r[m]] == v) return m; c[r[m]] < v ? L = m + 1 : R = m - 1; } return -1;} int main() { scanf("%d", &N); for(int i = 0; i < N; i++) { rx[i] = ry[i] = i; int _x, _y; scanf("%d%d", &_x, &_y); x[i] = _x + _y; y[i] = _x - _y; } sort(rx, rx + N, cmpX); sort(ry, ry + N, cmpY); smx[0] = X(0); smy[0] = Y(0); for(int i = 1; i < N; i++) { smx[i] = smx[i - 1] + X(i); smy[i] = smy[i - 1] + Y(i); } ans = 1LL << 60; for(int i = 0; i < N; i++) { int px = BS(x, rx, x[i]), py = BS(y, ry, y[i]); ll t = 0; if(!px) { t += smx[N - 1] - ll(N) * x[i]; } else { t += ll(x[i]) * (px * 2 + 2 - N) - 2LL * smx[px] + smx[N - 1]; } if(!py) { t += smy[N - 1] - ll(N) * y[i]; } else { t += ll(y[i]) * (py * 2 + 2 - N) - 2LL * smy[py] + smy[N - 1]; } ans = min(t, ans); } printf("%lld\n", ans >> 1); return 0;}-------------------------------------------------------------------------
3170: [Tjoi 2013]松鼠聚会
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 874 Solved: 421
[Submit][Status][Discuss]
Description
有N个小松鼠,它们的家用一个点x,y表示,两个点的距离定义为:点(x,y)和它周围的8个点即上下左右四个点和对角的四个点,距离为1。现在N个松鼠要走到一个松鼠家去,求走过的最短距离。Input
第一行给出数字N,表示有多少只小松鼠。0<=N<=10^5下面N行,每行给出x,y表示其家的坐标。
-10^9<=x,y<=10^9
Output
表示为了聚会走的路程和最小为多少。Sample Input
6-4 -1
-1 -2
2 -4
0 2
0 3
5 -2
Sample Output
20HINT
Source
相关文章推荐
- c# 基本控件,窗口程序
- <<梦断代码>>阅读笔记三
- 冒泡排序、选择排序
- 控制台界面控制(七):移动文本
- Hibernate注释大全
- IoC容器和 Dependency Injection模式 Inversion of Control Containers and the Dependency Injection pattern
- 19: 津津的储蓄计划(1.5)
- win10切换全角/半角
- <<梦断代码>>阅读笔记二
- <<梦断代码>>阅读笔记一
- Linux下的注释问题
- 无线网络嗅探工具Kismet
- python编写windows平台下脚本二三事
- 双击打开某张表的时候很慢的解决办法
- C和C++内存管理详解
- Ubuntu 12.04全局菜单开启和关闭
- 控制台界面控制(六):边框
- 2599: [IOI2011]Race 点分治
- phpExcel 读取和导出
- BZOJ 1509: [NOI2003]逃学的小孩