ZOJ Problem Set-2091Mean of SubsequenceTime Limit: 2 Seconds Memory Limit: 65536 KBGiven 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
Author:
SHI, Xiaohan
Source:
ZOJ Monthly, March 2004
In fact, it is the maximum value of the average of the last few digits !! (Greedy policy! Find the correct one)
K = 1, 2, 3 ...... N: Mark k and the k whose average value is the largest behind k for maxk.
Once Roy selects this maxk, PMH will definitely maximize the length of the selected number.
Why ??
It is proved by the inverse proof: If the last number of the selected length is not the last number n, but the number t between maxk and n
That is to say, ave (maxk... t) <ave (maxk... n)
In this case, ave (t + 1... n)> ave (maxk... n) indicates that the mean of the numbers after t + 1 is the largest and the question is set.
AC code:
# Include <stdio. h> int str [10000]; int main () {double ans, sum; int n, m, I; while (scanf ("% d", & n )! = EOF) {if (n = 0) {putchar (10); continue;} sum = 0; for (I = 0; I <n; I ++) {scanf ("% d", & str [I]); sum + = str [I];} ans = sum/n; m = n; for (I = 0; I <n-1; I ++) {sum-= str [I]; m --; if (sum/m> = ans) ans = sum/m ;} printf ("%. 6lf \ n ", ans);} return 0 ;}
ZOJ-2091-Mean of Subsequence !!)