lightoj 1349 - Aladdin and the Optimal Invitation (中位数的运用)
来源:互联网 发布:python模拟鼠标点击 编辑:程序博客网 时间:2024/05/21 10:35
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 the overall 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.
#include<cstdio>#include<algorithm>#include<cstring> using namespace std;struct T{int x,y,w;}a[50010];bool cmp1(T u,T v){return u.x<v.x;}bool cmp2(T u,T v){return u.y<v.y;}int main(){int t,n,m,q,k=1;int xx,yy;long long num,cnt;scanf("%d",&t);while(t--){scanf("%d%d%d",&n,&m,&q);memset(a,0,sizeof(a));num=0;for(int i=0;i<q;i++){scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w);num+=a[i].w;}num=(num+1)/2;//所有的人 中位数 sort(a,a+q,cmp1);cnt=0;for(int i=0;i<q;i++)//中位数所在横坐标 {cnt+=a[i].w;if(cnt>=num){//printf("%d %d==\n",cnt,num);xx=a[i].x;break;}}sort(a,a+q,cmp2);cnt=0;for(int i=0;i<q;i++)//中位数所在纵坐标 {cnt+=a[i].w;if(cnt>=num){yy=a[i].y;break;}}printf("Case %d: %d %d\n",k++,xx,yy);}return 0;}
- lightoj 1349 - Aladdin and the Optimal Invitation 【中位数的运用】
- lightoj 1349 - Aladdin and the Optimal Invitation (中位数的运用)
- lightoj 1349 - Aladdin and the Optimal Invitation 贪心 中位数
- lightoj 1349 Aladdin and the Optimal Invitation
- LightOJ 1349 - Aladdin and the Optimal Invitation(数学啊)
- lightoj1349 - Aladdin and the Optimal Invitation【求中位数】
- Light OJ 1349 Aladdin and the Optimal Invitation
- BNUOJ 13268 Aladdin and the Optimal Invitation
- LightOJ 1342 - Aladdin and the Magical Sticks
- lightOJ 1341 Aladdin and the Flying Carpet
- LightOJ 1341 Aladdin and the Flying Carpet
- LightOJ 1341Aladdin and the Flying Carpet
- LightOJ 1341 - Aladdin and the Flying Carpet
- Aladdin and the Flying Carpet LightOJ
- Aladdin and the Return Journey LightOJ
- Aladdin and the Flying Carpet LightOJ
- LightOJ 1341 Aladdin and the Flying Carpet
- Aladdin and the Flying Carpet LightOJ
- SURF提取与匹配
- 如何判断对象已死(可杀之)
- 垃圾收集器
- .net 控件生命周期
- ViewPager动画制作炫酷的引导页
- lightoj 1349 - Aladdin and the Optimal Invitation (中位数的运用)
- 选择排序之Java实现
- cookie自动登录认证
- pdf.js插件乱码问题
- SD/MMC 卡读写模块---SD/MMC 卡的外部物理接口
- 垃圾收集算法
- Edge disjoint paths with Ford-Fulkerson algorithm
- 宗教里的节日
- TestNG的IHookable监听器详解