lightoj 1349 - Aladdin and the Optimal Invitation 【中位数的运用】
2015-11-08 12:37
489 查看
1349 - Aladdin and the Optimal Invitation
Finally Aladdin reached home, with the great magical lamp. He was happier than ever. As he was a nice boy, he wanted to share the happiness with all people in the town. So, he wanted to invite all people in town in some place such that they can meet there
easily. As Aladdin became really wealthy, so, number of people was not an issue. Here you are given a similar problem.
Assume that the town can be modeled as an m x n 2D grid. People live in the cells. Aladdin wants to select a cell such that all people can gather here with optimal overall cost. Here, cost for a person is the distance he
has to travel to reach the selected cell. If a person lives in cell (x, y) and he wants to go to cell (p, q), then the cost is |x-p|+|y-q|. So, distance between (5, 2) and (1, 3) is |5-1|+|2-3| which
is 5. And the overall cost is the summation of costs for all people.
So, you are given the information of the town and the people, your task to report a cell which should be selected by Aladdin as the gathering point and theoverall cost should be as low as possible.
Each case starts with a blank line. Next line contains three integers: m, n and q (1 ≤ m, n, q ≤ 50000), m and n denote the number of rows and columns of the grid respectively. Each of the
next q lines contains three integers u v w (1 ≤ u ≤ m, 1 ≤ v ≤ n, 1 ≤ w ≤ 10000), meaning that there are w persons who live in cell (u, v). You can assume that there are no people in the cells
which are not listed. You can also assume that each of the q lines contains a distinct cell.
2. Dataset is huge, use faster I/O methods.
PROBLEM SETTER: JANE ALAM JAN
题意:给定一个n*m的图,图上有q个点住有人。现在已经给出每个点的坐标和该点的人数,让你找到一个点使得图中所有人到达该点的曼哈顿距离之和最小。
思路:排序后找中位数,一开始用了map,结果一直WA。换了结构体就过了,醉了。。。
AC代码:
PDF (English) | Statistics | Forum |
Time Limit: 4 second(s) | Memory Limit: 32 MB |
easily. As Aladdin became really wealthy, so, number of people was not an issue. Here you are given a similar problem.
Assume that the town can be modeled as an m x n 2D grid. People live in the cells. Aladdin wants to select a cell such that all people can gather here with optimal overall cost. Here, cost for a person is the distance he
has to travel to reach the selected cell. If a person lives in cell (x, y) and he wants to go to cell (p, q), then the cost is |x-p|+|y-q|. So, distance between (5, 2) and (1, 3) is |5-1|+|2-3| which
is 5. And the overall cost is the summation of costs for all people.
So, you are given the information of the town and the people, your task to report a cell which should be selected by Aladdin as the gathering point and theoverall cost should be as low as possible.
Input
Input starts with an integer T (≤ 20), denoting the number of test cases.Each case starts with a blank line. Next line contains three integers: m, n and q (1 ≤ m, n, q ≤ 50000), m and n denote the number of rows and columns of the grid respectively. Each of the
next q lines contains three integers u v w (1 ≤ u ≤ m, 1 ≤ v ≤ n, 1 ≤ w ≤ 10000), meaning that there are w persons who live in cell (u, v). You can assume that there are no people in the cells
which are not listed. You can also assume that each of the q lines contains a distinct cell.
Output
For each case, print the case number and the row and column position of the cell where the people should be invited. There can be multiple solutions, any valid one will do.Sample Input | Output for Sample Input |
2 5 1 1 2 1 10 5 5 4 1 1 1 2 2 1 4 4 1 5 5 1 | Case 1: 2 1 Case 2: 3 3 |
Note
1. This is a special judge problem; wrong output format may cause 'Wrong Answer'.2. Dataset is huge, use faster I/O methods.
PROBLEM SETTER: JANE ALAM JAN
题意:给定一个n*m的图,图上有q个点住有人。现在已经给出每个点的坐标和该点的人数,让你找到一个点使得图中所有人到达该点的曼哈顿距离之和最小。
思路:排序后找中位数,一开始用了map,结果一直WA。换了结构体就过了,醉了。。。
AC代码:
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <vector> #define INF 0x3f3f3f3f #define eps 1e-8 #define MAXN 100000+10 #define MAXM 50000000 #define Ri(a) scanf("%d", &a) #define Rl(a) scanf("%lld", &a) #define Rf(a) scanf("%lf", &a) #define Rs(a) scanf("%s", a) #define Pi(a) printf("%d\n", (a)) #define Pf(a) printf("%lf\n", (a)) #define Pl(a) printf("%lld\n", (a)) #define Ps(a) printf("%s\n", (a)) #define W(a) while(a--) #define CLR(a, b) memset(a, (b), sizeof(a)) #define MOD 1000000007 #define LL long long #define lson o<<1, l, mid #define rson o<<1|1, mid+1, r #define ll o<<1 #define rr o<<1|1 using namespace std; int n, m, q; struct Node{ int x, y, w; }; Node num[MAXN]; bool cmp1(Node a, Node b){ return a.x < b.x; } bool cmp2(Node a, Node b){ return a.y < b.y; } int main() { int t, kcase = 1; Ri(t); W(t) { Ri(n); Ri(m); Ri(q); int sumw = 0; for(int i = 0; i < q; i++) { Ri(num[i].x); Ri(num[i].y); Ri(num[i].w); sumw += num[i].w; } sumw /= 2; sort(num, num+q, cmp1); int cntx = 0, cnty = 0; int r, c; for(int i = 0; i < q; i++) { cntx += num[i].w; if(cntx > sumw) { r = num[i].x; break; } } sort(num, num+q, cmp2); for(int i = 0; i < q; i++) { cnty += num[i].w; if(cnty > sumw) { c = num[i].y; break; } } printf("Case %d: %d %d\n", kcase++, r, c); } return 0; }
相关文章推荐
- Linux_NetworkManager_RHEL7
- XAMPP(Linux版-x86兼容)官网下载
- linux命令-vim
- openfire编译
- linux服务器部署项目
- Linux和Windows下开启服务的一些命令
- [转]OpenWRT之获取MAC地址
- linux 进程线程拓展
- 在虚拟机(VMware)中安装Linux CentOS 6.4系统(图解)
- Linux下查找相关命令
- Julia: 引用与copy: 关于数组、自定义类型与初始化
- windows共享文件夹给centOS
- linux下搭建SVN服务器完全手册
- 【OpenCV人脸识别入门教程之三】CMake编译OpenCV源码
- Linux 搭建svn服务器
- Hive入门学习之二:Hive 的部署
- Windows系统调用架构分析—也谈KiFastCallEntry函数地址的获取 + 一个hook的实现
- Hive入门学习之一:Hive的简述与架构
- 关于linux 内存分配【zz】
- 修复ShopNC使用QQ 互联时提示100010 错误