题目传送门

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

分析:这题有很强的技巧性,直接计算每个点的曼哈顿距离和是不可行的。这里用到了前缀的思想,先对点按照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. 创立一个站点的前前后后(起因,域名,云平台,备案,CDN等等)(1)

    起因 写完<完美软件开发:方法与逻辑>这书后,原本想继续写书的,可出来參加了些社区活动后,我发现我写的书大家评价还行.但事实上不太理解.而接下来想写的书更加抽象点.准备叫<管理的解析 ...

  2. vue Iframe

    1.Iframe.vue <!-- Iframe --> <template> <div> <!-- 标题栏 --> <mt-header tit ...

  3. hdu 3853(数学期望入门)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3853 LOOPS Time Limit: 15000/5000 MS (Java/Others)    ...

  4. 【转载】企业服务总线Enterprise service bus介绍

    企业服务总线(Enterprise service bus). 以往企业已经实现了很多服务, 构成了面向服务的架构,也就是我们常说的SOA. 服务的参与双方都必须建立1对1 的联系,让我们回顾一下SO ...

  5. Spring嵌套事务控制

    A类   callBack_test() B类   testadd() C类   select(),得查询到B类testadd方法中新增的数据.以及初始化一些属性 场景:A类 嵌套 B类  B类嵌套C ...

  6. JavaScript学习14:表单处理

    什么是表单? 在HTML中,表单是由<form>元素来表示的.而在JavaScript中,表单相应的则是HTMLFormElement类型.HTMLFormElement继承了HTMLEl ...

  7. aspx后台引用不到服务器控件

    从其他地方拷贝的页面到自己的项目,后台CS代码引用hidden时,提示找不到,百度,发现可能是网站项目和Web应用程序的区别,右键aspx转化为Web应用程序即可.

  8. IDEA debug调式快捷键

    F9 resume programe 恢复程序 Alt+F10 show execution point 显示执行断点 F8 Step Over 相当于eclipse的f6 跳到下一步 F7 Step ...

  9. [学习笔记]dsu on a tree(如何远离线段树合并)

    https://www.zybuluo.com/ysner/note/1318613 背景 这玩意来源于一种有局限性的算法. 有一种广为人知的,树上离线维护子树信息的做法. (可以参照luogu360 ...

  10. Python安装、配置图文详解

    原文地址:http://weixiaolu.iteye.com/blog/1617440 目录: 一. Python简介 二. 安装python 1. 在windows下安装 2. 在Linux下安装 ...