543543543
2024-04-23 20:37:44
发布于:江西
24阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
int a[100]={};
int maxx(int stepb,int stepc)//比较大小函数
{
if(stepb>=stepc) return stepb;
if(stepc>=stepb) return stepc;
}
int main()
{
int n,l,r;
int all=0,stepb=0,stepc=0;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
all=all+a[i];//总和
}
cin>>l>>r;
if((all<n*l)||(all>n*r)){//如果总和都不在规定范围内,就不可能有解
cout<<"-1";
return 0;
}
for(int j=1;j<=n;j++){
if(a[j]>r) stepb=stepb+a[j]-r;
if(a[j]<l) stepc=stepc+l-a[j];
}
cout<<maxx(stepb,stepc);//要都满足,就要取最大值(有可能不一样大)
return 0;
}
这里空空如也
有帮助,赞一个