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 ;
    } 
    
    每次做题提醒自己:题目到底有没有读懂,有没有分析彻底、算法够不够贪心、暴力够不够优雅。
  • 相关阅读:
    Title
    Title
    JS淘宝小广告
    JS淘宝浏览商品
    JS隐藏显示图片
    JS图片轮播
    C#连接数据库的方法
    让$(window).scroll()监听事件只执行一次
    axios发delete请求,后台收不到参数;
    .gitignore规则简介
  • 原文地址:https://www.cnblogs.com/spnooyseed/p/12870879.html
Copyright © 2011-2022 走看看