[HDU4336]Card Collector(min-max容斥,最值反演)

Card Collector

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5254    Accepted Submission(s): 2676
Special Judge


Problem Description
In your childhood, do you crazy for collecting the beautiful cards in the snacks? They said that, for example, if you collect all the 108 people in the famous novel Water Margin, you will win an amazing award.

As a smart boy, you notice that to win the award, you must buy much more snacks than it seems to be. To convince your friends not to waste money any more, you should find the expected number of snacks one should buy to collect a full suit of cards.
 
Input
The first line of each test case contains one integer N (1 <= N <= 20), indicating the number of different cards you need the collect. The second line contains N numbers p1, p2, ..., pN, (p1 + p2 + ... + pN <= 1), indicating the possibility of each card to appear in a bag of snacks.

Note there is at most one card in a bag of snacks. And it is possible that there is nothing in the bag.
 
Output
Output one number for each test case, indicating the expected number of bags to buy to collect all the N different cards.

You will get accepted if the difference between your answer and the standard answer is no more that 10^-4.
 
Sample Input
1 0.1 2 0.1 0.4
 
Sample Output
10.000 10.500
 
Source
 
Recommend
zhoujiaqi2010   |   We have carefully selected several similar problems for you:   4337  4331  4332  4333  4334 
 


Statistic | Submit | Discuss | Note

首先状压DP很显然,就是$f_S=\frac{1+\sum_{i\subseteq S}p_i f_{S\cup i}}{1-\sum_{i\subseteq S}p_i}$

时间$O(2^n*n)$,空间$O(2^n)$。

引入最值反演:$\max\{S\}=\sum_{T\subseteq S}(-1)^{|T|+1}min\{T\}$。

这个式子里的$\max\{S\}$可以表示集合中最大的数,也可以表示集合中最后一个出现的数(因为按照出现顺序标号就成了求最大数了)。

min-max容斥其实就是最值反演,考虑这样一类问题,每个元素有出现概率,求某个集合最后一个出现的元素所需次数的期望。

$E[\max\{S\}]=\sum_{T\subseteq S}(-1)^{|T|+1}E[min\{T\}]$

考虑$E[min\{T\}]$怎么求,实际上就是求集合中出现任意一个元素的概率的倒数,也就是$\frac{1}{\sum_{i\in T}p_i}$

这样只需枚举全集的子集即可。复杂度优化很多。

时间$O(2^n)$,空间$O(n)$。

 1 #include<cstdio>
 2 #include<algorithm>
 3 #define rep(i,l,r) for (int i=l; i<=r; i++)
 4 typedef double db;
 5 using namespace std;
 6 
 7 const int N=21;
 8 const db eps=1e-8;
 9 db a[N],ans;
10 int n;
11 
12 void dfs(int x,db sum,int k){
13     if (x>n) { if (sum>=eps) ans+=(db)k/sum; return; }
14     dfs(x+1,sum,k); dfs(x+1,sum+a[x],-k);
15 }
16 
17 int main(){
18     while (~scanf("%d",&n)){
19         ans=0; rep(i,1,n) scanf("%lf",&a[i]);
20         dfs(1,0,-1); printf("%.10lf\n",ans);
21     }
22     return 0;
23 }

猜你喜欢

转载自www.cnblogs.com/HocRiser/p/8975820.html