[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 ...
随机推荐
- (整)Unreal渲染模块 框总览
@author: 黑袍小道 随缘查看 说明 由于搬山的渲染这部分担心自己理解错误,故而搬移官方下,后面整个完成再反过来更新 (这当且仅当做Unreal的帮助文档). 图形编程 模块 渲 ...
- leetcode_day02
任务二:删除排序数组中的重复项 原文链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/ 最开始的解决思路: ...
- java课后作业2017.10.20
动手动脑1: public class Test{ public static void main(String args[]) { Foo obj1=new Foo(); }}class Foo{ ...
- hdu 1969 Pie (二分法)
Pie Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- [洛谷P3805]【模板】manacher算法
题目大意:给你一个字符串,求出它的最长回文字段 题解:$manacher$算法 卡点:$p$数组未开两倍空间 C++ Code: #include <cstdio> #include &l ...
- [poj] 2749 building roads
原题 2-SAT+二分答案! 最小的最大值,这肯定是二分答案.而我们要2-SATcheck是否在该情况下有可行解. 对于目前的答案limit,首先把爱和恨连边,然后我们n^2枚举每两个点通过判断距离来 ...
- BZOJ5157 [Tjoi2014]上升子序列 【树状数组】
题目链接 BZOJ5157 题解 我们只需计算每个位置为开头产生的贡献大小,就相当于之后每个大于当前位置的位置产生的贡献 + 1之和 离散化后用树状数组维护即可 要注意去重,后面计算的包含之前的,记录 ...
- python下载链接内容
下面代码下载京东注册码,可接收参数num dir 可以将连接构造成其它网址,比如移动联通网上营业厅的验证码都是固定网址+13位时间戳的结构. #!/usr/bin/python #code utf-8 ...
- jira7.3/7.2安装、中文及破解
jira是Atlassian公司出品的项目与事务跟踪工具,被广泛应用于缺陷跟踪(bug管理).客户服务.需求收集.流程审批.任务跟踪.项目跟踪和敏捷管理等工作领域. 现在我们来介绍,有关jira安装. ...
- mac 安装 navicat premium11.2.1400 破解版
前言: 在 Mac 系统下 安装 navicat for mysql 时,遇到注册码的问题,适用了很多破解下载,都没有成功,现找到一篇特此记录下. Mac 下一款实用的数据库管理工具--Navicat ...