UVA 11300 <中位数定理>

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <climits>
#include <cstdlib>
#include <ctime>
using namespace std;
//ios::sync_with_stdio(false);
long long int a[1000005];

int main()
{
	ios::sync_with_stdio(false);
	int n;

	while(cin >> n)
	{
		long long int  sum = 0;

		for(int i = 0; i < n;i++)
		{
			cin >> a[i];
			sum += a[i];
		}
		sum /= n;
		a[0] = 0;
		for(int i = 1; i < n ;i++)
		{
			a[i] = a[i-1] + a[i] - sum;
		}
		sort(a,a+n);

		sum = a[n/2];
		long long int ans = 0;
		for(int i = 0; i < n;i++)
		{
			ans += abs(a[i] - sum);
		}
		cout << ans << endl;
	}
	return 0;
}
这题中重要的是其中的推理和证明:
设xi 为第i个人给他后面的人的金币的数量,(由于是环形的所以x1代表的是1给最后一个人的金币的数量,当xi为负数的时候说明是他从后面的人那里得到的),由于第i个人的金币的数量只在于他自己原有的金币的数量Ai和他旁边人的变化的金币数量,例如第一个人最后的数量就是Ai - x(i) +x(i+1),由于最后必须是平均数所以这个式子就等于m;  最后得到了一连串的式子
<span style="font-family: Arial, Helvetica, sans-serif;">A1 - x(1) + x(2) = m       </span>
<span style="font-family: Arial, Helvetica, sans-serif;">A2 - x(2) + x(3) = m     </span>
<span style="font-family: Arial, Helvetica, sans-serif;">A3 - x(3) + x(4) = m </span>
<span style="white-space:pre">	</span>.
An - x(n) + x(1) = m
很明显我们最后要求的是|x1| + |x2| + |x3|··· + |xn|
这里有很多的变量不好求出起变化,所以这时候就需要换元
全部换成x(1)
x(1) = x(1)
x(2) = m - A1 + x(1) = x(1) - c1 (c1 = A1 - m)
x(3) = x(1) - c2
<span style="white-space:pre">	</span>.
x(n) = x(1) - cn-1;
所以最后就变成了求|x1| + |x1 - c2| + |x1 - c3| +....+|x1 - cn-1|
这时候就用到了中位数定理
中位数定理:在一组数中c1,c2,c3,c4,c5......cn中位数到所有数的距离之和最小
所有上面的x1 就是0,c1,c2,.....cn-1的中位数
<div class="ptt" style="text-align: center; font-size: 18pt; font-weight: bold; color: blue; padding: 10px;"><a target=_blank href="http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=33899" target="_blank" style="color: blue; text-decoration: none;">Spreading the Wealth</a></div><div class="plm" style="text-align: center;font-size:14px;"><table align="center"><tbody><tr><td><strong>Time Limit:</strong> 6000MS</td><td width="10px"> </td><td><strong>Memory Limit:</strong> Unknown</td><td width="10px"> </td><td><strong>64bit IO Format:</strong> %lld & %llu</td></tr></tbody></table></div><p class="opt_btn" align="center" style="font-family: Simsun;font-size:14px;"><span style="font-size:14px;color:#333399;"><a target=_blank href="http://acm.hust.edu.cn/vjudge/problem/toSubmit.action?id=33899" class="login ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" style="display: inline-block; position: relative; padding: 0px; margin-right: 0.1em; cursor: pointer; vertical-align: middle; overflow: visible; text-decoration: none; font-family: Verdana, Arial, sans-serif; border: 1px solid rgb(211, 211, 211); color: blue; border-radius: 4px; font-size: 12px !important; background: url(http://acm.hust.edu.cn/vjudge/jquery-ui-1.11.1.custom/images/ui-bg_glass_75_e3e4f8_1x400.png) 50% 50% repeat-x rgb(227, 228, 248);"><span class="ui-button-text" style="color: black; display: block; padding: 0.4em 1em;">Submit</span></a> <a target=_blank href="http://acm.hust.edu.cn/vjudge/problem/status.action#OJId=UVA&probNum=11300" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" style="display: inline-block; position: relative; padding: 0px; margin-right: 0.1em; cursor: pointer; vertical-align: middle; overflow: visible; text-decoration: none; font-family: Verdana, Arial, sans-serif; border: 1px solid rgb(211, 211, 211); color: blue; border-radius: 4px; font-size: 12px !important; background: url(http://acm.hust.edu.cn/vjudge/jquery-ui-1.11.1.custom/images/ui-bg_glass_75_e3e4f8_1x400.png) 50% 50% repeat-x rgb(227, 228, 248);"><span class="ui-button-text" style="color: black; display: block; padding: 0.4em 1em;">Status</span></a></span></p><div class="hiddable" id="vj_description" style="font-family: 'times new roman'; font-size: 17px;"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Description</p><div class="textBG" style="border-radius: 10px; padding: 10px; border-style: dotted; border-width: 2px; background-color: rgb(234, 235, 255);"><span style="float: right;"><a target=_blank target="_blank" href="http://uva.onlinejudge.org/external/113/11300.pdf" style="color: blue; text-decoration: none;"><img width="100" height="26" border="0" title="Download as PDF" alt="Download as PDF" src="http://uva.onlinejudge.org/components/com_onlinejudge/images/button_pdf.png" /></a></span><div style="clear: both;"></div><h1 style="margin-bottom: 0px;"><br clear="ALL" /><center><table bgcolor="#0060F0"><tbody><tr><td><strong><span style="font-size:24px;color:#C0FFFF;"> <a target=_blank name="SECTION0001000000000000000000" href="http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33899" style="color: blue; text-decoration: none;">F. Spreading the Wealth</a></span> </strong></td></tr></tbody></table></center></h1><p style="margin-top: 0px; margin-bottom: 0.0001pt;"></p><h2 style="margin-bottom: 0px;"><span style="color:#0070E8;"><a target=_blank name="SECTION0001002000000000000000" href="http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33899" style="color: blue; text-decoration: none;">Problem</a></span></h2><p style="margin-top: 0px; margin-bottom: 0.0001pt;">A Communist regime is trying to redistribute wealth in a village. They have have decided to sit everyone around a circular table. First, everyone has converted all of their properties to coins of equal value, such that the total number of coins is divisible by the number of people in the village. Finally, each person gives a number of coins to the person on his right and a number coins to the person on his left, such that in the end, everyone has the same number of coins. Given the number of coins of each person, compute the minimum number of coins that must be transferred using this method so that everyone has the same number of coins.</p><h2 style="margin-bottom: 0px;"><span style="color:#0070E8;"><a target=_blank name="SECTION0001003000000000000000" href="http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33899" style="color: blue; text-decoration: none;">The Input</a></span></h2><p style="margin-top: 0px; margin-bottom: 0.0001pt;">There is a number of inputs. Each input begins with <strong>n</strong>(<strong>n</strong><1000001), the number of people in the village. <strong>n</strong> lines follow, giving the number of coins of each person in the village, in counterclockwise order around the table. The total number of coins will fit inside an unsigned 64 bit integer.</p><h2 style="margin-bottom: 0px;"><span style="color:#0070E8;"><a target=_blank name="SECTION0001004000000000000000" href="http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33899" style="color: blue; text-decoration: none;">The Output</a></span></h2><p style="margin-top: 0px; margin-bottom: 0.0001pt;">For each input, output the minimum number of coins that must be transferred on a single line.</p><h2 style="margin-bottom: 0px;"><span style="color:#0070E8;"><a target=_blank name="SECTION0001005000000000000000" href="http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33899" style="color: blue; text-decoration: none;">Sample Input</a></span></h2><p style="margin-top: 0px; margin-bottom: 0.0001pt;"></p><pre style="white-space: pre-wrap; word-wrap: break-word;">3
100
100
100
4
1
2
5
4

Sample Output

0
4


                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值