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

    解题思路:给定数轴上的 n 个点,中位数离所有点的距离之和最小

     1 ///////////////////////////////////////////////////////////////////////////
     2 //problem_id: uva 11300
     3 //user_id: SCNU20102200088
     4 ///////////////////////////////////////////////////////////////////////////
     5 
     6 #include <algorithm>
     7 #include <iostream>
     8 #include <iterator>
     9 #include <iomanip>
    10 #include <cstring>
    11 #include <cstdlib>
    12 #include <string>
    13 #include <vector>
    14 #include <cstdio>
    15 #include <cctype>
    16 #include <cmath>
    17 #include <queue>
    18 #include <stack>
    19 #include <list>
    20 #include <set>
    21 #include <map>
    22 using namespace std;
    23 
    24 ///////////////////////////////////////////////////////////////////////////
    25 #pragma comment(linker,"/STACK:1024000000,1024000000")
    26 
    27 #define lson l,m,rt<<1
    28 #define rson m+1,r,rt<<1|1
    29 ///////////////////////////////////////////////////////////////////////////
    30 
    31 ///////////////////////////////////////////////////////////////////////////
    32 const double EPS=1e-8;
    33 const double PI=acos(-1.0);
    34 
    35 const int x4[]={-1,0,1,0};
    36 const int y4[]={0,1,0,-1};
    37 const int x8[]={-1,-1,0,1,1,1,0,-1};
    38 const int y8[]={0,1,1,1,0,-1,-1,-1};
    39 ///////////////////////////////////////////////////////////////////////////
    40 
    41 ///////////////////////////////////////////////////////////////////////////
    42 typedef long long LL;
    43 
    44 typedef int T;
    45 T max(T a,T b){ return a>b? a:b; }
    46 T min(T a,T b){ return a<b? a:b; }
    47 T gcd(T a,T b){ return b==0? a:gcd(b,a%b); }
    48 T lcm(T a,T b){ return a/gcd(a,b)*b; }
    49 ///////////////////////////////////////////////////////////////////////////
    50 
    51 ///////////////////////////////////////////////////////////////////////////
    52 //Add Code:
    53 LL a[1000005],b[1000005];
    54 ///////////////////////////////////////////////////////////////////////////
    55 
    56 int main(){
    57     ///////////////////////////////////////////////////////////////////////
    58     //Add Code:
    59     int n,i;
    60     while(scanf("%d",&n)!=EOF){
    61         LL sum=0;
    62         for(i=1;i<=n;i++){
    63             scanf("%lld",&a[i]);
    64             sum+=a[i];
    65         }
    66         LL avg=sum/n;
    67         b[0]=0;
    68         for(i=1;i<n;i++) b[i]=b[i-1]+a[i]-avg;
    69         sort(b,b+n);
    70         LL mid=b[n/2],ans=0;
    71         for(i=0;i<n;i++) ans+=abs(mid-b[i]);
    72         printf("%lld
    ",ans);
    73     }
    74     ///////////////////////////////////////////////////////////////////////
    75     return 0;
    76 }
    77 
    78 ///////////////////////////////////////////////////////////////////////////
    79 /*
    80 Testcase:
    81 Input:
    82 3
    83 100
    84 100
    85 100
    86 4
    87 1
    88 2
    89 5
    90 4
    91 Output:
    92 0
    93 4
    94 */
    95 ///////////////////////////////////////////////////////////////////////////
  • 相关阅读:
    webpack浅析
    MongoDB简易
    async函数
    react -- context
    抓包移动端方法
    stateless 无状态组件
    移动端适配
    JS和JQuery获取和修改Label的值的示例代码
    HTML DOM querySelector() 方法
    filter
  • 原文地址:https://www.cnblogs.com/linqiuwei/p/3313717.html
Copyright © 2011-2022 走看看