ZOJ-2091-Mean of Subsequence (反证法的运用!!)

ZOJ Problem Set - 2091

Mean of Subsequence


Time Limit: 2 Seconds      Memory Limit: 65536 KB


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


Author: SHI, Xiaohan

Source: ZOJ Monthly, March 2004

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

k=1,2,3……n ,记k以及k后面的数的平均值最大的那个k做maxk

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

为什么呢??

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

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

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

AC代码:

#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;
}
时间: 2024-10-29 07:21:12

ZOJ-2091-Mean of Subsequence (反证法的运用!!)的相关文章

zoj 2136 Longest Ordered Subsequence

最长上升子序列 一个数的序列 a i ,当 a 1 < a 2 < # < a N 时,称这个序列是上升的.对于给定的一个序列 ( a 1 , a 2 ,# , a N ),可以得到一些上升子序列( a i1 , a i 2 ,# , a iK ),这里 1 ≤ i1 < i 2 < # < iK ≤ N . 例如,对于序列(1, 7, 3, 5, 9, 4, 8),它包含一些上升子序列,如(1, 7),(3, 4, 8)等. 这些子序列中最长的长度是 4,比如子序列(

ZOJ 3349 Special Subsequence

Special Subsequence Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Original ID: 334964-bit integer IO format: %lld      Java class name: Main There a sequence S with n integers , and A is a special subsequence that satis

ZOJ 2672 Fibonacci Subsequence(动态规划+hash)

AlphaBlend实现透明效果,只是仅仅能针对某块区域进行alpha操作,透明度可设. TransparentBlt能够针对某种颜色进行透明,只是透明度不可设. AlphaBlend: BLENDFUNCTION bn; bn.AlphaFormat = 0; bn.BlendFlags = 0; bn.BlendOp = AC_SRC_OVER; bn.SourceConstantAlpha = 0; //透明度设置,0为不透明:255为全然透明 AlphaBlend(hMemDC,0,38

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)

Longest Common Subsequence

Problem statement: Given two strings, find the longest common subsequence (LCS). Your code should return the length of LCS. Have you met this question in a real interview? Yes Clarification What's the definition of Longest Common Subsequence? https:/

中南OJ1551: Longest Increasing Subsequence Again(分块+离散化线段树)

1551: Longest Increasing Subsequence Again Time Limit: 2 Sec  Memory Limit: 256 MB Submit: 29  Solved: 15 [Submit][Status][Web Board] Description Give you a numeric sequence. If you can demolish arbitrary amount of numbers, what is the length of the

ZOJ3349——Special Subsequence

Special Subsequence Time Limit: 5 Seconds      Memory Limit: 32768 KB There a sequence S with n integers , and A is a special subsequence thatsatisfies |Ai-Ai-1| <= d ( 0 <i<=|A|)) Now your task is to find the longest special subsequence of a cer

概率dp ZOJ 3640

Help Me Escape Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3640 Appoint description:  System Crawler  (2014-10-22) Description Background     If thou doest well, shalt thou not be accepted? an

zoj 2156 - Charlie&#39;s Change

题目:钱数拼凑,面值为1,5,10,25,求组成n面值的最大钱币数. 分析:dp,01背包.需要进行二进制拆分,否则TLE,利用数组记录每种硬币的个数,方便更新. 写了一个 多重背包的 O(NV)反而没有拆分快.囧,最后利用了状态压缩优化 90ms: 把 1 cents 的最后处理,其他都除以5,状态就少了5倍了. 说明:貌似我的比大黄的快.(2011-09-26 12:49). #include <stdio.h> #include <stdlib.h> #include <