Topcoder SRM 656 (Div.1) 250 RandomPancakeStack - 概率+记忆化搜索
最近连续三次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[][];
bool vis[][];
int a[];
class RandomPancakeStack
{
public:
double dfs(int n,int cur)//n个剩余,可选1~cur
{
if (vis[n][cur]) return dp[n][cur];
vis[n][cur]=;
dp[n][cur]=;
for (i=;i<=cur;i++)
{
dp[n][cur]+=(dfs(n-,i-)+a[i])/n;
}
return dp[n][cur];
} double expectedDeliciousness(vector <int> d)
{
int n=d.size();
memset(vis,,sizeof(vis));
for (i=;i<n;i++) a[i+]=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.
Topcoder SRM 656 (Div.1) 250 RandomPancakeStack - 概率+记忆化搜索的更多相关文章
- TopCoder SRM 596 DIV 1 250
body { font-family: Monospaced; font-size: 12pt } pre { font-family: Monospaced; font-size: 12pt } P ...
- 【topcoder SRM 702 DIV 2 250】TestTaking
Problem Statement Recently, Alice had to take a test. The test consisted of a sequence of true/false ...
- Topcoder SRM 661 (Div.1) 250 MissingLCM - 数论
[题意] 给你一个数N(1<=N<=10^6),要求最小的M(M>N),使得lcm(n+1,n+2,...m)=lcm(1,2,3,...,m) [思路] 手速太慢啦,等敲完代码的时 ...
- UVA1637Double Patience(概率 + 记忆化搜索)
训练指南P327 题意:36张牌分成9堆, 每堆4张牌.每次拿走某两堆顶部的牌,但需要点数相同.如果出现多种拿法则等概率的随机拿. 如果最后拿完所有的牌则游戏成功,求成功的概率. 开个9维数组表示每一 ...
- Codeforces 540D Bad Luck Island - 概率+记忆化搜索
[题意] 一个岛上有三种生物A,B,C,各有多少只在输入中会告诉你,每种最多100只 A与B碰面,A会吃掉B, B与C碰面,B会吃掉C, C与A碰面,C会吃掉A...忍不住想吐槽这种环形食物链 碰面是 ...
- Codeforces Round #338 (Div. 2) B. Longtail Hedgehog 记忆化搜索/树DP
B. Longtail Hedgehog This Christmas Santa gave Masha a magic picture and a pencil. The picture con ...
- TopCoder SRM 560 Div 1 - Problem 1000 BoundedOptimization & Codeforces 839 E
传送门:https://284914869.github.io/AEoj/560.html 题目简述: 定义"项"为两个不同变量相乘. 求一个由多个不同"项"相 ...
- TopCoder SRM 667 Div.2题解
概览: T1 枚举 T2 状压DP T3 DP TopCoder SRM 667 Div.2 T1 解题思路 由于数据范围很小,所以直接枚举所有点,判断是否可行.时间复杂度O(δX × δY),空间复 ...
- HDU 5001 概率DP || 记忆化搜索
2014 ACM/ICPC Asia Regional Anshan Online 给N个点,M条边组成的图,每一步能够从一个点走到相邻任一点,概率同样,问D步后没走到过每一个点的概率 概率DP 測 ...
随机推荐
- 4种检测是否支持HTML5的方法,你知道几个?
4种检测是否支持HTML5的方法,你知道几个? 1,检查特定的属性是否存在于全局的对象里面,比如说window或navigator. 比如geolocation,它是HTML5新加支持的新特性:它是由 ...
- Python新手学习基础之初识python——与众不同2
看完了Python的缩进,现在来看看Python的标识符.引号和注释. 标识符 关于Python的标识符,其实不是与众不同,只是有一定的规则. 标识符是编程时使用的名字.在Python中,标识符有几点 ...
- MySQL Explain 结果解读与实践
Explain 结果解读与实践 基于 MySQL 5.0.67 ,存储引擎 MyISAM . 注:单独一行的"%%"及"`"表示分隔内容,就象分开&qu ...
- [BZOJ 3209] 花神的数论题 【数位统计】
题目链接: BZOJ - 3209 题目大意 设 f(x) 为 x 的二进制表示中 1 的个数.给定 n ,求 ∏ f(i) (1 <= i <= n) . 题目分析 总体思路是枚 ...
- Promise 让异步更优
每个异步方法都返回一个Promise 更优雅. then方法 每一个Promise 都有一个叫then 的方法, 接受一对callback 被解决时调用,resolve, 被拒绝 reje ...
- 关于form.item不兼容的问题
form.item()能在IE下运行,在firefox中会报脚本错误,没有这个函数. 可以使用 Form.elements 方法得到 HTMLCollection 后再使用 item 方法获取表单内元 ...
- Visual C++ 8.0对象布局的奥秘:虚函数、多继承、虚拟继承(VC直接输出内存布局)
原文:VC8_Object_Layout_Secret.html 哈哈,从M$ Visual C++ Team的Andy Rich那里又偷学到一招:VC8的隐含编译项/d1reportSingleCl ...
- bzoj2818
我们先穷举素数p然后令y>x 这样问题就是求这个gcd(x,y)=p (1<=x<y=n)不难发现必须y=kp k∈N* 当y=p时,易知个数为φ(1)当y=2p 个数为φ(2), ...
- Search Insert Position——LeetCode
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- Java---实现运行任意目录下class中加了@MyTest的空参方法(实现图形界面)
说明: 因为上个代码,总是要输入完整的绝对路径,比较麻烦,于是,就写了这个小程序,直接进入文件对话框选择需要运行的class文件. 只需要提前输入完整的类名. 注意:加的MyTest必须打个包,加上: ...