ZOJ 2091 Mean of Subsequence(博弈,反证法,结论巧妙)
2015-05-27 19:48
337 查看
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后面几个数的平均数最大 与题设矛盾(转载)
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-7 using 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 -x[i-1])/(n-i+1); if(p>s) s=p; } printf("%.6lf\n",s); } return 0; }
相关文章推荐
- ZOJ-2091-Mean of Subsequence
- ZOJ 2091 Mean of Subsequence
- zoj 2091 Mean of Subsequence(奇怪的贪心)
- ZOJ-2091-Mean of Subsequence (反证法的运用!!)
- ZOJ-2091-Mean of Subsequence (反证法的运用!!)
- ZOJ-2091-Mean of Subsequence (反证法的运用!!)
- ZOJ 3964 Yet Another Game of Stones (博弈)
- ZOJ 3964 Yet Another Game of Stones(Nim博弈 待整理)
- Yet Another Game of Stones ZOJ - 3964 (博弈)
- ZOJ 3964 Yet Another Game of Stones (博弈)
- zoj1026 Modular multiplication of polynomials
- zoj 2136 Longest Ordered Subsequence
- 2013年5月11日zoj比赛残留题 zoj5000 Density of Power Network && zoj5002 Friends
- ZOJ 2830 Champion of the swordsmanship
- ZOJ 2283 Challenge of Wisdom
- ZOJ 3198 Intersection of Two Sets
- zoj 2136 - Longest Ordered Subsequence
- 博弈类题目小结(HDU,POJ,ZOJ)
- ZOJ 3872 Beauty of Array
- ZOJ 1733(Common Subsequence)