最近连续三次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:

  1. If there are no more pancakes remaining, terminate.
  2. Choose a pancake uniformly at random from the pancakes that have not been chosen yet.
  3. If the width of this pancake is greater than the width of the pancake on top of the stack, terminate without taking it.
  4. Place the chosen pancake on top of the stack and go back to step 1.

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

    
Class: RandomPancakeStack
Method: expectedDeliciousness
Parameters: vector <int>
Returns: double
Method signature: double expectedDeliciousness(vector <int> d)
(be sure your method is public)

Limits

    
Time limit (s): 2.000
Memory limit (MB): 256
Stack limit (MB): 256

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,1,1}
Returns: 1.6666666666666667
The following scenarios may occur:
  1. With probability 1/3, Charlie chooses pancake 0 first. In this case he won't be able to add any more pancakes and the total deliciousness of his serving of pancakes will be 1.
  2. With probability 1/3, Charlie chooses pancake 1 first. What happens in the second round? With probability 1/2 he will choose pancake 0 and with probability 1/2 it will be pancake 2. In the first case the total deliciousness of Charlie's pancakes will be 2, in the second case it will be 1.
  3. With probability 1/3, Charlie chooses pancake 2 first. If he chooses pancake 0 next, the total deliciousness of his pancakes will be 2. If he happens to choose pancake 1 next (followed by pancake 0 in the third round), the total deliciousness will be 3.

Summing this up, we get the expected deliciousness to be 1/3 * (1) + 1/3 * (1/2 * 1 + 1/2 * 2) + 1/3 * (1/2 * 2 + 1/2 * 3) = 5/3 = 1.666...

1)  
    
{3,6,10,9,2}
Returns: 9.891666666666667
 
2)  
    
{10,9,8,7,6,5,4,3,2,1}
Returns: 10.999999724426809
 
3)  
    
{1,2,3,4,5,6,7,8,9,10}
Returns: 7.901100088183421
 
4)  
    
{2,7,1,8,2,8,1,8,2,8,4,5,90,4,5,2,3,5,60,2,8,74,7,1}
Returns: 19.368705050402465
 
5)  
    
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
Returns: 1.718281828459045
 

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 - 概率+记忆化搜索的更多相关文章

  1. TopCoder SRM 596 DIV 1 250

    body { font-family: Monospaced; font-size: 12pt } pre { font-family: Monospaced; font-size: 12pt } P ...

  2. 【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 ...

  3. 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) [思路] 手速太慢啦,等敲完代码的时 ...

  4. UVA1637Double Patience(概率 + 记忆化搜索)

    训练指南P327 题意:36张牌分成9堆, 每堆4张牌.每次拿走某两堆顶部的牌,但需要点数相同.如果出现多种拿法则等概率的随机拿. 如果最后拿完所有的牌则游戏成功,求成功的概率. 开个9维数组表示每一 ...

  5. Codeforces 540D Bad Luck Island - 概率+记忆化搜索

    [题意] 一个岛上有三种生物A,B,C,各有多少只在输入中会告诉你,每种最多100只 A与B碰面,A会吃掉B, B与C碰面,B会吃掉C, C与A碰面,C会吃掉A...忍不住想吐槽这种环形食物链 碰面是 ...

  6. 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 ...

  7. TopCoder SRM 560 Div 1 - Problem 1000 BoundedOptimization & Codeforces 839 E

    传送门:https://284914869.github.io/AEoj/560.html 题目简述: 定义"项"为两个不同变量相乘. 求一个由多个不同"项"相 ...

  8. TopCoder SRM 667 Div.2题解

    概览: T1 枚举 T2 状压DP T3 DP TopCoder SRM 667 Div.2 T1 解题思路 由于数据范围很小,所以直接枚举所有点,判断是否可行.时间复杂度O(δX × δY),空间复 ...

  9. HDU 5001 概率DP || 记忆化搜索

    2014 ACM/ICPC Asia Regional Anshan Online 给N个点,M条边组成的图,每一步能够从一个点走到相邻任一点,概率同样,问D步后没走到过每一个点的概率 概率DP  測 ...

随机推荐

  1. Ubuntu 创建快捷方式的方法

    ln -s  /要创建快捷方式的地方/ /创建在哪里/

  2. openerp import l field size limit

    modify the file addons/base_import/models.py add the following line at the very begining of the _con ...

  3. jquery获取元素的所有宽高(包括内边距和外边距)

    width() - 返回元素的宽度.height() - 返回元素的高度.innerWidth() 方法返回元素的宽度(包括内边距).                    innerHeight() ...

  4. WinPcap编程(一)

    0. 按着文档顺序写的. 开发环境:win10+VS2013. 配置WinPcap环境就不多说.直接给网址:http://blog.sina.com.cn/s/blog_57432f380101qh3 ...

  5. c# 哈希表集合;函数

    * 哈希表集合 1.先进去的后出来,最后进去的先出来 2.利用枚举类型打印出集合中的Key值和Value值 ** 函数 1.函数:能够独立完成某项功能的模块. 函数四要素:输入.输出.函数体.函数名 ...

  6. [置顶] 2014年八大最热门IT技能

    根据Computerworld网站组织的年度预测调查,众多IT专业人士在2014年所面临的整体就业形势与今年基本持平——今年有33%的企业有计划增加IT部门的员工数量,而未来一年则有32%的企业有此打 ...

  7. HTTP请求和响应详解

    HTTP有两部分组成:请求与响应,下面分别整理. 一.HTTP请求 1.HTTP请求格式: <request line> <headers> <blank line> ...

  8. ZOJ 3469 Food Delivery

    题目大意: 有n个人,住在一条直线上.第i个人的坐标是Xi,街上有个外卖餐馆的位置是X,现在餐厅工作人员要给街上的每个人送饭,送完之后再回到餐厅,送饭人的速度是V,每个人有个不满意值,当这个人送餐时间 ...

  9. wpa_cli与wpa_supplicant的交互命令

    1)通过adb命令行,可以直接打开supplicant,从而运行wpa_cli,可以解决客户没有显示屏而无法操作WIFI的问题,还可以避免UI的问题带到driver.进一步来说,可以用在很多没有键盘输 ...

  10. 搜索(四分树):BZOJ 4513 [SDOI2016 Round1] 储能表

    4513: [Sdoi2016]储能表 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 395  Solved: 213[Submit][Status] ...