题目传送门

题意:给一些坐标轴上的点,选一个点,使得其他点到该点曼哈顿距离和最小

分析:这题有很强的技巧性,直接计算每个点的曼哈顿距离和是不可行的。这里用到了前缀的思想,先对点按照x从左到右排序,p[i].sum保存选择i点时曼哈顿距离和是多少,p[i].sum = (i - 1) * p[i].x - sum (p1.x~pi-1.x) + sum (pi+1.x~pn.x) - (n - i) * p[i].x; ,i前面每个点的到i的x轴距离为:p[i].x - p[j].x,i后面每个点到i的x轴距离为:p[j].x - p[i].x,累计求和就是上式,y轴同理,可能配上图好理解:

那么每个点都计算出来了,只要在遍历一遍取最小值就是答案

收获:1. 前缀的使用技巧 2. 还有升级版的?

代码:

/************************************************
* Author :Running_Time
* Created Time :2015-8-24 13:21:56
* File Name :B_2.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const ll INFF = 1ll << 62;
const int MOD = 1e9 + 7;
struct Point {
ll x, y, sum;
}p[N];
int n; bool cmpx(Point a, Point b) {
return a.x < b.x;
} bool cmpy(Point a, Point b) {
return a.y < b.y;
} int main(void) {
int T; scanf ("%d", &T);
while (T--) {
scanf ("%d", &n);
for (int i=1; i<=n; ++i) {
scanf ("%I64d%I64d", &p[i].x, &p[i].y);
}
sort (p+1, p+1+n, cmpx); ll sum = 0;
for (int i=1; i<=n; ++i) {
p[i].sum = (i - 1) * p[i].x - sum;
sum += p[i].x;
}
sum = 0;
for (int i=n; i>=1; --i) {
p[i].sum += sum - (n - i) * p[i].x;
sum += p[i].x;
} sort (p+1, p+1+n, cmpy); sum = 0;
for (int i=1; i<=n; ++i) {
p[i].sum += (i - 1) * p[i].y - sum;
sum += p[i].y;
}
sum = 0;
for (int i=n; i>=1; --i) {
p[i].sum += sum - (n - i) * p[i].y;
sum += p[i].y;
} ll ans = INFF;
for (int i=1; i<=n; ++i) {
ans = min (ans, p[i].sum);
}
printf ("%I64d\n", ans);
} return 0;
}

  

前缀+排序 HDOJ 4311 Meeting point-1的更多相关文章

  1. HDU 4311 Meeting point-1(曼哈顿距离最小)

    http://acm.hdu.edu.cn/showproblem.php?pid=4311 题意:在二维坐标中有n个点,现在要从这n个点中选出一个点,使得其他点到该点的曼哈顿距离总和最小. 思路: ...

  2. HDU 4311 Meeting point-1 && HDU 4312 Meeting point-2

    这俩个题  题意::给出N(<1e5)个点求找到一个点作为聚会的地方,使每个点到达这里的距离最小.4311是 曼哈顿距离 4312是 切比雪夫距离: 曼哈顿距离 :大家都知道 对于二维坐标系a( ...

  3. 双拓扑排序 HDOJ 5098 Smart Software Installer

    题目传送门 /* 双拓扑排序:抄的,以后来补 详细解释:http://blog.csdn.net/u012774187/article/details/40736995 */ #include < ...

  4. cmp排序hdoj 1106排序

    上班之余抽点时间出来写写博文,希望对新接触的朋友有帮助.今天在这里和大家一起学习一下cmp排序 /*标题还是比拟的水吧,但是花的时间还是比拟的多,心不够静*/ #include <iostrea ...

  5. HDU 4311 Meeting point-1 求一个点到其它点的曼哈顿距离之和

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4311 解题报告:在一个平面上有 n 个点,求一个点到其它的 n 个点的距离之和最小是多少. 首先不得不 ...

  6. Hdu 4311-Meeting point-1 曼哈顿距离,前缀和

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4311 Meeting point-1 Time Limit: 2000/1000 MS (Java/Oth ...

  7. MongoDB 使用Index

    Index 能够提高查询的性能,如果没有Index,MongoDB必须扫描整个collection,从collection的第一个doc开始,直到最后一个doc,即使第一个doc之后的所有doc都不满 ...

  8. Google HTML/CSS代码风格指南(中文版)

    原文链接:http://wncbl.cn/posts/c8e10815/ 看一下没什么印象,那就写一遍吧. 背景 本文档定义了HTML/CSS的编写格式和风格规则.它旨在提高合作和代码质量,并使其支持 ...

  9. Codeforces Round #344 (Div. 2) C. Report

    Report 题意:给长度为n的序列,操作次数为m:n and m (1 ≤ n, m ≤ 200 000) ,操作分为t r,当t = 1时表示将[1,r]序列按非递减排序,t = 2时表示将序列[ ...

随机推荐

  1. jquery全局变量---同步请求设置

    1.同步 $.ajaxSetup({ async: false }); 2.异步 $.ajaxSetup({   async: true   }); 3.说明:我们一般使用同步完要恢复异步.由于js默 ...

  2. hdoj2680 Choose the best route

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission ...

  3. getifaddrs

    getifaddrs 获取本地网络接口的信息.在路由器上可以用这个接口来获取wan/lan等接口当前的ip地址,广播地址等信息. #include <sys/types.h> #inclu ...

  4. POJ 题目1204 Word Puzzles(AC自己主动机,多个方向查询)

    Word Puzzles Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 10244   Accepted: 3864   S ...

  5. python 线程 进程 标识

    s = '%s%s%s%s%s%s%s%s' % ( time.strftime('%Y%m%d %H:%M:%S', time.localtime(time.time())), ' os.getpp ...

  6. apply current folder view to all folders

    https://www.tenforums.com/tutorials/35093-apply-folder-view-all-folders-same-type-windows-10-a.html ...

  7. Kotlin 单例

    单例的实现方法,可以通过同伴对象,或者 lazy. 示例: class Hello private constructor() { companion object { val instance = ...

  8. AutoIT: 开发界面结合GUI automation和Watir Automation

    可以应用AutoIT开发出界面,从而把AutoIT对GUI的自动化测试与Watir对web的自动化测结合在一起.以下代码是我学习GUI界面开发的实例代码.1. 当点击Watir_Test_Button ...

  9. vs 中switch语句快捷键列出枚举

    先switch然后两下tab会补完到default,光标显示在switch后的变量这时输入枚举,输完后回车,自动补完所有枚举的case

  10. angular中transclude的理解

    今天被这个transclude搞糊涂了,弄了半天,才知道原来使用起来很简单.很烦恼为社么书中的对于这个的介绍这么晦涩难懂.直到看到了这篇文章,才让我弄清楚了. 一.transclude介绍 trans ...