【样例2】
Input
6 6
12 2 3 4 5 0
15 5 0
-2 2 4 5 0
-11 2 5 0
5 0
1 2 4 5 0
Output
10.023470
【数据规模】
1<=k<=100,1<=n<=15,分值为[-10^6,10^6]内的整数。
期望DP.
根据期望DP 这一步的期望=(上一步的期望+上一步de得分)/k (k为种类数)
从后往前算是规避不可能状态的常用手段
#include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<functional> #include<iostream> using namespace std; #define MAXN (100+10) #define MAXK (16) double f[MAXN][(1<<MAXK)-1]; int n,k,p[MAXK+1],d[MAXK+1]={0}; int main() { scanf("%d%d",&n,&k); int m=(1<<k)-1; for (int i=0;i<MAXN;i++) for (int j=0;j<=m;j++) f[i][j]=0.0; for (int i=1;i<=k;i++) { scanf("%d",&p[i]); int t; while (scanf("%d",&t)!=EOF&&t) { d[i]|=(1<<(t-1)); } } for (int i=n;i;i--) for (int j=0;j<=m;j++) { for (int l=1;l<=k;l++) if ((d[l]&j)==d[l]) f[i][j]+=max(f[i+1][j],f[i+1][j|(1<<(l-1))]+p[l]);//eat or not else f[i][j]+=f[i+1][j]; //can't eat f[i][j]/=(double)k; } printf("%.6lf\n",f[1][0]); return 0; }