您的位置:首页 > 其它

礼物 期望dp

2017-10-19 08:22 155 查看



Description

夏川的生日就要到了。作为夏川形式上的男朋友,季堂打算给夏川买一些生日礼物。 

商店里一共有种礼物。夏川每得到一种礼物,就会获得相应喜悦值Wi(每种礼物的喜悦值不能重复获得)。 

每次,店员会按照一定的概率Pi(或者不拿出礼物),将第i种礼物拿出来。季堂每次都会将店员拿出来的礼物买下来。 

众所周知,白毛切开都是黑的。所以季堂希望最后夏川的喜悦值尽可能地高。 

求夏川最后最大的喜悦值是多少,并求出使夏川得到这个喜悦值,季堂的期望购买次数。


Input

第一行,一个整数N,表示有N种礼物。 

接下来N行,每行一个实数Pi和正整数Wi,表示第i种礼物被拿出来的概率和可以获得喜悦值。


Output

第一行,一个整数表示可以获得的最大喜悦值。 

第二行,一个实数表示获得这个喜悦值的期望购买次数,保留3位小数。


Sample Input



0.1 2 

0.2 5 

0.3 7


Sample Output

14 

12.167


Data Constraint

对于10%的数据,N = 1 

对于30%的数据,N ≤ 5 

对于100%的数据,N ≤ 20 ,0 < Wi ≤ 10^9 ,0 < Pi ≤ 1且∑Pi ≤ 1 

注意:本题不设spj


Solution

看N大小,显然状压DP 

转移方程 

Fs=∑F[s′]∗Pi+(1−∑Pi)+1(s=s′+2i)

当然,这样是比较奇怪的,所以移项 

∑Pi∗Fs=Fs′∗pi

接着移项 

Fs=Fs′∗pi∑Pi

上面的那个是正推,倒退是一样的,是指f数组的含义改变了,f【i】表示从i这个状态到目标状态的期望步数,显然f [ (1 < < n) - 1 ]=0,f [ 0 ]  就是所求答案。
吐槽:总感觉期望的题倒退比顺推简单
正推:
#include<cstdio>
#include<algorithm>
#define fo(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
int n;
double f[1048576],p[21];
int main()
{
scanf("%d",&n);long long an=0,jy;
fo(i,1,n) scanf("%lf %lld",&p[i],&jy),an+=jy;
printf("%lld\n",an);
fo(s,1,(1<<n)-1)
{
double sig=0;f[s]=0;
fo(i,1,n) if(s & (1<<i-1)) f[s]=f[s]+f[s-(1<<i-1)]*p[i],sig=sig+p[i];
f[s]=(f[s]+1)/sig;
}
printf("%.3lf",f[(1<<n)-1]);
}


逆推:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std ;

#define N 2000000 + 10
typedef long long ll ;

double f
, P
;
int n , m ;
ll ans ;

int main() {
//freopen( "gift.in" , "r" , stdin ) ;
//freopen( "gift.out" , "w" , stdout ) ;
scanf( "%d" , &n ) ;
for (int i = 1 ; i <= n ; i ++ ) {
int a ;
scanf( "%lf%d" , &P[i] , &a ) ;
if ( P[i] > 0 ) ans += a ;
}
m = (1 << n) - 1 ;
f[m] = 0 ;
for (int s = m - 1 ; s >= 0 ; s -- ) {
double sum = 0 ;
for (int j = 0 ; j < n ; j ++ ) {
if ( (s & (1 << j)) == 0 ) {
sum += P[j+1] ;
int _s = s | (1 << j) ;
f[s] += P[j+1] * f[_s] ;
}
}
f[s] ++ ;
f[s] = f[s] / sum ;
}
printf( "%lld\n%.3lf\n" , ans , f[0] ) ;
return 0 ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: