• BZOJ 1045 [HAOI2008] 糖果传递


    1045: [HAOI2008] 糖果传递

    Time Limit: 10 Sec  Memory Limit: 162 MB
    Submit: 2786  Solved: 1218
    [Submit][Status][Discuss]

    Description

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

    Input

    小朋友个数n 下面n行 ai

    Output

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

    Sample Input

    4
    1
    2
    5
    4

    Sample Output

    4

    HINT

    100% n<=987654321


     

    Source

    题解:窝也只能写这种题了。。。跟均分纸牌一样搞出一个p数组,求个中位数。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<algorithm>
     5 #include<stack>
     6 #include<queue>
     7 #include<cstring>
     8 #define PAU putchar(' ')
     9 #define ENT putchar('
    ')
    10 using namespace std;
    11 const int maxn=1000000+10;
    12 int A[maxn],p[maxn],n;
    13 long long ans,tot,pos;
    14 inline int read(){
    15     int x=0,sig=1;char ch=getchar();
    16     for(;!isdigit(ch);ch=getchar())if(ch=='-')sig=0;
    17     for(;isdigit(ch);ch=getchar())x=10*x+ch-'0';
    18     return sig?x:-x;
    19 }
    20 inline void write(long long  x){
    21     if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x;
    22     int len=0;long long buf[20];while(x)buf[len++]=x%10,x/=10;
    23     for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return;
    24 }
    25 int main(){
    26     n=read();for(int i=0;i<n;i++)A[i]=read(),tot+=A[i];
    27     tot/=n;for(int i=1;i<n;i++)p[i]=p[~-i]+tot-A[i];
    28     sort(p,p+n);pos=p[n>>1];
    29     for(int i=0;i<n;i++)ans+=abs(p[i]-pos);write(ans);
    30     return 0;
    31 }
  • 相关阅读:
    HDU-4035 Maze
    poj 3744 Scout YYF I
    HDU 4911 Inversion
    HDU-3001 Travelling
    HDU 4539 郑厂长系列故事——排兵布阵
    poj 3311 Hie with the Pie
    poj-1185 炮兵阵地
    位运算
    HDU-1438 钥匙计数之一
    poj 3254 Corn Fields
  • 原文地址:https://www.cnblogs.com/chxer/p/4727029.html
Copyright © 2020-2023  润新知