[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 ...
随机推荐
- (笔记) RealTimeRender[实时渲染] C2
@author: 白袍小道 @来源:RealTime Render @建议书籍:龙书.RealTimeR第四版.GPUGem和PRO (来源:暗影不解释) 引点 这一章关注的管线中的管道功能,而非实现 ...
- easyUI layout
layout是一个容器,它有5个区域:north(北丐),south(南帝),east(东邪),west(西毒),center(中神通),像不像金庸的天龙八部,中间区域的panel是必须的, 周边区域 ...
- 第十五篇:jQuery
本篇内容 简介 使用 一. 简介 jQuery 是一个 JavaScript 库. jQuery 极大地简化了 JavaScript 编程. jQuery 很容易学习. jQuery 库可以通过一行简 ...
- java 图形化界面笔记(1)
目录 JFrame窗体......................................................................................... ...
- NOIP2017年11月9日赛前模拟
最后一次NOIP模拟了····· 题目1:回文数字 Tom 最近在研究回文数字. 假设 s[i] 是长度为 i 的回文数个数(不含前导0),则对于给定的正整数 n 有:
- 解决某些PC站在手机端宽度显示不正常的问题
可以打开控制台查看html标签的宽度,发现不是当前屏幕的宽度,更改下宽度即可:用js控制下,上代码 document.getElementsByTagName('html')[0].style.wid ...
- Python实现求矩阵路径最小和,使用动态规划
题目: 给定一些NxN的矩阵,对于任意的路线,定义其[和]为其线路上所有节点的数字的和,计算从左上角到右下角的路线和最小值.每条路线只能从某一点到其周围(上下左右)的点,不可斜行.例如: 4,6 2, ...
- Spring Cloud配置文件加载优先级简述
Spring Cloud中配置文件的加载机制与其它的Spring Boot应用存在不一样的地方:如它引入了bootstrap.properties的配置文件,同时也支持从配置中心中加载配置文件等:本文 ...
- 汕头市队赛 SRM14 T2 最长上升子序列
最长上升子序列 (tree.pas/c/cpp) 128MB 1s 有一个长度为n的序列a[i],其中1到n的整数各自在a[i]中出现恰好一次. 现在已知另一个等长的序列f[i],表示a[i]中以第i ...
- win8.1 安装sql2008 启动报错.net framework 应用程序的组件中发生了异常unable to read the previous list
解决方案:删除 RegSrvr.xml C:\Documents and Settings\Administrator\Application Data\Microsoft\Microsoft SQL ...