zoukankan      html  css  js  c++  java
  • UVa 11300 Spreading the Wealth

    题意:n个人坐成一圈,分金币(只能传递金币给相邻的人),使得最后每个人的金币数相等,问最少传送多少个金币

    白书上讲得很清楚, 最开始不懂的是这个式子

    |x1| + |x1-c1| + | x1 - c2|+-----

    后来才发现要求的就是,x1 + x2 + x3 + x4 + ----

    x1-c1对应x2,,

    x1-c2对应x3------

    然后c0是等于0的

    我真是太捉急了-------------------5555555555

     1 #include<iostream>  
     2 #include<cstdio>  
     3 #include<cstring> 
     4 #include <cmath> 
     5 #include<stack>
     6 #include<vector>
     7 #include<map> 
     8 #include<set>
     9 #include<queue> 
    10 #include<algorithm>  
    11 using namespace std;
    12 
    13 typedef long long LL;
    14 const int INF = (1<<30)-1;
    15 const int mod=1000000007;
    16 const int maxn=1000005;
    17 
    18 int n;
    19 LL a[maxn],c[maxn];
    20 
    21 int main(){
    22     while(scanf("%d",&n) != EOF && n){
    23         memset(a,0,sizeof(a));
    24         memset(c,0,sizeof(c));
    25         LL ret=0,M=0;
    26         for(int i = 1;i <= n;i++) scanf("%lld",&a[i]),ret += a[i];
    27         M = ret / n;
    28         c[0] = 0;
    29         for(int i = 1;i < n;i++) c[i] = c[i-1] + a[i] - M;
    30         sort(c,c+n);
    31         LL x1 = c[n/2];
    32         LL ans = 0;
    33         for(int i = 0;i < n;i++) ans += abs(x1 - c[i]);
    34         printf("%lld
    ",ans);
    35     }
    36     return 0;
    37 }
    View Code
  • 相关阅读:
    PHP标准库 (SPL) 笔记
    PHP反射
    PHPer书单
    深入理解面向对象——六大基本原则
    Session自定义存储及分布式存储
    06- Shell脚本学习--其它
    05- Shell脚本学习--函数
    04- Shell脚本学习--条件控制和循环语句
    03- Shell脚本学习--字符串和数组
    02- Shell脚本学习--运算符
  • 原文地址:https://www.cnblogs.com/wuyuewoniu/p/4621035.html
Copyright © 2011-2022 走看看