HLG2081分蘋果

來源:互聯網
上載者:User

標籤:

蘋果
Time Limit: 1000 MS Memory Limit: 32768 K
Total Submit: 39(24 users) Total Accepted: 29(22 users) Rating: Special Judge: No
Description

圓桌旁圍坐n個人,按順序將他們編號為1~n,第i個人有xi個蘋果(i=1,2,...,n)。蘋果的總數量為A。資料保證A為n的倍數,且v=A/n。

每個人可以給左右相鄰的人蘋果,直到每個人手上的蘋果數為平均值v。

輸出需要轉移的蘋果數量的最小值。

Input

有多組測試資料。

每組測試資料的第一行為正整數n(n<=10^6),接下來有n行,每行一個整數,逆時針給出初始狀態每人手中的蘋果數xi。

處理到檔案結束。

Output

對每組測試資料,輸出轉移蘋果數量的最小值,每組一行。資料保證輸出為64位不帶正負號的整數範圍內。

Sample Input
310010010041254
Sample Output
04
Source
"科林明倫杯"哈爾濱理工大學第四屆ACM程式設計競賽(預選賽)

 

 

 

 

 

 

 

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;/*能兩邊給,那麼x1可以給x2,x2也可給x1,絕對值就是|x1-x2|,我們假設逆時針順序x2給x1,就相當於x2給了x1了a2(a2正負都OK)個蘋果,那個x1就有:A1+a2-a1=m,A2+a3-a2=m......         a2=m-A1+a1   a3=m-A2+a2=2m-A1-A2+a1   a4=3m-A1-A2-A3+a1.......由於A1,A2和m都是給定的,所以影響的只有a,也就是指a1.Ci指Ai-M 那麼a2=a1-C1,a3=a1-C2,a4=a1-C3使ai的絕對值最小,則轉換成一條直線上的點到某點上的距離之和最小的問題 則a1為這些數的中位元*/int A[1000001],C[1000001];int main(){    int n,sum;    while(~scanf("%d",&n)){        long long int sum=0;        for(int i=1;i<=n;i++)        {            scanf("%d",&A[i]);            sum+=A[i];        }        int v=sum/n;        /*計算Ci,寫寫方程代代看看*/        C[0]=0;        for(int i=1;i<=n;i++)        {            C[i]=A[i]-v+C[i-1];        }        /*求和*/        sort(C+1,C+1+n);        long long int s=0;        for(int i=1;i<=n/2;i++)        {            s=s+C[n+1-i]-C[i];        }        printf("%lld\n",s);    }    return 0;}
 

 

 

 

 

 

 

#include<stdio.h>#include<string.h>#include<iostream>#include<math.h>#include<algorithm>using namespace std;int a[1000005],c[1000005];int main(){    int n;    while(scanf("%d",&n)!=EOF)    {        long int sum=0;        for(int i=1;i<=n;i++)        {            scanf("%d",&a[i]);            sum=sum+a[i];        }        int m=sum/n;        c[0]=0;        for(int i=1;i<=n;i++)            c[i]=m+c[i-1]-a[i];        sort(c+1,c+n+1);       long  int num=0;       int temp=c[n/2+1];        for(int i=1;i<=n;i++)        {          num=num+abs(c[i]-temp);        }         printf("%ld\n",abs(num));    }    return 0;}
 

 

HLG2081分蘋果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.