ZOJ 2091 Mean of Subsequence(博弈,反证法,结论巧妙)

来源:互联网 发布:web3.0与云计算 编辑:程序博客网 时间:2024/05/21 18:43
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


题意:其实就是找后几个数的平均值的最大值!! (贪心策略!要找对)

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

为什么呢??

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

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

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


#include<iostream>  #include<algorithm>  #include<string>  #include<set> #include<vector>#include<cmath>#include<queue>#include<string.h>  #include<stdlib.h>  #include<stdio.h>#define ll long long #define eps 10e-7using namespace std;double x[100001];int main(){int n;while(cin>>n){x[0]=0;for(int i=1;i<=n;++i){cin>>x[i];x[i]+=x[i-1];}double s=-100000000;  //注意这里不能设成0 for(int i=1;i<=n;++i){double p=(double)(x[n]-x[i-1])/(n-i+1);if(p>s)s=p;}printf("%.6lf\n",s);}return 0;}



0 0