Acwing 232. 守卫者的挑战 【概率】
#include <stdio.h> #include <cstring> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <iostream> #include <map> #define go(i, l, r) for(int i = (l), i##end = (int)(r); i <= i##end; ++i) #define god(i, r, l) for(int i = (r), i##end = (int)(l); i >= i##end; --i) #define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0) #define debug_in freopen("in.txt","r",stdin) #define debug_out freopen("out.txt","w",stdout); #define pb push_back #define all(x) x.begin(),x.end() #define fs first #define sc second using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll,ll> pii; const ll maxn = 1e5+10; const ll maxM = 1e6+10; const ll inf_int = 1e8; const ll inf_ll = 1e17; template<class T>void read(T &x){ T s=0,w=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();} while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar(); x = s*w; } template<class H, class... T> void read(H& h, T&... t) { read(h); read(t...); } void pt(){ cout<<'\n';} template<class H, class ... T> void pt(H h,T... t){ cout<<" "<<h; pt(t...);} //-------------------------------------------- double eps = 1e-7; int N,L,K; double E[205][205][405]; double p[205]; int a[205]; double dfs(int i,int j,int ca){ //在进行第i轮之前,成功轮j次,背包容量与碎片数量之差为ca if(i>N){//比赛结束后,看是否可以成功离开 if(j>=L && ca>=0) return 1; else return 0; } if(E[i][j][ca + 200] >= 0) return E[i][j][ca + 200]; double ans = 0; if(a[i] == -1) ans = p[i] * dfs(i+1,j+1,ca-1) + (1-p[i]) * dfs(i+1,j,ca); //情况1 else ans = p[i] * dfs(i+1,j+1,ca+a[i]) + (1-p[i]) * dfs(i+1,j,ca); //情况2 . 其实可以合并一块写 return E[i][j][ca + 200] = ans; } int main() { // debug_in; // debug_out; read(N,L,K); K = min(200,K); go(i,1,N) scanf("%lf",&p[i]),p[i]/=100; go(i,1,N) read(a[i]); go(i,0,200) go(j,0,200) go(k,0,400) E[i][j][k] = -1; double ans = dfs(1,0,K); printf("%.6f\n",ans); return 0; }
Ryuichi的算法分享 文章被收录于专栏
分享我的一些算法题解,致力于把题解做好,部分题解可能会采用视频的形式来讲解。