列表

详情


NC207571. SumoandHisFollowers

描述

Sumo GG is very popular in the laboratory. Many people come to ask Sumo GG for questions.

Now there are n people in line to ask him for advice, as the time for i-th people to ask him questions is . In order not to affect everyone's lunch, please line up for n people so that the average waiting time of n people is the minimum.

输入描述

The first line contains  — the number of test cases.

For each test case, the first line contains a single integer , the number of people waiting for asking a question.

The next line contains n integers , the time of people required to ask a question.

输出描述

For each of the test cases, output a single integer, the minimun average waiting time of n people (accurate to two decimal places).

示例1

输入:

2
1
5
10 
56 12 1 99 1000 234 33 55 99 812

输出:

0.00
291.90

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 140ms, 内存消耗: 876K, 提交时间: 2020-06-06 13:52:29

#include<bits/stdc++.h>
using namespace std;
int t, n, a[123456];
int main()
{
	for(cin >> t; t--; )
	{
		cin >> n;
		for(int i = 0; i < n; i++)
			cin >> a[i];
		sort(a, a+n);
		double sum = 0, s = 0;
		for(int i = 0; i < n-1; i++)
			s += a[i], sum += s;
		printf("%.2f\n", sum / n);
	}
}

C++11(clang++ 3.9) 解法, 执行用时: 132ms, 内存消耗: 996K, 提交时间: 2020-06-06 20:38:34

#include<bits/stdc++.h>

using namespace std;


int main(){
	int t;
	cin>>t;
	while(t--){
		int n;
		int a[100005]={};
		cin>>n;
		for(int i=0;i<n;i++)cin>>a[i];
		sort(a,a+n);
		double an=0;
		for(int i=0;i<n-1;i++){
			an+=a[i]*(n-1-i);
		} 
		printf("%.2lf\n",an/n);
	}
}

Python3(3.9) 解法, 执行用时: 507ms, 内存消耗: 10296K, 提交时间: 2021-03-23 14:09:46

for _ in range(int(input())):
    n=int(input())
    m=n
    an=list(map(int,input().split()))
    res=0
    an.sort()
    for i in an:
        res+=i*(n-1)
        n=n-1
    print('%.2f'%(res/m))

上一题