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

bzoj 1045

时间:2015-05-17 10:38:37      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:

1045: [HAOI2008] 糖果传递

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 2628  Solved: 1134
[Submit][Status][Discuss]

Description

有n个小朋友坐成一圈,每人有ai个糖果。每人只能给左右两人传递糖果。每人每次传递一个糖果代价为1。

Input

小朋友个数n 下面n行 ai

Output

求使所有人获得均等糖果的最小代价。

Sample Input

4
1
2
5
4

Sample Output

4

HINT

 

数据规模

30% n<=1000

100% n<=100000

 

中位数定理

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<string>
#include<algorithm>
using namespace std;
long long n,a[1000005],p[1000005],tot;
long long ans;
int main()
{
    scanf("%lldd",&n);
    for(int i=0;i<n;i++)
        scanf("%lld",&a[i]),tot+=a[i];
    tot=tot/n;
    for(int i=1;i<n;i++)
        p[i]=p[i-1]+tot-a[i];
    sort(p,p+n);
    int sum;
    sum=p[n/2];
    for(int i=0;i<n;i++)
        ans+=abs(p[i]-sum);
    printf("%lld\n",ans);
    return 0;
}

  

 

bzoj 1045

标签:

原文地址:http://www.cnblogs.com/water-full/p/4509216.html

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