最近连续三次TC爆零了,,,我的心好痛。
不知怎么想的,这题把题意理解成,第一次选择j,第二次选择i后,只能从1~i-1、i+1~j找,其实还可以从j+1~n中找,只要没有被选中过就行。。。
【题意】
给出n个蛋糕,第i个蛋糕的宽度是i,每个蛋糕有一个娱乐值d[i],(题目中是从0开始的,我这里暂时视为从1开始)一开始从所有蛋糕中等概率拿出一个蛋糕,设它的宽度为k。
接下来,等概率地从剩余蛋糕中选择,【1】如果选出的蛋糕宽度大于K,则终止选择,【2】如果小于K,则继续以同样的方法等概率地在剩余的蛋糕中选择。。直到不能再选择或者选择终止为止。
求所选出的蛋糕娱乐和的期望。
【解】
设dp[now][cur]为剩余n个蛋糕,并且上一次选择是cur+1,即选择1~cur会发生上边【2】的情况。
那么dp[now][cur]=sigema( (dp[now-1][i-1]+a[i])/now ) (1<=i<=cur) ,就是选中i后剩下now-1个蛋糕,下次可选的是前i-1个蛋糕
写成记忆化搜索也可以,直接枚举也可以(貌似记忆化搜索更容易想)。
#include<bits/stdc++.h> #define eps 1e-9 #define ALL(x) x.begin(),x.end() #define INS(x) inserter(x,x.begin()) #define FOR(i,j,k) for(int i=j;i<=k;i++) #define MAXN 1005 #define MAXM 40005 #define INF 0x3fffffff using namespace std; typedef long long LL; int i,j,k,n,m,x,y,T,ans,big,cas,num; bool flag; double dp[255][255]; bool vis[255][255]; int a[255]; class RandomPancakeStack { public: double dfs(int n,int cur)//n个剩余,可选1~cur { if (vis[n][cur]) return dp[n][cur]; vis[n][cur]=1; dp[n][cur]=0; for (i=1;i<=cur;i++) { dp[n][cur]+=(dfs(n-1,i-1)+a[i])/n; } return dp[n][cur]; } double expectedDeliciousness(vector <int> d) { int n=d.size(); memset(vis,0,sizeof(vis)); for (i=0;i<n;i++) a[i+1]=d[i]; return dfs(n,n); } };
附上题目:
Problem Statement |
|||||||||||||
Charlie has N pancakes. He wants to serve some of them for breakfast. We will number the pancakes 0 through N-1. For each i, pancake i has width i+1 and deliciousness d[i]. Charlie chooses the pancakes he is going to serve using the following randomized process: He starts by choosing the first pancake uniformly at random from all the pancakes he has. He places the chosen pancake onto a plate. This pancake now forms the bottom of a future stack of pancakes. Then, Charlie repeats the following procedure:
You are given the vector <int> d with N elements. The total deliciousness of a serving of pancakes is the sum of the deliciousness of all pancakes used in the serving. Compute and return the expected value of the total deliciousness of the pancakes chosen by Charlie. |
|||||||||||||
Definition |
|||||||||||||
|
|||||||||||||
Limits |
|||||||||||||
|
|||||||||||||
Notes |
|||||||||||||
- | Your return value must have an absolute or relative error smaller than or equal to 1e-6 | ||||||||||||
Constraints |
|||||||||||||
- | The number of elements in d will be between 1 and 250, inclusive. | ||||||||||||
- | Each element of d will be between 1 and 1,000, inclusive. | ||||||||||||
Examples |
|||||||||||||
0) | |||||||||||||
|
|||||||||||||
1) | |||||||||||||
|
|||||||||||||
2) | |||||||||||||
|
|||||||||||||
3) | |||||||||||||
|
|||||||||||||
4) | |||||||||||||
|
|||||||||||||
5) | |||||||||||||
|
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.