概率dp集合
bzoj1076
你正在玩你最喜欢的电子游戏,并且刚刚进入一个奖励关。在这个奖励关里,系统将依次随机抛出k次宝物,
每次你都可以选择吃或者不吃(必须在抛出下一个宝物之前做出选择,且现在决定不吃的宝物以后也不能再吃)。
宝物一共有n种,系统每次抛出这n种宝物的概率都相同且相互独立。也就是说,即使前k-1次系统都抛出宝物1(
这种情况是有可能出现的,尽管概率非常小),第k次抛出各个宝物的概率依然均为1/n。 获取第i种宝物将得到Pi
分,但并不是每种宝物都是可以随意获取的。第i种宝物有一个前提宝物集合Si。只有当Si中所有宝物都至少吃过
一次,才能吃第i种宝物(如果系统抛出了一个目前不能吃的宝物,相当于白白的损失了一次机会)。注意,Pi可
以是负数,但如果它是很多高分宝物的前提,损失短期利益而吃掉这个负分宝物将获得更大的长期利益。 假设你
采取最优策略,平均情况你一共能在奖励关得到多少分值?
题解:套了概率的记忆化搜索;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<iomanip>
using namespace std;
#define LL long long
#define up(i,j,n) for(int i=(j);i<=(n);i++)
#define FILE "dealing"
int read(){
int f=,x=,ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<='')x=(x<<)+(x<<)+ch-'',ch=getchar();
return f*x;
}
const int maxn=;
const double eps=0.0000001;
int k,n;
double v[maxn];//分数
int d[maxn][maxn],cnt[maxn];//前提集合
double f[][];
inline bool cmp(double x,double y){return abs(x-y)<=eps;}
double dfs(int pos,int S){
if(pos==)return f[pos][S]=;
if(!cmp(f[pos][S],-))return f[pos][S];
f[pos][S]=;
up(i,,n){
bool flag=;
up(j,,cnt[i])if(!(S&(<<d[i][j]-)))flag=;
if(flag)f[pos][S]+=max(dfs(pos-,S|(<<i-))+v[i],dfs(pos-,S));
else f[pos][S]+=dfs(pos-,S);
}
f[pos][S]/=(n*1.0);
return f[pos][S];
}
int main(){
freopen(FILE".in","r",stdin);
freopen(FILE".out","w",stdout);
k=read(),n=read();
up(i,,n){
v[i]=read();int x=read();
while(x){
d[i][++cnt[i]]=x;
x=read();
}
}
up(i,,k)up(j,,(<<n)-)f[i][j]=-;
printf("%.6lf\n",dfs(k,));
return ;
}
bzoj1415

这题题目描述我感觉有问题;
标解是按每次聪聪都走两步算的,而我认为聪聪可能只走一步;
其他的就按普通的概率dp计算即可;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<iomanip>
using namespace std;
#define LL long long
#define up(i,j,n) for(int i=(j);i<=(n);i++)
#define FILE "dealing"
int read(){
int f=,x=,ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<='')x=(x<<)+(x<<)+ch-'',ch=getchar();
return f*x;
}
const int maxn=;
const double eps=0.0000001;
int n,m,sx,sy;
struct node{
int y,next;
}e[maxn<<];
int linkk[maxn],len=;
void insert(int x,int y){
e[++len].y=y;
e[len].next=linkk[x];
linkk[x]=len;
}
int q[maxn<<],head,tail;
int next[maxn][maxn],d[maxn];
void bfs(int s){
head=tail=;
memset(next[s],,sizeof(next[s]));
memset(d,,sizeof(d));
q[++tail]=s;int x;
next[s][s]=;d[s]=;
while(++head<=tail){
x=q[head];
for(int i=linkk[x];i;i=e[i].next){
if(!d[e[i].y])next[s][e[i].y]=x,d[e[i].y]=d[x]+,q[++tail]=e[i].y;
if(d[e[i].y]>d[x]&&next[s][e[i].y]>x)next[s][e[i].y]=x;
}
}
}
double f[maxn][maxn];
double dfs(int x,int y){
if(x==y)return ;
if(f[x][y]!=-)return f[x][y];
if(next[x][y]==x||next[x][next[x][y]]==x)return f[x][y]=;
int sum=;
f[x][y]=;
for(int i=linkk[x];i;i=e[i].next)
sum++,f[x][y]+=dfs(e[i].y,next[x][next[x][y]]);
f[x][y]+=dfs(x,next[x][next[x][y]]);
f[x][y]=f[x][y]/sum+;
return f[x][y];
}
int main(){
freopen(FILE".in","r",stdin);
freopen(FILE".out","w",stdout);
n=read(),m=read(),sx=read(),sy=read();
up(i,,m){
int x=read(),y=read();
insert(x,y),insert(y,x);
}
up(i,,n)bfs(i);
up(i,,n)up(j,,n)f[i][j]=-;
printf("%.3lf\n",dfs(sy,sx));
return ;
}
bzoj1426收集邮票
有n种不同的邮票,皮皮想收集所有种类的邮票。唯一的收集方法是到同学凡凡那里购买,每次只能买一张,并且买到的邮票究竟是n种邮票中的哪一种是等概率的,概率均为1/n。但是由于凡凡也很喜欢邮票,所以皮皮购买第k张邮票需要支付k元钱。 现在皮皮手中没有邮票,皮皮想知道自己得到所有种类的邮票需要花费的钱数目的期望。
这题还是很神奇的;
设f[i]表示已经有了i张邮票还需要花费f[i]单位的钱买全所有邮票;
设g[i]表示买了i张邮票还需要买g[i]张邮票才能买全邮票;
g[i]很好求,买第i张邮票成功的概率(n-i)/n,第i张邮票买n/(n-i)次,然后累加就可以了;
怎么处理钱数?我们不如先给钱数多的钱,也就是说在期望再买k次就能买全的时候,买这一张用k元,之后递减,可以证明这两种付款等价;
也可以视为这张票是1元买的,而后面的每张票都贵了1元;
于是有了递推式子:
f[i]=(n-i)/n*(f[i+1]+g[i+1]+1)+i/n*(g[i]+f[i]+1)
经过转化,有了这个式子
f[i]=f[i+1]+g[i+1]+i/(n-i)*g[i]+n/(n-i)
递推即可;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<iomanip>
using namespace std;
#define LL long long
#define up(i,j,n) for(int i=(j);i<=(n);i++)
#define FILE "dealing"
int read(){
int f=,x=,ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<='')x=(x<<)+(x<<)+ch-'',ch=getchar();
return f*x;
}
const int maxn=;
const double eps=0.0000001;
int n;
double g[maxn],f[maxn];
double dfs(int i){
if(i==n)return ;
return dfs(i+)+g[i+]+i*1.0/(n-i)*g[i]+n*1.0/(n-i);
}
int main(){
freopen(FILE".in","r",stdin);
freopen(FILE".out","w",stdout);
n=read();
for(int i=n-;i>=;i--)g[i]=g[i+]+n*1.0/(n-i);
printf("%.2lf\n",dfs());
return ;
}
bzoj4008: [HNOI2015]亚瑟王
小 K 不慎被 LL 邪教洗脑了,洗脑程度深到他甚至想要从亚瑟王邪教中脱坑。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<iomanip>
using namespace std;
#define LL long long
#define up(i,j,n) for(int i=(j);i<=(n);i++)
#define FILE "dealing"
int read(){
int f=,x=,ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<='')x=(x<<)+(x<<)+ch-'',ch=getchar();
return f*x;
}
const int maxn=;
const double eps=0.0000001;
int T,n,r;
double d[],p[],f[][],g[];
double mi(double a,int b){
double ans=;
while(b){
if(b%)ans*=a;
b/=;
a*=a;
}
return ans;
}
int main(){
freopen(FILE".in","r",stdin);
freopen(FILE".out","w",stdout);
T=read();
while(T--){
n=read(),r=read();
up(i,,n)scanf("%lf%lf",&p[i],&d[i]);
memset(f,,sizeof(f));
memset(g,,sizeof(g));
f[][]=;
up(i,,n)up(j,,min(i,r)){
f[i][j]=f[i-][j]*mi(-p[i],r-j)+((j->=)?(f[i-][j-]*(-mi(-p[i],r-j+))):());
}
up(i,,n)up(j,,i-)g[i]+=f[i-][j]*mi(-p[i],r-j);
double ans=;
up(i,,n)ans+=(-g[i])*d[i];
printf("%.10lf\n",ans);
}
return ;
}
概率dp集合的更多相关文章
- HDU 4405 【概率dp】
题意: 飞行棋,从0出发要求到n或者大于n的步数的期望.每一步可以投一下筛子,前进相应的步数,筛子是常见的6面筛子. 但是有些地方可以从a飞到大于a的b,并且保证每个a只能对应一个b,而且可以连续飞, ...
- 期望与概率dp
概率与期望dp 定义: 概率:事件A发生的可能性,计作P(A) 期望:事件A结果的平均大小,记住E(x) E(x)=每种结果的大小与其概率的乘积的和 注意计算概率时需要考虑是否要用容斥原理 期望d ...
- 概率dp作业
概率dp特征: 概率DP一般求的是实际结果,在DP过程中,当前状态是由所有子状态的概率共同转移而来,所以概率DP只是利用了DP的动态而没有规划(即只需转移无需决策).-------qkoqhh A - ...
- bzoj1076 奖励关(概率dp)(状态压缩)
BZOJ 1076 [SCOI2008]奖励关 Description 你正在玩你最喜欢的电子游戏,并且刚刚进入一个奖励关.在这个奖励关里,系统将依次随机抛出k次宝物,每次你都可以选择吃或者不吃(必须 ...
- Codeforces 28C [概率DP]
/* 大连热身D题 题意: 有n个人,m个浴室每个浴室有ai个喷头,每个人等概率得选择一个浴室. 每个浴室的人都在喷头前边排队,而且每个浴室内保证大家都尽可能均匀得在喷头后边排队. 求所有浴室中最长队 ...
- HDU 4405 Aeroplane chess (概率DP)
题意:你从0开始,要跳到 n 这个位置,如果当前位置是一个飞行点,那么可以跳过去,要不然就只能掷骰子,问你要掷的次数数学期望,到达或者超过n. 析:概率DP,dp[i] 表示从 i 这个位置到达 n ...
- POJ 2096 Collecting Bugs (概率DP)
题意:给定 n 类bug,和 s 个子系统,每天可以找出一个bug,求找出 n 类型的bug,并且 s 个都至少有一个的期望是多少. 析:应该是一个很简单的概率DP,dp[i][j] 表示已经从 j ...
- POJ 2151 Check the difficulty of problems (概率DP)
题意:ACM比赛中,共M道题,T个队,pij表示第i队解出第j题的概率 ,求每队至少解出一题且冠军队至少解出N道题的概率. 析:概率DP,dp[i][j][k] 表示第 i 个队伍,前 j 个题,解出 ...
- 概率DP light oj 1030
t组数据 n块黄金 到这里就捡起来 出发点1 到n结束 点+位置>n 重掷一次 dp[i] 代表到这里的概率 dp[i]=(dp[i-1]+dp[i-2]... )/6 如果满6个的话 否则 ...
随机推荐
- 【线段树区间最值单点更新模板】BNUOJ 52965 E Excellent Engineers
http://acm.bnu.edu.cn/v3/external/gym/101512.pdf #include<bits/stdc++.h> using namespace std; ...
- 货车运输(codevs 3287)
题目描述 Description A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物,司机们想知道每辆车在不超过 ...
- 洛谷 [P3205] 合唱队
区间DP 手动模拟一下,我们发现本题就是一个左右加数的区间DP #include <iostream> #include <cstdio> #include <cstri ...
- 【ZOJ4053】Couleur(主席树,set,启发式)
题意: 有n个位置,每个位置上的数字是a[i],现在有强制在线的若干个单点删除操作,每次删除的位置都不同,要求每次删除之后求出最大的连续区间逆序对个数 n<=1e5,1<=a[i]< ...
- Java面试题集(四)
二. Java Web基础部分 在js中如何创建一个对象? var p1={name:”tom”,”age”:12}; function Person(name,age){ this.name=nam ...
- Redis对象的设计与实现
一.Redis对象结构Redis中的每个对象都由一个redisObject结构表示: typedef struct redisObject { unsigned type;//类型 unsigned ...
- raspberrypi树莓派liunx下安装golang1.9环境(debian)
直接安装go1.6以上版本会出现提示 Set $GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4. Go1.9的构建过程需要用于引导的 Go1.4二 ...
- 利用背景流量数据(contexual flow data) 识别TLS加密恶意流量
识别出加密流量中潜藏的安全威胁具有很大挑战,现已存在一些检测方法利用数据流的元数据来进行检测,包括包长度和到达间隔时间等.来自思科的研究人员扩展现有的检测方法提出一种新的思路(称之为“dataomni ...
- 带您了解Oracle层次查询
http://database.51cto.com/art/201010/231539.htm Oracle层次查询(connect by )是结构化查询中用到的,下面就为您介绍Oracle层次查询的 ...
- paddle中新增layer
Implement C++ Class The C++ class of the layer implements the initialization, forward, and backward ...