前缀+排序 HDOJ 4311 Meeting point-1
题意:给一些坐标轴上的点,选一个点,使得其他点到该点曼哈顿距离和最小
分析:这题有很强的技巧性,直接计算每个点的曼哈顿距离和是不可行的。这里用到了前缀的思想,先对点按照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的更多相关文章
- HDU 4311 Meeting point-1(曼哈顿距离最小)
http://acm.hdu.edu.cn/showproblem.php?pid=4311 题意:在二维坐标中有n个点,现在要从这n个点中选出一个点,使得其他点到该点的曼哈顿距离总和最小. 思路: ...
- HDU 4311 Meeting point-1 && HDU 4312 Meeting point-2
这俩个题 题意::给出N(<1e5)个点求找到一个点作为聚会的地方,使每个点到达这里的距离最小.4311是 曼哈顿距离 4312是 切比雪夫距离: 曼哈顿距离 :大家都知道 对于二维坐标系a( ...
- 双拓扑排序 HDOJ 5098 Smart Software Installer
题目传送门 /* 双拓扑排序:抄的,以后来补 详细解释:http://blog.csdn.net/u012774187/article/details/40736995 */ #include < ...
- cmp排序hdoj 1106排序
上班之余抽点时间出来写写博文,希望对新接触的朋友有帮助.今天在这里和大家一起学习一下cmp排序 /*标题还是比拟的水吧,但是花的时间还是比拟的多,心不够静*/ #include <iostrea ...
- HDU 4311 Meeting point-1 求一个点到其它点的曼哈顿距离之和
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4311 解题报告:在一个平面上有 n 个点,求一个点到其它的 n 个点的距离之和最小是多少. 首先不得不 ...
- Hdu 4311-Meeting point-1 曼哈顿距离,前缀和
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4311 Meeting point-1 Time Limit: 2000/1000 MS (Java/Oth ...
- MongoDB 使用Index
Index 能够提高查询的性能,如果没有Index,MongoDB必须扫描整个collection,从collection的第一个doc开始,直到最后一个doc,即使第一个doc之后的所有doc都不满 ...
- Google HTML/CSS代码风格指南(中文版)
原文链接:http://wncbl.cn/posts/c8e10815/ 看一下没什么印象,那就写一遍吧. 背景 本文档定义了HTML/CSS的编写格式和风格规则.它旨在提高合作和代码质量,并使其支持 ...
- 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时表示将序列[ ...
随机推荐
- hdu 3255 Farming(扫描线)
题目链接:hdu 3255 Farming 题目大意:给定N个矩形,M个植物,然后给定每一个植物的权值pi,pi表示种植物i的土地,单位面积能够收获pi,每一个矩形给定左下角和右上角点的坐标,以及s, ...
- 【转】Selenium2学习路线
课程大纲:第一部分:基础入门 第一课: SELENIUM2的原理介绍及环境搭建本节课主要讲解SELENIUM2的原理,让大家了解SELENIUM2的发展历程,同时解惑大家对自动化测试中产生的一些误区. ...
- 【spring+websocket的使用】
一.spring配置文件Java代码 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns= ...
- Android5.0(lollipop)新特性介绍(一)
今年6月的Google I/O大会上.Android L的初次见面我相信让会让非常多android粉丝有些小激动和小期待.当然作为开发人员的我来说,激动不言而喻,毕竟这是自08年以来改变最大的一个版本 ...
- struts2多图片上传实例【转】
原文地址:http://blog.csdn.net/java_cxrs/article/details/6004144 描述: 通过struts2实现多图片上传. 我使用的版本是2.2.1,使用的包有 ...
- Redhat enterpise6 安装unix2dos/dos2unix
初用unix2dos,在rhel6 上 用yum install unix2dos , 提示源不可用, 那好吧, 就去rpm包网:http://rpm.pbone.net/ 下载了一个unix2dos ...
- mongodb07---用户权限
用户管理: 注意: 添加用户后,我们再次退出并登陆,发现依然可以直接读数据库? 原因: mongodb服务器启动时, 默认不是需要认证的. 要让用户生效, 需要启动服务器时,就指定 --auth 选项 ...
- Mac下安装manen
下载好maven,放到特定目录下: 打开终端: 进入根目录: cd ~ 创建文件.bash_profile: vi .bash_profile 编辑文件添加内容 MAVEN_HOME=/Users/c ...
- java生成随机汉字
方法一: public static char getRandomChar() { return (char) (0x4e00 + (int) (Math.random() * (0x9fa5 - 0 ...
- The android gradle plugin version 2.3.0-beta2 is too old, please update to the latest version.
编译项目的时候,报如下错误: Error:(, ) A problem occurred evaluating project ':app'. > Failed to apply plugin ...