NC219651. StopCounting!
描述
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 , the number of cards in the deck.The second line of input contains space-separated integers, the values on the cards. The value of each card is in the range . 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 , or the relative error is less than .
示例1
输入:
5 10 10 -10 -4 10
输出:
10.000000000
示例2
输入:
4 -3 -1 -4 -1
输出:
0.000000000
Python3(3.9) 解法, 执行用时: 1602ms, 内存消耗: 130652K, 提交时间: 2021-03-22 22:15:10
read=lambda:map(int,input().split()) a,=read() save=[*read()] pre=[] suf=[] tot=0 for each in save: tot+=each pre.append((tot)/(len(pre)+1)) save.reverse() tot=0 for each in save: tot+=each suf.append((tot)/(len(suf)+1)) res=0.0000 for i in range(len(pre)): res=max(res,max(suf[i],pre[i])) print("%.9f\n"%res)
C++(clang++11) 解法, 执行用时: 180ms, 内存消耗: 8188K, 提交时间: 2021-05-10 20:36:21
#include<stdio.h> double a[1001234]; int main() { int n,i,j; double sum=0; scanf("%d",&n); double maxx=0; for(i=1;i<=n;i++) { scanf("%lf",&a[i]); sum+=a[i]; if(sum/i>maxx) maxx=sum/i; } sum=0; for(i=n;i>=1;i--) { sum+=a[i]; if(sum/(n-i+1)>maxx) maxx=sum/(n-i+1); } printf("%.9lf\n",maxx); }