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

  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.

转载于:https://www.cnblogs.com/zhyfzy/p/4436017.html

Topcoder SRM 656 (Div.1) 250 RandomPancakeStack - 概率+记忆化搜索相关推荐

  1. Discovering Gold LightOJ - 1030[概率dp或者记忆化搜索]

    题目大意:有一个[1,n][1,n][1,n]的数轴,数轴上的每个对应位置上都有金矿,你初始位置是1,然后你每次都会投色子决定你下一步跳到哪里,如果你跳出了nnn,那么你就要重新投.问你跳到nnn的时 ...

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

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

  3. 一个数变成0的概率有多少?(记忆化搜索)

    Description 给定一个数a0, 并给出定义:序列a1,a2,a3- 1.从闭区间[0,a0]中等概率随机选择一个整数k0,令a1=a0-k0 2.得到随机数a1后,再从闭区间[0,a1]中等 ...

  4. BZOJ.2246.[SDOI2011]迷宫探险(DP 记忆化搜索 概率)

    题目链接 求最大的存活概率,DP+记忆化. 用f[s][x][y][hp]表示在s状态,(x,y)点,血量为hp时的存活概率. s是个三进制数,记录每个陷阱无害/有害/未知. 转移时比较容易,主要是在 ...

  5. Topcoder SRM 648 (div.2)

    第一次做TC全部通过,截图纪念一下. 终于蓝了一次,也是TC上第一次变成蓝名,下次就要做Div.1了,希望div1不要挂零..._(:зゝ∠)_ A. KitayutaMart2 万年不变的水题. # ...

  6. Topcoder SRM 663 DIV 1

    ABBADiv1 题意: 规定两种操作,一种是在字符串的末尾添加A,另一种是在末尾添加B然后反转字符串.现在给你一个起始串,一个终点串,然后问你是否能够通过以上两种操作,从起始串变为终点串. 题解: ...

  7. TopCoder SRM 152 div 2 500point

    这个题目比较简单,自己在纸上写出来给的例子,然后分析一下就会发现规律了:) 我的代码 #include<vector> #include<iostream> using nam ...

  8. Topcoder SRM 628 DIV 2

    被自己蠢哭了.... 250-point problem 国际象棋棋盘上给出两个坐标,问象从一个走到还有一个最少要几步. 黑格象仅仅能走黑格,白格象仅仅能走白格,仅仅要推断两个坐标的颜色是否同样就能推 ...

  9. Topcoder SRM 638 DIV 2 (大力出奇迹)

    水题,就是一个暴力.大力出奇迹. Problem Statement   There is a narrow passage. Inside the passage there are some wo ...

  10. Topcoder SRM 637 (Div.2)

    A.GreaterGameDiv2 不能更水 1 #line 7 "GreaterGameDiv2.cpp" 2 #include<cstdio> 3 #include ...

最新文章

  1. Ambari在离线环境中安装Hadoop集群
  2. 模型可解释性-树结构可视化
  3. JVM加载class文件的原理机制
  4. 数据库连接字符串大全
  5. 谷歌大脑Wasserstein自编码器:新一代生成模型算法
  6. JVM--对象的实例化过程
  7. 使用阿里云加速docker镜像的安装
  8. php 联接sq sever,步骤 4:使用 PHP 弹性连接到 SQL
  9. 26留数及其应用(二)
  10. JAVA8的学习笔记之Collection
  11. Exchange2013/2016 ECP/OWA无法通过用户验证EventID3002/3005
  12. 【Spring-AOP】自动代理类AnnotationAwareAspectJAutoProxyCreator
  13. 小米商城官网部分代码
  14. JAVA微信小程序医院预约挂号小程序系统毕业设计 开题报告
  15. 看了《就算老公一毛钱股份都没拿到 在我心里他依然是最牛逼的创业者》小感
  16. 光流传感器 定位精度_光流传感器
  17. 记两个非常不错的技术博客
  18. ExaGrid在2021年网络计算大奖评选中大获全胜
  19. 发表论文被拒?只因你没注意这几点
  20. VAS开启LaFi空投——助力打造区块链数字黄金新生态

热门文章

  1. java servlet JSP 区别_servlet和jsp的区别
  2. 基于SSM的理财系统
  3. 【mybatisPlus】mybatis基本使用
  4. 三、运算符、表达式和语句
  5. Oracle 11g r2 下载地址
  6. Thymeleaf-extras-Spring Security 权限控制
  7. Win10 查看开/关机历史记录、 删除管理员账户密码
  8. Git 命令 clone、add、status、commit、push、init 使用介绍.
  9. 阶段1 语言基础+高级_1-3-Java语言高级_08-JDK8新特性_第2节 Stream流式思想概述_2_使用Stream流的方式,遍历集合...
  10. 解决stackoverflow加载慢的插件