hdu4336之状态压缩慨率DP

Card Collector

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2141    Accepted Submission(s): 1008

Special Judge

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.

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.4

Sample Output

10.000
10.500
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#define INF 99999999
typedef long long LL;
using namespace std;

const int MAX=(1<<20)+10;
const double eps=1e-4;
int n;
double dp[MAX],p[MAX];

int main(){
	while(~scanf("%d",&n)){
		double p2=1;
		for(int i=0;i<n;++i){scanf("%lf",&p[i]);p2-=p[i];}
		int bit=1<<n;
		dp[bit-1]=0;
		for(int i=bit-2;i>=0;--i){
			double p1=p2,ans=0;
			for(int j=0;j<n;++j){
				if(i&(1<<j)){//j这张卡片存在
					p1+=p[j];
				}else{
					ans+=p[j]*(dp[i+(1<<j)]+1);
				}
				dp[i]=(ans+p1)/(1-p1);
			}
		}
		printf("%.4f\n",dp[0]);//这里保留4位小数是因为题目最后一句话
	}
	return 0;
}

hdu4336之状态压缩慨率DP

时间: 2024-08-08 02:56:39

hdu4336之状态压缩慨率DP的相关文章

hdu4035之经典慨率DP

Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 1419    Accepted Submission(s): 511 Special Judge Problem Description When wake up, lxhgww find himself in a huge maze. The maze consisted b

ZOJ3640之简单慨率DP

Help Me Escape Time Limit: 2 Seconds      Memory Limit: 32768 KB Background     If thou doest well, shalt thou not be accepted? and if thou doest not well, sin lieth at the door. And unto thee shall be his desire, and thou shalt rule over him. And Ca

(hiho1048)POJ2411Mondriaan&#39;s Dream(DP+状态压缩 or 轮廓DP)

问题: Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series' (where he had to use his toilet paper to draw on, for all of his paper was filled with squares and rectangle

HDU4336——期望+状态压缩DP——Card Collector

http://acm.hdu.edu.cn/showproblem.php?pid=4336 转自http://www.cnblogs.com/zhj5chengfeng/archive/2013/03/02/2939601.html 做法分析 由于卡片最多只有 20 种,使用状态压缩,用 0 表示这种卡片没有收集到, 1 表示这种卡片收集到了 令:f[s] 表示已经集齐的卡片种类的状态的情况下,收集完所有卡片需要买东西次数的期望 买一次东西,包装袋中可能: 1. 没有卡片 2. 卡片是已经收集

POJ 2923 【01背包+状态压缩/状压DP】

题目链接 Emma and Eric are moving to their new house they bought after returning from their honeymoon. Fortunately, they have a few friends helping them relocate. To move the furniture, they only have two compact cars, which complicates everything a bit.

帮助BSNY(状态压缩+4维dp+滚动数组)

懵逼题,一度推出六维的DP,最后看了题解.. 恍然大悟...(需要运用好题目的限制(a[i]>=25 且a[i]<=32))并将相同的a[i]进行压缩,压缩成一个值 因为拿出一本书只有两种可能,(1)放到最前面,(2)放到与它相同编号的书的旁边,那么我们可以就此加上限制,就可以推出状态转移方程式了(PS:每次拿书必须是一团一团地取出来,否则并不改变原状态) #include<iostream> #include<cstdio> #include<cstring&g

HDU 5025 状态压缩蛇+bfs+dp

题目大意:孙悟空要找到一条花费时间最短的路径,路上为S的代表有蛇,经过需多花一分钟,其他情况下都是走过花费一分钟,但数字必须依次得到,最后到了唐僧处,可以经过也可以救出,救出前提是得到所有种类的钥匙 这道题,我们不断找到到达每一个点的不同状态下的最小花费时间,用dp[N][N][11][status]来存,status代表所有蛇的状态,因为蛇只有5条,所以status<32,不会爆掉. 类似spfa中不断对某个状态进行弛缓操作,如果成功就更新数据后入队,否则不再入队. 这题之所以不能dfs来做,

uva 1252(状态压缩dp)

题意:有n个二进制串,长度都是m且都不相同,问最少询问多少个问题可以把这n个串完全区分开. 题解:1<=m<=11,从这个范围就可以推测是状态压缩,那么dp肯定要有一维表示提问的问题,然后另一位就是根据提出的问题把串分类,一种是符合提出的问题的状态,另一种不符合.这样f[i][j]表示在问了问题i的状态下答案是状态j时还要提出多少个问题才能把所有串区分开. 如果找到在问题i下和答案j相同的串只有1串或没有,说明f[i][j]=0不需要再提问就已经区分开了,否则就要再提问问题,把之前问题i的位上

hdu 5067 Harry And Dig Machine (状态压缩dp)

题目链接 bc上的一道题,刚开始想用这个方法做的,因为刚刚做了一个类似的题,但是想到这只是bc的第二题, 以为用bfs水一下就过去了,结果MLE了,因为bfs的队列里的状态太多了,耗内存太厉害. 题意: 从某一点出发,遍历网格上的一些点,每个点至少访问一次需要的最小时间是多少. 官方题解: 由于Harry的dig machine是无限大的,而装载石头和卸载石头是不费时间的,所以问题可以转化成:从某一点出发,遍历网格上的一些点,每个点至少访问一次需要的最小时间是多少.这就是经典的旅行商问题,考虑到