码迷,mamicode.com
首页 > 其他好文 > 详细

Uva 11300 Spreading the Wealth 中位数

时间:2020-04-09 16:43:06      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:sig   pread   hellip   lin   count   read   round   this   out   

UVA - 11300 Spreading the Wealth

Description

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.

Input

There is a number of inputs. Each input begins with n (n < 1000001), the number of people in the
village. n 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.

Output

For each input, output the minimum number of coins that must be transferred on a single line.

Simple Input

3
100
100
100
4
1
2
5
4

Simple Output

0
4

题意:n个人围一圈,每人有若干个金币,每个人可以分别给相邻的两个人若干个金币,求最少的金币转移数。

分析:如果一共有w个金币,那么每个人最后有w/n个金币。

        定义a[i]为第i个人原来的金币,x[i]为第i个人给第i+1个人的金币,A为最后每个人的金币

    那么我们可以推得a[1]+x[n]-x[1]=A

            a[2]+x[1]-x[2]=A

            a[n]+x[n-1]-x[n]=A

    移个项再代入就能得到x[2]=x[1]-(A-a[1])

              x[3]=x[1]-(2*A-a[1]-a[3])

    我们需要求得是|x1|+|x1-b1|+|x2-b2|+……+|xn-bn|需要选择中位数

技术图片
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 200000+5;
 4 typedef long long ll;
 5 struct Node{
 6     int to, dis, next;
 7 }edge[maxn << 1];
 8 ll head[maxn], cnt, p1, p2, t, ans, f[maxn][3];
 9 int n, m;
10 void Add(int u, int v, int w){
11     edge[++cnt].to = v;
12     edge[cnt].next = head[u];
13     edge[cnt].dis = w;
14     head[u] = cnt;
15 }
16 void Dfs(int rt, int fa, ll sum){
17     if (ans < sum){
18         ans = sum;
19         t = rt;
20     }
21     for (int i = head[rt]; i; i = edge[i].next){
22         int v = edge[i].to, dis = edge[i].dis;
23         if (v != fa){
24             Dfs(v, rt, sum + dis);
25         }
26     }
27 }
28 void Dfs1(int rt, int fa, int k){
29     for (int i = head[rt]; i; i = edge[i].next){
30         int v = edge[i].to, dis = edge[i].dis;
31         if (v == fa) continue;
32         f[v][k] = f[rt][k] + dis;
33         Dfs1(v, rt, k);
34     }
35 }
36 int main(){
37     scanf("%d%d", &n, &m);
38     for (int i = 1; i <= m; i++){
39         int u, v, w;
40         scanf("%d%d%d", &u, &v, &w);
41         Add(u, v, w);
42         Add(v, u, w);
43     }
44     Dfs(1, 0, 0);
45     p1 = t; ans = 0;
46     Dfs(p1, 0, 0);
47     p2 = t;
48     Dfs1(p1, 0, 1);
49     Dfs1(p2, 0, 0);
50     ll ans1 = 0;
51     for (int i = 1 ; i <= n; i++){
52         ans1 = max(ans1, min(f[i][0], f[i][1]));
53     }
54     printf("%lld", ans1 + ans);
55     return 0;
56 }
View Code

 

 

 

Uva 11300 Spreading the Wealth 中位数

标签:sig   pread   hellip   lin   count   read   round   this   out   

原文地址:https://www.cnblogs.com/ghosh/p/12667832.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!