Help Me Escape


Time Limit: 2 Seconds      Memory Limit: 32768 KB


Background

    If thou doest well, shalt thou not be accepted? and if thou doest not well, sin lieth at the door. And unto thee shall be his desire, and thou shalt
rule over him. 

    And Cain talked with Abel his brother: and it came to pass, when they were in the field, that Cain rose up against Abel his brother, and slew him. 

    And the LORD said unto Cain, Where is Abel thy brother? And he said, I know not: Am I my brother's keeper? 

    And he said, What hast thou done? the voice of thy brother's blood crieth unto me from the ground. 

    And now art thou cursed from the earth, which hath opened her mouth to receive thy brother's blood from thy hand; 

    When thou tillest the ground, it shall not henceforth yield unto thee her strength; a fugitive and a vagabond shalt thou be in the earth.

—— Bible Chapter 4

Now Cain is unexpectedly trapped in a cave with N paths. Due to LORD's punishment, all the paths are zigzag and dangerous. The difficulty of the ith path is ci.

Then we define f as the fighting capacity of Cain. Every day, Cain will be sent to one of the N paths randomly.

Suppose Cain is in front of the ith path. He can successfully take ti days to escape from the cave as long as his fighting capacity f is larger than ci.
Otherwise, he has to keep trying day after day. However, if Cain failed to escape, his fighting capacity would increase ci as the result of actual combat. (A kindly reminder: Cain will never died.)

As for ti, we can easily draw a conclusion that ti is closely related to ci. Let's use the following function to describe their relationship:

After D days, Cain finally escapes from the cave. Please output the expectation of D.

Input

The input consists of several cases. In each case, two positive integers N and f (n ≤ 100, f ≤ 10000) are given in the first line. The second
line includes N positive integers ci (ci ≤ 10000, 1 ≤ i ≤ N)

Output

For each case, you should output the expectation(3 digits after the decimal point).

Sample Input

3 1
1 2 3

Sample Output

6.889

题意:

dfs记忆话搜索方式:

/*题意:
一仅仅吸血鬼,有n条路给他走,每次他随机走一条路,
每条路有个限制,假设当时这个吸血鬼的攻击力大于
等于某个值,那么就会花费t天逃出去,否则,花费1天
的时间,而且攻击力添加,问他逃出去的期望
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#define INF 99999999
typedef long long LL;
using namespace std; const int MAX=20000+10;
int n,f,c[MAX];
double dp[MAX],p;//dp[i]表示战斗力为i时出去的期望 double dfs(int f){
if(dp[f]>0)return dp[f];
for(int i=1;i<=n;++i){
if(f>c[i])dp[f]+=(int)(p*c[i]*c[i])*1.0/n;
else dp[f]+=(dfs(f+c[i])+1)*1.0/n;
}
return dp[f];
} int main(){
p=(1.0+sqrt(5))/2.0;
while(~scanf("%d%d",&n,&f)){
memset(dp,0,sizeof dp);
for(int i=1;i<=n;++i)scanf("%d",&c[i]);
printf("%.3lf\n",dfs(f));
}
return 0;
}

递推方式:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#define INF 99999999
typedef long long LL;
using namespace std; const int MAX=20000+10;
int n,f,c[MAX];
double dp[MAX],p;//dp[i]表示战斗力为i时出去的期望 LL cal(){
LL sum=0;
for(int i=1;i<=n;++i)sum+=(int)(p*c[i]*c[i]);
return sum;
} int main(){
p=(1.0+sqrt(5))/2.0;
while(~scanf("%d%d",&n,&f)){
int maxc=0;
for(int i=1;i<=n;++i){scanf("%d",&c[i]);maxc=max(c[i],maxc);}
LL sum=cal();
for(int i=maxc+1;i<maxc+maxc+1;++i)dp[i]=sum*1.0/n;
for(int i=maxc;i>=f;--i){
dp[i]=0;
for(int j=1;j<=n;++j){
if(i>c[j]){
dp[i]+=int(p*c[j]*c[j])*1.0/n;
}else{
dp[i]+=(dp[i+c[j]]+1)/n;
}
}
}
printf("%.3lf\n",dp[f]);
}
return 0;
}

ZOJ3640之简单慨率DP的更多相关文章

  1. hdu4035之经典慨率DP

    Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submi ...

  2. 【bzoj3687】简单题 背包dp+STL-bitset

    题目描述 小呆开始研究集合论了,他提出了关于一个数集四个问题:1.子集的异或和的算术和.2.子集的异或和的异或和.3.子集的算术和的算术和.4.子集的算术和的异或和.目前为止,小呆已经解决了前三个问题 ...

  3. 简单状压dp的思考 - 最大独立集问题和最大团问题 - 壹

    本文参考:CPH ,USACO Guide (大佬请越过,这是初学笔记,不要吐槽内容) 前置知识:位运算基础,动态规划基础 介绍 状态是元素的子集的动态规划算法,可以用位运算来高效的优化. 那么第一道 ...

  4. [CF225C] Barcode (简单DAG上dp)

    题目链接:http://codeforces.com/problemset/problem/225/C 题目大意:给你一个矩阵,矩阵中只有#和.两种符号.现在我们希望能够得到一个新的矩阵,新的矩阵满足 ...

  5. cojs 简单的数位DP 题解报告

    首先这道题真的是个数位DP 我们考虑所有的限制: 首先第六个限制和第二个限制是重复的,保留第二个限制即可 第五个限制在转移中可以判断,不用放在状态里 对于第一个限制,我们可以增加一维表示余数即可 对于 ...

  6. [Swust OJ 648]--简单字典(数位dp)

    题目链接:http://acm.swust.edu.cn/problem/0648/ Time limit(ms): 1000 Memory limit(kb): 65535   有这样一本字典,它每 ...

  7. CF 319C(Kalila and Dimna in the Logging Industry-斜率DP,注意叉积LL溢出)

    C. Kalila and Dimna in the Logging Industry time limit per test 2 seconds memory limit per test 256 ...

  8. HDU3480-Division-斜率dp

    首先想到的就是sort一下,然后每个集合都在排过序的数组里面取,不重复. 这样就推出公式dp[i][j] = min(dp[k][j-1] + (s[i]-s[k+1])^2) 其中dp[i][j]为 ...

  9. HDU3507-Print Article-斜率dp入门题

    为了学CDQ分治,从斜率dp和凸包开始做吧.. 代码就是维护一个凸包.利用递增的性质丢掉不合适的点. http://www.cnblogs.com/Rlemon/p/3184899.html 代码学的 ...

随机推荐

  1. NSUserDefaults API简单的介绍和使用英文文件

    Overview The NSUserDefaults class provides a programmatic interface for interacting with the default ...

  2. 讲座:html5于canvas疯狂的炮轰实现

    <html> <head> <title>坎农</title> <script src="../js/jscex.jscexRequir ...

  3. Java单链逆转

    本文介绍两种方法单向链表反转.记录,如下面: 1. package com.leetcode; public class ListReverse { public static void main(S ...

  4. Java 新特性(7) - Java EE 7 新特性

    http://www.ibm.com/developerworks/cn/java/j-lo-javaee7/ 新特性主要集中在: 1. 提高开发人员的生产力 2. 加强对 HTML5 动态可伸缩应用 ...

  5. 理解git经常使用命令原理

    git不同于类似SVN这样的版本号管理系统,尽管熟悉经常使用的操作就能够满足大部分需求,但为了在遇到麻烦时不至于靠蛮力去尝试,了解git的原理还是非常有必要. 文件 通过git管理的文件版本号信息所有 ...

  6. Codeforces Round #243 (Div. 2) Problem B - Sereja and Mirroring 解读

    http://codeforces.com/contest/426/problem/B 对称标题的意思大概是.应当指出的,当线数为奇数时,答案是线路本身的数 #include<iostream& ...

  7. Linux内核分析(五)----字符设备驱动实现

    原文:Linux内核分析(五)----字符设备驱动实现 Linux内核分析(五) 昨天我们对linux内核的子系统进行简单的认识,今天我们正式进入驱动的开发,我们今后的学习为了避免大家没有硬件的缺陷, ...

  8. iOS发展系列II - UILabel 使用摘要

    // 初始化标签 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 300, 150)]; // 设置标签文字 l ...

  9. 【ThinkingInC++】2、输入和输出流

    /** *特征:输入和输出流 *时间:2014年8月8日07:37:35 *作者:cutter_point */ #include<iostream> using namespace st ...

  10. JavaScript中的各种奇葩问题

    原文:JavaScript中的各种奇葩问题 JavaScript浮点数 var a = (0.1 + 0.2) + 0.3; var b = 0.1 + (0.2 + 0.3); console.lo ...