lightoj 1349 - Aladdin and the Optimal Invitation 贪心 中位数

1349 - Aladdin and the Optimal Invitation

PDF (English) Statistics Forum
Time Limit: 4 second(s) Memory Limit: 32 MB

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.

链接:http://lightoj.com/volume_showproblem.php?problem=1349

题意: 有n*m的 格子。 然后输入每个格子的人数。 最后决定所有人到达一个格子,要求每个人走的路都最少。

做法:横坐标为i 的 所有格子人数和 放在numr【i】里 。 纵坐标同理。 这个格子的 横坐标和纵坐标的选择是不会相互影响的。  我们只用找到 所有格子的总人数和,取一个中位数mid=sum/2,然后判断第mid个人在第几行,那行就是答案的横坐标。

因为,如果这个横坐标向下移,因为它本来是中位数,所以上面的人数肯定变得大于下面的人数。而结果会导致,这个横坐标上面的人  要多走一步,下面的人少走一步,最后就是总步数增加了。

int rnum[50010];
int lnum[50010];

int main()
{
	int t,n;
	int r,l,c,q,m;
	cin>>t;
	int cas=1;
	while(t--)
	{
		cin>>n>>m>>q;
		//50000
		for(int i=1;i<=n;i++)
			rnum[i]=0;
		for(int i=1;i<=m;i++)
			lnum[i]=0;
		int sum=0;
		while(q--)
		{
			scanf("%d%d%d",&r,&l,&c);
			sum+=c;
			rnum[r]+=c;
			lnum[l]+=c;
		}
		int ansr,ansc;
		int num=0;
		int mid=(sum+1)/2;
		for(int i=1;i<=n;i++)
		{
			num+=rnum[i];
			if(num>=mid)
			{
				ansr=i;
				break;
			}
		}
		num=0;
		for(int i=1;i<=m;i++)
		{
			num+=lnum[i];
			if(num>=mid)
			{
				ansc=i;
				break;
			}
		}
		printf("Case %d: %d %d\n",cas++,ansr,ansc);
	}
	return 0;
}
时间: 2024-10-04 14:09:54

lightoj 1349 - Aladdin and the Optimal Invitation 贪心 中位数的相关文章

LightOJ 1349 - Aladdin and the Optimal Invitation(数学啊)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1349 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 invi

BNUOJ 13268 Aladdin and the Optimal Invitation

思路:x轴和y轴的情况是独立的,可以分开考虑,那么只要枚举位置,能维护最小值即可,这只要把公式拆开,预处理出两个前缀和即可,一个是w的前缀和,一个是w * x的前缀和 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 50005; typedef long long ll; int t, n, m, q; int x[N], y

LightOJ 1341 - Aladdin and the Flying Carpet (唯一分解定理 + 素数筛选)

http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1341 Description It's said that Aladdin had to solve seven

LightOJ 1341 - Aladdin and the Flying Carpet(算术基本定理啊)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1341 It's said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a powerful Genie. Here we are concerned about the first mystery. Aladdin was about to enter

Lightoj 1348 Aladdin and the Return Journey (树链剖分)(线段树单点修改区间求和)

Finally the Great Magical Lamp was in Aladdin's hand. Now he wanted to return home. But he didn't want to take any help from the Genie because he thought that it might be another adventure for him. All he remembered was the paths he had taken to reac

LightOJ 1341 - Aladdin and the Flying Carpet 基本因子分解

http://www.lightoj.com/volume_showproblem.php?problem=1341 题意:给你长方形的面积a,边最小为b,问有几种情况. 思路:对a进行素因子分解,再乘法原理算一下,最后减去小于b的因子的情况即可. /** @Date : 2016-12-01-19.04 * @Author : Lweleth ([email protected]) * @Link : https://github.com/ * @Version : */ #include<b

LightOJ 1341 - Aladdin and the Flying Carpet【合数分解】

题目链接:http://lightoj.com/volume_showproblem.php?problem=1341 题意: 给出整数 a 和 b ,求区间[b, a] 内的 a 的约数对的个数,a 的约数对(比如[2, 3] 与 [3, 2] 为同一对). 解法: 主要利用公式: 一个整数n可以表示为若干素数乘积: n = p1^a1 * p2^a2*-*pm^am; 则 n 的正因数的个数可以表示为: num = (a1+1)*(a2+1)-(am+1); 代码: #include <st

LightOJ 1341 Aladdin and the Flying Carpet(唯一分解定理)

http://lightoj.com/volume_showproblem.php?problem=1341 题意:给你矩形的面积(矩形的边长都是正整数),让你求最小的边大于等于b的矩形的个数. 思路:根据唯一分解定理,把X写成若干素数相乘的形式,则X的正因数的个数为:(1+a1)(1+a2)(1+a3)...(1+an).(ai为指数) 因为这道题目是求矩形,所以知道一个正因数后,另一个正因数也就确定了,所以每组正因数重复计算了两遍,需要除以2. 最后减去小于b的因数. 1 #include<

LightOJ 1341(Aladdin and the Flying Carpet )算术基本定理

It's said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a powerful Genie. Here we are concerned about the first mystery. Aladdin was about to enter to a magical cave, led by the evil sorcerer who disguised hi