30分,求帮助
查看原帖
30分,求帮助
396547
MilkaThePigeon楼主2021/12/11 09:45

测试样例时b会把a[1]加进去为10,不知道为什么,求帮助。

#include<bits/stdc++.h>
using namespace std;
int n,a[51],l,r,all,b,c,t;
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[n]);
		all+=a[n];
	}
	sort(a+1,a+n+1);
	scanf("%d",&l);
	scanf("%d",&r);
	if(all<l*n || all>r*n) cout<<-1;
	else
	{
		t=1;
		while(a[t]<l)
		{
			b+=l-a[t];
			//cout<<"t="<<t<<" "<<"b="<<b<<endl;
			t++;
		}
		t=n;
		while(a[t]>r)
		{
			c+=a[t]-r;
			//cout<<"t="<<t<<" "<<"c="<<c<<endl;
			t--;
		}
		b=max(b,c);
		cout<<b;
	}
	return 0;
}
2021/12/11 09:45
加载中...