您的位置:首页 > 产品设计 > UI/UE

ZOJ-2091-Mean of Subsequence

2009-05-30 00:53 190 查看
Given N numbers in a line, we can determine a continuous subsequence by giving its start position and its length.
PMH and Roy played a game the other day. Roy gives the start position first, then PMH gives the length. Roy wants the mean of the subsequence as large as possible, while PMH wants it as small as possible.

You are to calculate the best result Roy can get, assuming PMH is very clever.

Input

There are multiple testcases.

Each testcase begins with a line containing N only.

The following line contains N numbers, separated by spaces.

Output

For each testcase, you are to print the best mean of subsequece Roy can get, precise to 6 digit after decimal point.

Sample Input

10
2 10 4 6 5 10 10 2 3 2

Sample Output

5.777778

其实就是找后k个数的平均值的最大值 k=1,2,3……n ,记做kmax

因为一旦Roy选了这个kmax,PMH必定会将所选数字长度最大化

用反证法证明:如果所选长度的最后一个数字不是n 而是kmax与n中间的某个数t

那么也就是说ave(kmax...t)<ave(kmax...n)

那么必有 ave(t+1...n)>ave(kmax...n) 说明最后t+1个数的平均数最大 与题设矛盾

#include <stdio.h>
#define MAXNUM 10000

int main()
{
{
{
{
sum-=num[i];
ave=sum*1.0/t--;
if (ave>max)
max=ave;
}
printf("%.6f\n",max);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: