zoukankan      html  css  js  c++  java
  • bzoj 1045

    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
    ",ans);
        return 0;
    }
    

      

     
  • 相关阅读:
    第二十天笔记
    第十九天笔记
    第十七天笔记
    第十五天笔记
    第十六天笔记
    第十二天笔记
    数字三角形
    最大子段和与最大子矩阵和
    分组背包
    二维背包
  • 原文地址:https://www.cnblogs.com/water-full/p/4509216.html
Copyright © 2011-2022 走看看