1 条题解

  • 0

    #include<iostream>
    using namespace std;
    int group[1000000];
    int main()
    {
    int gnum,top,bottom;
    cin>>gnum;
    for(int i=0;i<gnum;i++)
    {
    cin>>group[i];
    }
    cin>>bottom>>top;
    long long moretop=0,morebottom=0,lesstop=0,lessbottom=0;
    /*
    moretop 所有组中大于上界的人数和
    morebottom 所有组中大于下界的人数和
    lesstop 所有组中小于上界的人数和
    lessbottom 所有组中小于下界的人数和
    */
    for(int i=0;i<gnum;i++)
    {
    if(group[i]<bottom)
    {
    lesstop+=(long long)top-group[i];
    lessbottom+=(long long)bottom-group[i];
    }
    else if(group[i]<top)
    {
    lesstop+=(long long)top-group[i];
    morebottom+=(long long)group[i]-bottom;
    }
    else
    {
    moretop+=(long long)group[i]-top;
    morebottom+=(long long)group[i]-bottom;
    }
    }
    //极端情况是每组学生数都是上界或都是下界
    //moretop>lesstop 过上界的学生数大于小于上界的学生数,也就是总学生数过多,不可以
    //morebottom<lessbottom 同上理,总学生数太少,不可以
    if(moretop>lesstop||morebottom<lessbottom)
    {
    cout<<-1<<endl;
    }
    //最少调整应取max,因为既要>=下界,又要<=上界
    else
    {
    cout<<max(lessbottom,moretop)<<endl;
    }
    return 0;
    }

  • 1

信息

ID
1252
难度
9
分类
(无)
标签
递交数
2
已通过
2
通过率
100%
上传者