luogu 1109

贪心,优先把人数大于r的组中的人分配给人数小于l的组。

所以只要求max(人数大于r的组中多出人数的总和,人数小于l的组中需要人数的总和)。

注意要判断是否可行。

#include"cstdio"
#include"cctype"
int max(int a,int b){return a>b? a:b;}
int read()
{
    int c,x=0; while(!isdigit(c=getchar()));
    while(x=x*10+c-'0',isdigit(c=getchar()));
    return x;
}
int main()
{
    int n=read(),a[51],ans=0,tot=0,r1=0,r2=0;
    for(int i=1; i<=n; i++)
        a[i]=read(),tot+=a[i];
    int l=read(),r=read();
    if(l*n>tot || r*n<tot)
    {
        puts("-1");
        return 0;
    }
    for(int i=1; i<=n; i++)
    {
        if(a[i]<l) r1+=l-a[i];
        if(a[i]>r) r2+=a[i]-r;
    }
    printf("%d",max(r1,r2));
    return 0;
}

 

posted @ 2018-01-21 20:10  TrassBlose  阅读(122)  评论(0编辑  收藏  举报