1048. 最小等待时间

单点时限: 2.0 sec

内存限制: 256 MB

设在 S 处提供同一服务,有 个顾客等待,顾客 需要的时间为 ,那么,应如何安排 个顾客的服务次序才能使总的等待时间达到最小(总的等待时间是每个顾客等待服务时间的总和)?

输入格式
第一行为一个整数 , 表示有几组测试数据。

每组测试数据的第一行为一个整数 (),表示有 个顾客在等待。

接下来的 行,每行一个整数 (),表示第 个用户的服务需要多少时间。

输出格式
对于每组测试数据,只输出一行,每行只包含一个整数,表示最小的总等待时间。

样例
input
2
3
1
2
3
4
91
13
43
44
output
4
169

/*
思路:根据操作系统中短进程优先原则,则等待时间将达到最短
*/
#include<iostream>
#include<algorithm>
using namespace std;

int main() {
	int t;
	cin>>t;
	for(int i = 0; i < t; i++) {
		int n;
		cin>>n;
		int a[n];
		for(int j = 0; j < n; j++)
			cin>>a[j];
		sort(a,a+n);
		long long ans=0;
		for(int j = 1; j < n; j++) {
			ans+=(n-j)*a[j-1];
		}
		cout<<ans<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40394960/article/details/105923092