http://acm.hdu.edu.cn/showproblem.php?pid=5013

m个游客,n座城市(m, n <= 16), 每个人从1走到n, 每次有一定概率停在原地,然后以后就不前进了。一个人到过一个城会得到一定的愉悦度,对于相邻的两座城,会额外产生Cj / Cj - 1 * Hj的愉悦度,Cj是到过j城的人数,Hj是到过j城的人在这里获得的愉悦度之和。求期望的总愉悦度。

根据题解给出的解法

http://blog.csdn.net/oilover/article/details/39526899

需要跑3s左右

优化成纯dp能变成78ms,但是看不懂别人的代码...

根据期望的线性性,分别求每个人每天的值的期望



设f[i][j][x][y]表示第i天,前j个人,前一天x,当前天y的概率



设g[i][j][x][y]表示第i天,前j个人,前一天x,当前天y的期望



那么

f[i][j][x][y]=f[i][j-1][x][y]*(1-p[j]^(i-2))+f[i][j-1][x-1][y]*p[j]^(i-2)*(1-p[j])+f[i][j-1][x-1][y-1]*p[j]^(i-1)

g[i][j][x][y]=g[i][j-1][x][y]*(1-p[j]^(i-2))+g[i][j-1][x-1][y]*p[j]^(i-2)*(1-p[j])+(g[i][j-1][x-1][y-1]+f[i][j-1][x-1][y-1]*h[j][i])*p[j]^(i-1)

状态方程不懂,求各位大神解释..

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <iostream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define clr0(x) memset(x,0,sizeof(x))
typedef long long LL;
const int maxn = 20;
int n, m;
double p[maxn][maxn],h[maxn][maxn],f[maxn][maxn][maxn],g[maxn][maxn][maxn]; int main() {
while (~RD2(m,n)) {
for (int i = 1; i <= m; i++) {
scanf("%lf", &p[i][1]);
p[i][0] = 1.0;
for (int j = 2; j <= n; j++) {
p[i][j] = p[i][j - 1] * p[i][1];
}
}
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
scanf("%lf", &h[i][j]);
}
}
clr0(f),clr0(g); double ans = 0;
for (int i = 2; i <= n; i++) {
f[i][0][0] = 1;
for (int j = 1; j <= m; j++) {
for (int x = m; x >= 0; x--) {
for (int y = x; y >= 0; y--) {
f[i][x][y] *= (1.0 - p[j][i - 2]);
if (x > 0) {
f[i][x][y] += f[i][x - 1][y] * p[j][i - 2] * (1.0 - p[j][1]);
if (y > 0) {
f[i][x][y] += f[i][x - 1][y - 1] * p[j][i - 1];
}
}
g[i][x][y] *= (1.0 - p[j][i - 2]);
if (x > 0) {
g[i][x][y] += g[i][x - 1][y] * p[j][i - 2] * (1.0 - p[j][1]);
if (y > 0) {
g[i][x][y] += (g[i][x - 1][y - 1] + f[i][x - 1][y - 1] * h[j][i]) * p[j][i - 1];
}
}
}
}
}
for (int x = 1; x <= m; x++) {
for (int y = 1; y <= x; y++) {
ans += g[i][x][y] * ((double)y / x + 1.0);
}
}
}
for (int i = 1; i <= m; i++) {
ans += h[i][1];
}
printf("%.10f\n", ans);
} return 0;
}

hdu 5013 优化疑问+dp的更多相关文章

  1. fwt优化+树形DP HDU 5909

    //fwt优化+树形DP HDU 5909 //见官方题解 // BestCoder Round #88 http://bestcoder.hdu.edu.cn/ #include <bits/ ...

  2. 2017多校第4场 HDU 6078 Wavel Sequence DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6078 题意:求两个序列的公共波形子序列的个数. 解法: 类似于最长公共上升子序列,对于每个i,只考虑存 ...

  3. HDU 1011 树形背包(DP) Starship Troopers

    题目链接:  HDU 1011 树形背包(DP) Starship Troopers 题意:  地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...

  4. 队列优化和斜率优化的dp

    可以用队列优化或斜率优化的dp这一类的问题为 1D/1D一类问题 即状态数是O(n),决策数也是O(n) 单调队列优化 我们来看这样一个问题:一个含有n项的数列(n<=2000000),求出每一 ...

  5. hdu 2296 aC自动机+dp(得到价值最大的字符串)

    Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  6. HDU 4778 状压DP

    一看就是状压,由于是类似博弈的游戏.游戏里的两人都是绝对聪明,那么先手的选择是能够确定最终局面的. 实际上是枚举最终局面情况,0代表是被Bob拿走的,1为Alice拿走的,当时Alice拿走且满足变换 ...

  7. HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)

    HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...

  8. HDOJ(HDU).2546 饭卡(DP 01背包)

    HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...

  9. HDOJ(HDU).2602 Bone Collector (DP 01背包)

    HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...

随机推荐

  1. 5E - A + B Again

    There must be many A + B problems in our HDOJ , now a new one is coming. Give you two hexadecimal in ...

  2. mysql学习笔记-1.下载与安装

    1.到mysql官网下载操作系统对应的数据库,下载地址https://dev.mysql.com/downloads/mysql/ 2.有msi安装版本和zip压缩版本,2种安装方式不同, 3.安装后 ...

  3. win10上VMare安装Centos7并使用Xshell连接Centos

      一.CentOS 使用VMware虚拟机如何上网 1.宿主机的虚拟网关VMnet8的IP设置为自动获取. (1)打开控制面板:“控制面板” ---> “网络和 Internet” ---&g ...

  4. Cannot switch on a value of type String for source level below 1.7. Only convertible int values or enum variables are permitted

    在java中写switch代码时,参数用的是string,jdk用的是1.8,但是还是报错,说不支持1.7版本以下的,然后查找了项目中的一些文件,打开一个文件如下,发现是1.6的版本,好奇怪啊,按照e ...

  5. HISAT2+StringTie+Ballgown安装及使用流程

    HISAT2+StringTie+Ballgown安装及使用流程 2015年Nature Methods上面发表了一款快速比对工具hisat,作为接替tophat和bowtie的比对工具,它具有更快的 ...

  6. ZBlog你选择PHP还是ASP?

    最近趁着空闲玩了一下zblog,对于很多第一次接触zblog的博主大多都会问zblog是PHP好还是ASP好?我们应该如何选择?其实,对于这个问题我也不是很懂,我个人比较倾向于PHP.今天我就整理一下 ...

  7. 20172325『Java程序设计』课程 结对编程练习_四则运算第三周阶段总结

    20172325『Java程序设计』课程 结对编程练习_四则运算第三周阶段总结 结对伙伴 学号:20172306 姓名:刘辰 在这次项目的完成过程中刘辰同学付出了很多,在代码的实践上完成的很出色,在技 ...

  8. Codeforces 798C. Mike and gcd problem 模拟构造 数组gcd大于1

    C. Mike and gcd problem time limit per test: 2 seconds memory limit per test: 256 megabytes input: s ...

  9. Writing modular applications with laravel-modules

    01-07-2016 Let me start by saying Laravel is an amazing framework. However when it comes to writing ...

  10. GOIP connects with Elastix through “config by line”

    GOIP connects with Elastix through “config by line” By grace Liu on May 17, 2013 in Elastix, Gateway ...