题意:
有 n n n个球,有黑有白,我们每次可以移除第 k k k个,或者倒数第 k k k个球,问期望能移除多少白球
分析:
因为球的个数比较少,所以我们可以考虑状压表示球
然后用 d f s dfs dfs暴力删球,当然会 T T T烂,所以我们需要搞点记忆化,单纯用 m a p map map时间复杂度太高,而用数组则空间复杂度爆炸,所以我们可以选定一个分界点,比如如果球数 ⩽ 24 \leqslant 24 ⩽24就用数组,否则就用 m a p map map
代码:
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<cmath>
#include<vector>
#define LL long long
using namespace std;
inline LL read() {
LL d=0,f=1;char s=getchar();
while(s<'0'||s>'9'){
if(s=='-')f=-1;s=getchar();}
while(s>='0'&&s<='9'){
d=d*10+s-'0';s=getchar();}
return d*f;
}
string s;
int n=read(),k=read();
int del(int x,int t)
{
t--;
return ((x>>t+1)<<t)+x%(1<<t);
}
int judge(int x,int t) {
return (x>>(t-1))&1;}
map<int,double> ma[35];
double f[33554440];bool tf[33554440];
double dfs(int m,int x)
{
if(m==n-k) return 0;
if(m>24)
{
if(ma[m].find(x)!=ma[m].end()) return ma[m][x];
double &s=ma[m][x];
for(int i=1;i<=(m>>1);i++) s+=(double)2*max(dfs(m-1,del(x,i))+judge(x,i),dfs(m-1,del(x,m-i+1))+judge(x,m-i+1));
if(m&1) s+=(double)dfs(m-1,del(x,(m>>1)+1))+judge(x,(m>>1)+1);
return s/=m;
}
else
{
int w=(x|(1<<m));
if(tf[w]) return f[w];
tf[w]=1;
double &s=f[w];
for(int i=1;i<=(m>>1);i++) s+=(double)2*max(dfs(m-1,del(x,i))+judge(x,i),dfs(m-1,del(x,m-i+1))+judge(x,m-i+1));
if(m&1) s+=(double)dfs(m-1,del(x,(m>>1)+1))+judge(x,(m>>1)+1);
return s/=m;
}
}
int main()
{
if(!k) return !printf("0.000000");
getline(cin,s);
int num=0;
for(int i=1;i<=n;i++) {
num<<=1;num+=(s[i-1]=='W'?1:0);}
printf("%.6lf",dfs(n,num));
return 0;
}