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][ ...
随机推荐
- STL模板_容器概念
一.STL(Standard Template Library,标准模板库)概述1.容器:基于泛型的数据结构.2.算法:基于泛型的常用算法.3.迭代器:以泛型的方式访问容器中的元素,是泛型的算法可以应 ...
- SGU题目总结
SGU还是个不错的题库...但是貌似水题也挺多的..有些题想出解法但是不想写代码, 就写在这里吧...不排除是我想简单想错了, 假如哪位神犇哪天发现请告诉我.. 101.Domino(2015.12. ...
- 搭建Hadoop集群 (二)
前面的步骤请看 搭建Hadoop集群 (一) 安装Hadoop 解压安装 登录master, 下载解压hadoop 2.6.2压缩包到/home/hm/文件夹. (也可以从主机拖拽或者psftp压缩 ...
- MarkDown使用 (二)矩阵
MarkDown的矩阵输入 MarkDown的矩阵输入 简单的Matrix 例如 $$ \begin{matrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ ...
- 公选网站作业4_2.php
序: 此实验的功能是只有已经登陆或者已经注册的用户才有发表留言的资格,否则只能够浏览留言. 1. 主要文件: 4_2login.html// 登陆的主页面 4_2login.php //处理登 ...
- mysql-connector-c 安装
cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/usr/local make make install
- curl: (6) Couldn’t resolve host ‘www.ttlsa.com’
上周, 部分站点出现Couldn't resolve host.....问题, 导致公司所有走api的程序都无法正常使用(系统redhat 6.3的都出现问题, redhat 5一切OK). 最 ...
- 窗口嵌入到另一个窗口(VC和QT都有)
1.用vc新建一个dialog1工程.属性默认. 2.insert一个dialog2,改为child. 3.在dialog1中包含dialog2头文件,在一个按钮事件中显示dialog2: Cdial ...
- 一步一步学习SignalR进行实时通信_2_Persistent Connections
原文:一步一步学习SignalR进行实时通信_2_Persistent Connections 一步一步学习SignalR进行实时通信\_2_Persistent Connections Signal ...
- git 导入代码到已有仓库
git remote add origin https://----------- git push -u origin master //这两行将该目录下的文件推送到远端(origin)上的 &qu ...