[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 ...
随机推荐
- loadrunner 欺骗ip设置
工具准备:loadrunner12,windows 10 ip欺骗=ip wizard 前提条件:本机IP地址为固定地址,不是自动获取的地址 方法: 1.管理员身份打开cmd 2.输入命令:confi ...
- 关于用python作为第三方程序,来调用shell命令的问题,以及返回值格式解析
1.用python语言作为第三方,调用shell 在python2.x中,可以通过包commands来进行调用shell命令.如下: cmd就是你要调用的shell命令,把环境配置好,输入正确的命令格 ...
- 聊聊、Mybatis XML
引入相应的依赖包 <dependency><groupId>org.mybatis</groupId><artifactId>mybatis-sprin ...
- NodeJs06 高并发
高并发架构 在业务的最初期,由于业务和用户的体量比较小,可能采用单机就足够了.随着业务的增长,用户量和并发请求量都会不断上升.当增长到一定的瓶颈的时候,系统能否抗住压力,就需要采取一些方案了.这就是著 ...
- crond守护进程
Linux系统任务计划/etc/crontab cron的主配置文件,可以定义PATHcron格式如下:# .----------------分钟 (0 - 59)# | .------------- ...
- 【bzoj4884】[Lydsy2017年5月月赛]太空猫 dp
原文地址:http://www.cnblogs.com/GXZlegend/p/6825431.html 题目描述 太空猫(SpaceCat)是一款画面精致.玩法有趣的休闲游戏,你需要控制一只坐在迷你 ...
- Codeforces755D PolandBall and Polygan
题目戳这里 我们只需要计算每增加一条线后穿过了几条已有的线即可.为了方便,我们令\(K \le N/2\),并且给每条线一个方向,即\(x\)到\((x+K) \; mod \; N\).然后我们假设 ...
- 【转发】Build Squid with SSL Bump and ICAP Client
原文文档:http://docs.diladele.com/administrator_guide_3_4/installation_and_removal/filtering_https.html ...
- poj 2367 拓扑排序入门
Description The system of Martians' blood relations is confusing enough. Actually, Martians bud when ...
- spring in action 学习笔记四:bean的生命周期
bean 的生命周期分为:一个是ApplicationContext的容器的bean的生命周期,另一个是BeanFactory容器的生命周期. 首先介绍一下:ApplicationContext的容器 ...