[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 JudgeProblem DescriptionIn
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.InputThe
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.
OutputOutput 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 Input1
0.1
2
0.1 0.4Sample Output10.000
10.500SourceRecommendzhoujiaqi2010 | We have carefully selected several similar problems for you: 4337 4331 4332 4333 4334
首先状压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)$。
#include<cstdio>
#include<algorithm>
#define rep(i,l,r) for (int i=l; i<=r; i++)
typedef double db;
using namespace std; const int N=;
const db eps=1e-;
db a[N],ans;
int n; void dfs(int x,db sum,int k){
if (x>n) { if (sum>=eps) ans+=(db)k/sum; return; }
dfs(x+,sum,k); dfs(x+,sum+a[x],-k);
} int main(){
while (~scanf("%d",&n)){
ans=; rep(i,,n) scanf("%lf",&a[i]);
dfs(,,-); printf("%.10lf\n",ans);
}
return ;
}
[HDU4336]Card Collector(min-max容斥,最值反演)的更多相关文章
- 【HDU4336】Card Collector(Min-Max容斥)
[HDU4336]Card Collector(Min-Max容斥) 题面 Vjudge 题解 原来似乎写过一种状压的做法,然后空间复杂度很不优秀. 今天来补一种神奇的方法. 给定集合\(S\),设\ ...
- Card Collector(期望+min-max容斥)
Card Collector(期望+min-max容斥) Card Collector woc居然在毫不知情的情况下写出一个min-max容斥 题意 买一包方便面有几率附赠一张卡,有\(n\)种卡,每 ...
- HDU - 4336:Card Collector(min-max容斥求期望)
In your childhood, do you crazy for collecting the beautiful cards in the snacks? They said that, fo ...
- min-max容斥/最值反演及其推广
设\(S\)是一个集合,\(\max(S)\)和\(\min(S)\)分别表示集合中的最大值与最小值. 那么有如下式子成立: \[\max(S)=\sum_{T \subseteq S}(-1)^{| ...
- HDU4336 Card Collector(期望 状压 MinMax容斥)
题意 题目链接 \(N\)个物品,每次得到第\(i\)个物品的概率为\(p_i\),而且有可能什么也得不到,问期望多少次能收集到全部\(N\)个物品 Sol 最直观的做法是直接状压,设\(f[sta] ...
- hdu4336 Card Collector MinMax 容斥
题目传送门 https://vjudge.net/problem/HDU-4336 http://acm.hdu.edu.cn/showproblem.php?pid=4336 题解 minmax 容 ...
- hdu4336 Card Collector 【最值反演】
题目链接 hdu4336 题解 最值反演 也叫做\(min-max\)容斥,在计算期望时有奇效 \[max\{S\} = \sum\limits_{T \in S} (-1)^{|T| + 1}min ...
- 【题解】HDU4336 Card Collector
显然,这题有一种很简单的做法即直接状压卡牌的状态并转移期望的次数.但我们现在有一个更加强大的工具——min-max容斥. min-max 容斥(对期望也成立):\(E[max(S)] = \sum_{ ...
- hdu4336 Card Collector
Problem Description In your childhood, do you crazy for collecting the beautiful cards in the snacks ...
随机推荐
- Python学习4,字符串
字符串这个东西主要靠记,多打打就好了. _author_ = "Happyboy" name = "my \tname is happyboy and i am 66 y ...
- rownum浅谈(二)
上篇说到rownum和order by及索引列的关系,明白了通过构建一个子查询把查询结果固定住再取数就可以了 .还是取最近10条创建的用户: select * from (select u.* fro ...
- Android动态添加和移除布局
package com.hyang.administrator.studentproject; import android.os.Bundle; import android.support.v7. ...
- 重复造轮子系列--字符串处理(C语言)
这些字符代码是以前写的,源于很久很久以前的一个VC++项目,在当时的部门编程比赛里因为用了项目代码的xsplit函数,万万没想到,那个做了几年的项目里面居然有坑..xsplit函数居然不能split连 ...
- ORACLE 向BLOB字段中出入图片等二进制文件,使用Oracle SQl Developer工具
使用PL/SQL也可以 create directory "image" as 'e:\'; --"image" 要带双引号,网上很多不带的,我测试时出错,并且 ...
- 2017 多校2 hdu 6053 TrickGCD
2017 多校2 hdu 6053 TrickGCD 题目: You are given an array \(A\) , and Zhu wants to know there are how ma ...
- BZOJ3594 [Scoi2014]方伯伯的玉米田 【树状数组优化dp】
题目链接 BZOJ3594 题解 dp难题总是想不出来,, 首先要观察到一个很重要的性质,就是每次拔高一定是拔一段后缀 因为如果单独只拔前段的话,后面与前面的高度差距大了,不优反劣 然后很显然可以设出 ...
- 【POJ 2406 Power Strings】
Time Limit: 3000MSMemory Limit: 65536K Description Given two strings a and b we define a*b to be the ...
- 在HTML网页上打印需要的内容
首先在head里面加入下面一段js代码: function preview(oper) { if (oper < 10) { bdhtml = window.document.body.inne ...
- 《c程序设计语言》-2.9
#include <stdio.h> /*int bitcount(unsigned x) { int b; for(b = 0;x != 0;x >>= 1) { if(x ...