Funny Funny Game

来源:互联网 发布:索尼降噪豆测评 知乎 编辑:程序博客网 时间:2024/05/21 17:20

Funny Funny Game

Time Limit: 1 Sec  Memory Limit: 128 MB
Submissions: 12  Solved: 10

Description

 

Super Hanhan(SH) loves playing all kinds of little games very much. Today rocket323 recommends a new game to SH.

The game’s name is “Eating stones”, and it’s played like this: in a single round, the game system will randomly generate a sequence of N numbers(all of these N numbers are unique) representing the size of stones, and place them from left to right in a single line. Each time the player can choose one stone to eat, and then get a score of P points, which equal to number of remaining stones which are smaller than the chosen stone and to the left of the chosen stone.

SH wants to maximize the score he can get, however, he can’t figure out the best strategy to reach the goal. So he asks you(a talented programmer) for help.

Please help the poor SH find out the best strategy and tell him the expected scores he can get under this strategy.

 

Input

 

There are multiple cases.

The first line is an integer T representing the number of test cases.

The following T lines each line representing a test case. For each case there is exactly one line containing a single number N, representing the number of stones in the game. (1 <= N <= 1000000)

 

Output

 

For each test case, output a line containing the answer in the following format:

Case #K: X

K is the case number starting from 1, and X must be printed as a real number with two-digit precision, and the last decimal digit must be rounded.

 

Sample Input

213

Sample Output

Case #1: 0.00Case #2: 1.50
题目描述:给定n个石子从左往右,吃n次,每次吃一个石头,分数加上该石头左边还剩的石头数
求最后得分的期望
分析:其实暴力模拟这个问题并不难,而且存在着大量的重叠子问题
比如当前石头序列为n,那么我们这轮有n种选择,每一种选择的后继状态是一个n-1的子问题
第i种选择表示吃第i个石头,那么当前的得分为i,我们定义dp[x]为该规模下总得分
很容易知道 总的情况数有x!这么多,那么期望就是dp[x]/x!
第i个选择共有(x-1)!种后继情况,而该轮得分为x-1,于是我们只要再加上x-1规模下的总得分就可以了
最后暴力发现该序列是一个二阶等差数列,于是乎可以O(1)的获得答案,即便不找规律
其实对暴力程序稍作修改,同样可以得到满意的解答,注意到dp[x]=sum((x-1)!*(i-1)+dp[x-1])(1<=i<=x)
前面部分可以预处理,后面部分是死的,于是可以得到一个线性的解决方案
C++ CODE   :No Title Code
 1 2 3 4 5 6 7 8 910111213141516171819202122232425262728293031323334
#include<iostream>#include<cstdio>using namespace std;const int maxn=21;typedef long long LL;LL dp[maxn],sum[maxn];LL dfs(int x){    if(dp[x]!=-1)return dp[x];     dp[x]=0;    for(int i=1;i<=x;i++){        dp[x]+=sum[x-1]*(i-1)+dfs(x-1);            }    return dp[x];        }int main(){    /*sum[0]=1;    for(int i=1;i<maxn;i++)sum[i]=sum[i-1]*i;    memset(dp,-1,sizeof(dp));    for(int i=1;i<maxn;i++){        dfs(i);        printf("i=%d %.1lf/n",i,(double)dp[i]/sum[i]);            }     system("pause");*/    int test;    scanf("%d",&test);    for(int caseid=1;caseid<=test;caseid++){        int x;        LL n;        scanf("%d",&x);        n=x;        printf("Case #%d: %.2lf/n",caseid,(n*n-n)/4.0);            }        return 0;    }
原创粉丝点击