[SCOI2008]奖励关

嘟嘟嘟


关于这题为什么逆推想了半天。


看数据范围就很容易想到状压,令\(dp[i][S]\)表示第\(i\)轮,选取宝物的状态为\(S\)时的期望得分。
但是这样用刷表法递推的时候,会产生一些凭空的状态。又考虑到终止状态是已知的,即每一个\(S_i\)都可能是终止状态,所以我们逆推。这样答案就是\(dp[1][0]\),而不是\(dp[k][S]\)(其实第一维只是记录循环次数,跟正逆推无关,关键是第二维)。
转移就很简单,如果当前枚举到的物品\(j\)的前置物品都选了,那么\(dp[i][S] += max(dp[i + 1][S], dp[i + 1][S | (i << (j - 1))] + val[j])\),否则\(dp[i][S] += dp[i + 1][S]\)。最后因为是期望,\(dp[i][S] *= \frac{1}{n}\)

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<assert.h>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxk = 105;
const int maxn = 15;
In ll read()
{
  ll ans = 0;
  char ch = getchar(), last = ' ';
  while(!isdigit(ch)) last = ch, ch = getchar();
  while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
  if(last == '-') ans = -ans;
  return ans;
}
In void write(ll x)
{
  if(x < 0) x = -x, putchar('-');
  if(x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}
In void MYFILE()
{
#ifndef mrclr
  freopen(".in", "r", stdin);
  freopen(".out", "w", stdout);
#endif
}

int K, n, val[maxn + 5], s[maxn + 5];
db dp[maxk][(1 << maxn) + 5];

int main()
{
  //MYFILE();
  K = read(), n = read();
  for(int i = 1, x; i <= n; ++i)
    {
      val[i] = read(), x = read();
      while(x) s[i] |= (1 << (x - 1)), x = read();
    }
  for(int i = K; i; --i)
    for(int S = 0; S < (1 << n); ++S)
      {
	for(int j = 1; j <= n; ++j)
	  if((S | s[j]) == S)
	    dp[i][S] += max(dp[i + 1][S], dp[i + 1][S | (1 << (j - 1))] + val[j]);
	  else dp[i][S] += dp[i + 1][S];
	dp[i][S] /= n;
      }
  printf("%.6lf\n", dp[1][0]);
  return 0;
}
posted @ 2019-05-20 17:08  mrclr  阅读(129)  评论(0编辑  收藏  举报