hdu4336 Card Collector 状态压缩dp
Card Collector
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1708 Accepted Submission(s): 780
Special Judge
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.
Note there is at most one card in a bag of snacks. And it is possible that there is nothing in the bag.
You will get accepted if the difference between your answer and the standard answer is no more that 10^-4.
0.1
2
0.1 0.4
10.500
#include <iostream>
#include <stdio.h>
#include <string.h> using namespace std;
double p[25];
int main()
{
int n,i,flag,j,k,all,cnt;
double sum,s,temp;
while(scanf("%d",&n)!=EOF)
{
flag=1;
for(i=0;i<n;i++)
{
scanf("%lf",&p[i]);
}
sum=0;
flag=1;
all=1<<n;
for(i=1;i<all;i++)
{
cnt=0;
temp=0;
for(j=0;j<n;j++)
{
if(i&(1<<j))//相交
{
temp+=p[j];cnt++;
}
}
if(cnt&1)//奇加偶减
sum+=1.0/temp;
else
sum-=1.0/temp;
}
printf("%.4f\n",sum);
}
return 0;
}
再来一个状态压缩dp,这里我们可以推出dp[n]=1+all dp[n](空的)+all dp[n](重复的)+all dp[k](没有但是加一个就有了);这样就好做了,好像这一题还非要写成输出4位,输出3位还是错的,样例应该是有问题的!很坑啊,有木有!
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
double p[25],dp[1<<20];
int main ()
{
int n,i,j,all;
double allp,tempallp,sum;
while(scanf("%d",&n)!=EOF)
{
allp=0;
for(i=0;i<n;i++)
{
scanf("%lf",&p[i]);
allp+=p[i];
}
all=1<<n;
dp[0]=0; for(i=1;i<all;i++)
{
tempallp=allp;
sum=1;//买了一袋
for(j=0;j<n;j++)
{
if(i&(1<<j))
{
sum+=p[j]*dp[i^(1<<j)]; }
else
{
tempallp-=p[j]; }
}
dp[i]=sum/tempallp; }
printf("%.4f\n",dp[all-1]);
} return 0;
}
hdu4336 Card Collector 状态压缩dp的更多相关文章
- [HDU 4336] Card Collector (状态压缩概率dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4336 题目大意:有n种卡片,需要吃零食收集,打开零食,出现第i种卡片的概率是p[i],也有可能不出现卡 ...
- [HDU4336]:Card Collector(概率DP)
题目传送门 题目描述 夏川的生日就要到了.作为夏川形式上的男朋友,季堂打算给夏川买一些生日礼物.商店里一共有种礼物.夏川每得到一种礼物,就会获得相应喜悦值$W_i$(每种礼物的喜悦值不能重复获得).每 ...
- hoj2662 状态压缩dp
Pieces Assignment My Tags (Edit) Source : zhouguyue Time limit : 1 sec Memory limit : 64 M S ...
- POJ 3254 Corn Fields(状态压缩DP)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4739 Accepted: 2506 Descr ...
- [知识点]状态压缩DP
// 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...
- HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP
题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...
- DP大作战—状态压缩dp
题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...
- 状态压缩dp问题
问题:Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Ev ...
- BZOJ-1226 学校食堂Dining 状态压缩DP
1226: [SDOI2009]学校食堂Dining Time Limit: 10 Sec Memory Limit: 259 MB Submit: 588 Solved: 360 [Submit][ ...
随机推荐
- 浅谈Mybatis(二)
一.resultMap 作用:发现数据库的查询结果与实体之间不匹配时,需要通过ResultMap来进行映射处理.常用于多表查询. 多表查询还是比较复杂的,因为可能的情况很多.这里只说两种情况: 1.1 ...
- Struts2拦截器总结
拦截器的本质: 拦截器就是一个类,一个实现了超级接口Interceptor的类.Interceptor接口里定义了三个方法 init(),destory(),intercept().其中inercep ...
- php phalcon
1. 准备所需工具 1) php环境 浏览 2) phalcon插件 浏览 2. 安装phalcon 将php_phalcon.dll拷贝到%PHP_HOME%\ext目录中 修改php.ini文件, ...
- RequireJS 和 SeaJS
RequireJS SeaJS CMD规范 CommonJS的规范: 根据CommonJS规范,一个单独的文件就是一个模块.加载模块使用require方法,该方法读取一个文件并执行,最后返回文件内部的 ...
- “-Xmx1024m -Xms1024m -Xmn512m -Xss256k”——Java运行参数(转)
JVM的堆的内存, 是通过下面面两个参数控制的 -Xms 最小堆的大小, 也就是当你的虚拟机启动后, 就会分配这么大的堆内存给你 -Xmx 是最大堆的大小 当最小堆占满后,会尝试进行GC,如果GC之后 ...
- android HTTP发送及MD5加密收集
发送部分: public void MyFunction{ HttpClient httpclient = new DefaultHttpClient(); //你的URL HttpPost http ...
- Android MediaPlayer Error -1004
之前用Android自带的VideoView播放在线视频一直没问题的.今天突然碰到无法播放. MediaPalyer返回的错误代码是-1004,API文档上写的是:Bitstream is not c ...
- android之PackageManager简单介绍
PackageManager相关 本类API是对全部基于载入信息的数据结构的封装,包含下面功能: 安装,卸载应用查询permission相关信息 查询Application相关信息(applicati ...
- 某IT校招笔试
前言 博主明天上午9点还有面试,今天突然看到某大牌IT公司笔试题目,必须做一下了 题目 1.假设把整数关键码K散列到N个槽列表,以下哪些散列函数是好的散列函数 A: h(K)=K/N; B: h(K) ...
- webform 不实用office控件导出excel StringBuilder 类型拼接字符串表格导出excel
StringBuilder sb = new StringBuilder(); sb.AppendLine("<meta http-equiv=\"Content-Type\ ...