[HDU4336]Card Collector(min-max容斥,最值反演)
阿新 • • 發佈:2018-05-01
12px each con have special ear clas ali multi
Card Collector
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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.
Total Submission(s): 5254 Accepted Submission(s): 2676
Special Judge
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.4Sample Output 10.000 10.500
Source 2012 Multi-University Training Contest 4
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 }
[HDU4336]Card Collector(min-max容斥,最值反演)