题目链接
https://www.luogu.com.cn/problem/P2473
题意
n轮,m种球,每轮随即一个球,可以接可以不接,接的话需要满足这个球的前置条件(比如3号球接球必须已经接过1,4号这种),然后获得一定分数(可负)。问最佳策略下期望得分
思路
练了这点DP总算有点眉目了。
求期望,并且发现我们前面的操作是有后效性的(我们需要作出吃负分,更新状态的不贪心操作),我们考虑倒推
显然我们需要记录当前状态以判断是否能接某个球,考虑状态压缩,某位置为1代表接到过这种球,对于只需要考虑是否接过球的本题足够了。
定义DP[i][j]为i轮开始时,j状态下还可以获得的分值期望,那么初始状态就是DP[n+1][]为0,第n轮结束后不可以再获得了。答案就是DP[1][0],初始轮次没接过球还能获得的分数期望就是总的分数期望
倒退考虑转移,DP[i][j]显然都是从DP[i+1]推来的,我们枚举m种抛球情况,每种等概率
- 对于无法接到的情况,显然期望不变,状态也不变。直接加上DP[i+1][j]/m
- 对于能接到的情况,我们对DP[i+1][j]和DP[i+1][w]+value(w为接球后状态)取一个max再除m加上即可
至于判断能否接到,我们对于每种球处理一个接到的状态(3号球接1,4,那就是1001),如果可接状态和当前状态求按位与结果和可接一致,那么就是可以接到。
至于上文说的转移的新状态w,就在当前状态对应接球位按位或一个1即可。
代码
#include<cstdio>
#include<iostream>
#include<iomanip>
#include<map>
#include<unordered_map>
#include<string>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<chrono>
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl "\n"
//#define int long long
//#define double long double
using namespace std;
typedef long long ll;
const int maxn=40005;
const int maxm=105;
const int inf=0x3f3f3f3f;
int n,m,k;
double dp[maxm][maxn];
int wuhu[maxm];
int v[maxm];
bool check(int a,int b){
if((a&wuhu[b])==wuhu[b]) return 1;
return 0;
}
signed main(){
IOS
#ifndef ONLINE_JUDGE
freopen("IO\\in.txt","r",stdin);
freopen("IO\\out.txt","w",stdout);
#endif
int tn=1;
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>v[i];
int t;
while(cin>>t&&t){
wuhu[i]=(wuhu[i]|(1<<(t-1)));
}
}
for(int i=n;i;i--){
for(int j=0;j<(1<<m);j++){
for(int w=1;w<=m;w++){
//可以转移,期望+v
//不能转移,直接期望
if(check(j,w))
dp[i][j]+=max(dp[i+1][j],dp[i+1][j|(1<<(w-1))]+v[w]);
else
dp[i][j]+=dp[i+1][j];
}
dp[i][j]/=m;
}
}
cout<<fixed<<setprecision(6)<<dp[1][0]<<endl;
}