贪心,不懂为什么会TLE
查看原帖
贪心,不懂为什么会TLE
1287433
__ycy1124__楼主2024/7/26 08:55
#include<bits/stdc++.h>
using namespace std;
int a[200001];
int main(){
	int t;
	scanf("%d",&t);
	while(t){
		t--;
		int n;
		long long k;
		int tot;
		long long js=0;
		scanf("%d%lld",&n,&k);
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
			js+=a[i];
		}
		tot=n;
		sort(a+1,a+n+1);
		int ans=0;
		while(js>k){
			++ans;
			if(n-tot+1>a[tot]-a[1]||tot==1){
				a[1]--;
				js-=(n-tot+1);
			}
			else{
				js-=(a[tot]-a[1]);
				tot--;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}
2024/7/26 08:55
加载中...