zoukankan      html  css  js  c++  java
  • 问题 J: Stop Counting!

    题目描述
    The Martingale casino is creating new games to lure in new gamblers who tire of the standard fare. Their latest invention is a fast-paced game of chance called Stop Counting! , where a single customer plays with a dealer who has a deck of cards. Each card has some integer value.
    One by one, the dealer reveals the cards in the deck in order, and keeps track of the sum of the played cards and the number of cards shown. At some point before a card is dealt, the player can call “Stop Counting!” After this, the dealer continues displaying cards in order, but does not include them in the running sums. At some point after calling “Stop Counting!”, and just before another card is dealt, the player can also call “Start Counting!” and the dealer then includes subsequent cards in the totals. The player can only call “Stop Counting!” and “Start Counting!” at most once each, and they must call “Stop Counting!” before they can call “Start Counting!”. A card is “counted” if it is dealt before the player calls “Stop Counting!” or is dealt after the player calls “Start Counting!”
    The payout of the game is then the average value of the counted cards. That is, it is the sum of the counted cards divided by the number of counted cards. If there are no counted cards, the payout is 0.
    You have an ‘in’ with the dealer, and you know the full deck in order ahead of time. What is the maximum payout you can achieve?

    输入
    The first line of the input contains a single integer 1 ≤ N ≤ 1 000 000, the number of cards in the deck.
    The second line of input contains N space-separated integers, the values on the cards. The value of each card is in the range [−109 , 109 ]. The cards are dealt in the same order they are given in the input.

    输出
    Output the largest attainable payout. The answer is considered correct if the absolute error is less than 10−6 , or the relative error is less than 10−9 .

    结论就是最大的平均值就是前缀平均值亦或者后缀平均值,
    证明:假设前缀平均值为A , 元素个数为x个, 后缀平均值为B , 元素个数为y个, 然后总平均值就是(x * A + y * B) / (x + y) , 我们就假设A <= B , 然后我们就可以得出
    (x * A + y * B) / (x + y) <= (x * B + y * B) / (x + y) == B , 也就是得出了该结论的其中一个值, 当B <= A 的时候,得出另一个

    #include <iostream>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int N = 1e6 + 10 ;
    double f[N][2] ;
    double a[N] ;
    int main()
    {
    	int n ; 
    	cin >> n ;
    	for(int i = 1 ;i <= n ;i ++)
    	 cin >> a[i] ;
    	for(int i = 1 ;i <= n ;i ++)
    	 f[i][0] = f[i - 1][0] + a[i] , f[n - i + 1][1] = f[n - i + 2][1] + a[n - i + 1] ;
    	for(int i = 1 ;i <= n ;i ++)
    	 f[i][0] = f[i][0] / i , f[n - i + 1][1] = f[n - i + 1][1] / i ;
        double ans = 0 ;
        for(int i = 1 ;i <= n ; i ++ )
         ans = max(ans , max(f[i][0] , f[n - i + 1][1])) ;
        printf("%.9lf
    " , ans) ;
    	return 0 ;
    } 
    
    每次做题提醒自己:题目到底有没有读懂,有没有分析彻底、算法够不够贪心、暴力够不够优雅。
  • 相关阅读:
    K好数
    蓝桥杯 安慰奶牛
    蓝桥杯 节点选择
    模拟链表
    10588
    八数码的 八种境界
    HIT 2051
    概率DP
    数组越界溢出
    FATFS在SD卡里,写入多行数据出的问题
  • 原文地址:https://www.cnblogs.com/spnooyseed/p/12870879.html
Copyright © 2011-2022 走看看