【概率dp】【滚动数组】CDOJ1652 都市大飙车

转移方程很显然。

因为是多段图模型,所以可以滚动数组优化一维空间。

#include<cstdio>
#include<cstring>
using namespace std;
int m,K,n,p;
bool zaw[1010][30010];
double f[2][30010];
int main(){
	int x,y;
	bool flag=1;
	scanf("%d%d%d%d",&m,&K,&n,&p);
	for(int i=1;i<=K;++i){
		scanf("%d%d",&x,&y);
		if(y<=n){
			zaw[y][x]=1;
			flag=0;
		}
	}
	if(m==1){
		if(!flag){
			puts("0.000000");
		}
		else{
			puts("1.000000");
		}
		return 0;
	}
	bool cur=0;
	f[cur][p]=1;
	for(int i=1;i<=n;++i){
		cur^=1;
		memset(f[cur],0,sizeof(f[cur]));
		for(int k=1;k<=2;++k){
			if(!zaw[i-1][1]){
				f[cur][k]+=f[cur^1][1]/2.0;
			}
		}
		for(int k=m-1;k<=m;++k){
			if(!zaw[i-1][m]){
				f[cur][k]+=f[cur^1][m]/2.0;
			}
		}
		for(int j=2;j<m;++j){
			for(int k=j-1;k<=j+1;++k){
				if(!zaw[i-1][j]){
					f[cur][k]+=f[cur^1][j]/3.0;
				}
			}
		}
	}
	double ans=0;
	for(int i=1;i<=m;++i){
		if(!zaw[n][i]){
			ans+=f[cur][i];
		}
	}
	printf("%.6lf\n",ans);
	return 0;
}
时间: 2024-12-23 14:08:51

【概率dp】【滚动数组】CDOJ1652 都市大飙车的相关文章

[ACM] HDU 4576 Robot (概率DP,滚动数组)

Robot Problem Description Michael has a telecontrol robot. One day he put the robot on a loop with n cells. The cells are numbered from 1 to n clockwise. At first the robot is in cell 1. Then Michael uses a remote control to send m commands to the ro

POJ3071-Football(概率DP+滚动数组)

Football Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2769   Accepted: 1413 Description Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, -, 2n. In each round of the tournament, all teams still in the

HDU - 4576 Robot(概率dp+滚动数组)

题意:所有的格子围成一个圈,标号为1~n,若从格子1出发,每次指令告知行走的步数,但可能逆时针也可能顺时针走,概率都是1/2,那么问走了m次指令后位于格子l~r(1≤l≤r≤n)的概率. 分析: 1.因为m次指令后不知道会走到哪,会有很多种可能,但是知道从哪里出发,所以起始状态是已知的,在最初的状态,位于格子1是必然的,概率为1. 2.本题应用滚动数组,因为每次指令后都会延伸出无数种可能,这些可能是在前一种状态的基础上延伸的,而且延伸过后前一种状态的值不再有意义,完全可以被当前状态所覆盖. 3.

Hello 2019 D 素因子贡献法计算期望 + 概率dp + 滚动数组

https://codeforces.com/contest/1097/problem/D 题意 给你一个n和k,问n经过k次操作之后留下的n的期望,每次操作n随机变成一个n的因数 题解 概率dp计算出每个素因子留下的概率,乘以这个素因子的值就是这个素因子的贡献期望 定义\(dp[i][j]\)为第i次操作后剩下j个素因子的概率,概率dp顺着推 \(dp[i][j]->dp[i+1][k](k<=j)\) \(dp[i+1][k]+=dp[i][j]\frac{1}{j+1}(k<=j)

HDU - 2294 Pendant (DP滚动数组降维+矩阵快速幂)

Description On Saint Valentine's Day, Alex imagined to present a special pendant to his girl friend made by K kind of pearls. The pendant is actually a string of pearls, and its length is defined as the number of pearls in it. As is known to all, Ale

HDU 1024 Max Sum Plus Plus --- dp+滚动数组

HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值,其中第i个子序列包括a[j], 则max(dp[m][k]),m<=k<=n 即为所求的结果 <2>初始状态: dp[i][0] = 0, dp[0][j] = 0; <3>状态转移: 决策:a[j]自己成为一个子段,还是接在前面一个子段的后面 方程: a[j]直接接在前面

poj3624 01背包入门 dp+滚动数组

poj3624 01背包 dp+滚动数组 Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25458   Accepted: 11455 Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the bes

HDU 5617 Jam&#39;s maze dp+滚动数组

题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5617 bc(中文):http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=666&pid=1003 题解: 设dp[x1][x2][i]表示第i步时,从(1,1)点走到了(x1,y1),(n,n)点走到了(x2,y2)点的合法的总数. 1 #include<iostream> 2 #include

HDU 5119 Happy Matt Friends (背包DP + 滚动数组)

题目链接:HDU 5119 Problem Description Matt has N friends. They are playing a game together. Each of Matt's friends has a magic number. In the game, Matt selects some (could be zero) of his friends. If the xor (exclusive-or) sum of the selected friends'ma