Codeforces #737Div2A. A. Ezzat and Two Subsequences(模拟)

Ezzat has an array of n integers (maybe negative). He wants to split it into two non-empty subsequences a and b, such that every element from the array belongs to exactly one subsequence, and the value of f(a)+f(b) is the maximum possible value, where f(x) is the average of the subsequence x.

A sequence x is a subsequence of a sequence y if x can be obtained from y by deletion of several (possibly, zero or all) elements.

The average of a subsequence is the sum of the numbers of this subsequence divided by the size of the subsequence.

For example, the average of [1,5,6] is (1+5+6)/3=12/3=4, so f([1,5,6])=4.

Input
The first line contains a single integer t (1≤t≤103)— the number of test cases. Each test case consists of two lines.

The first line contains a single integer n (2≤n≤105).

The second line contains n integers a1,a2,…,an (−109≤ai≤109).

It is guaranteed that the sum of n over all test cases does not exceed 3⋅105.

Output
For each test case, print a single value — the maximum value that Ezzat can achieve.

Your answer is considered correct if its absolute or relative error does not exceed 10−6.

Formally, let your answer be a, and the jury’s answer be b. Your answer is accepted if and only if |a−b|max(1,|b|)≤10−6.

样例输入

4
3
3 1 2
3
-7 -6 -6
3
2 2 2
4
17 3 5 -3
样例输出

4.500000000
-12.500000000
4.000000000
18.666666667
题意

将一个给定的数组分成两个数组,将每个数组的和除以这个数组的个数,将两个数相加。
找一个最合理的分配方式使相加后的和最大,输出这个最大的和;

这个简单,就是把最大的拿出来,加上前面的平均数;

如果有测试点2,超限的,提醒一下不要用double,在数组定义里面

听懂了记得给个赞鼓励一下,码字不易,用爱发电。

上ac代码。

 我的头像

 有事你就q我;QQ2917366383

学习算法

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
  cin>>t;
	while(t--)
	{
	long long sum=0;int m;
		long long  c[100000];//这里如果用double会提示测试点2不能通过,甚至可能出现错误
	scanf("%d",&m);
		for(int i=1;i<=m;i++)
	 scanf("%lld",&c[i]);
		sort(c+1,c+m+1);		
		for(int i=1;i<=m-1;i++)
		sum+=c[i];
printf("%.9f\n",(1.0*sum/(m-1)+c[m]));
	}
 } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

算法编程张老师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值