ZOJ-2091-Mean of Subsequence !!)

Source: Internet
Author: User

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 !!)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.