[HackerRank]Choosing White Balls
[HackerRank]Choosing White Balls
题目大意:
有\(n(n\le30)\)个球排成一行,每个球的颜色为黑或白。
执行\(k\)次操作,第\(i\)次操作形式如下:
- 从\([1,n−i+1]\)中,等概率随机选择一个整数\(x\)。
- 移除从左往右数的第\(x\)个球,或从右往左数的第\(x\)个球。之后,所有右侧的球的编号减\(1\)。
给定每个球的颜色信息,求在最优策略下,期望的移除白球数量最大值。
思路:
状压DP+哈希表。
源代码:
#include<cstdio>
#include<cctype>
#include<ext/pb_ds/assoc_container.hpp>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
inline int getval() {
register char ch;
while(!isalpha(ch=getchar()));
return ch=='W';
}
typedef long long int64;
int n,m;
__gnu_pbds::cc_hash_table<int64,double> map;
inline int del(const int &s,const int &i) {
return (s>>(i+1)<<i)+(s&((1<<i)-1));
}
inline int bit(const int &s,const int &i) {
return (s>>i)&1;
}
double dfs(const int &s,const int &cnt) {
if(cnt<=n-m) return 0;
const int64 p=(1ll<<cnt)|s;
if(map.find(p)!=map.end()) return map[p];
double ret=0;
for(register int i=0;i<cnt;i++) {
double tmp=0;
const int j=cnt-i-1;
tmp=std::max(tmp,dfs(del(s,i),cnt-1)+bit(s,i));
tmp=std::max(tmp,dfs(del(s,j),cnt-1)+bit(s,j));
ret+=tmp;
}
ret/=cnt;
return map[p]=ret;
}
int main() {
n=getint(),m=getint();
int all=0;
for(register int i=0;i<n;i++) {
all=all<<1|getval();
}
printf("%.8f\n",dfs(all,n));
return 0;
}
[HackerRank]Choosing White Balls的更多相关文章
- ural 2063. Black and White
2063. Black and White Time limit: 1.0 secondMemory limit: 64 MB Let’s play a game. You are given a r ...
- SGU 183. Painting the balls( dp )
dp..dp(i, j)表示画两个点为i-j, i的最优答案. dp(i, j) = min{ dp(i-j, k) } + cost[i] (1≤k≤M-j) 令f(i, j) = min{dp(i ...
- HDU 5194 DZY Loves Balls
DZY Loves Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- sgu 183. Painting the balls 动态规划 难度:3
183. Painting the balls time limit per test: 0.25 sec.memory limit per test: 4096 KB input: standard ...
- Codeforces Round #245 (Div. 2) B. Balls Game 并查集
B. Balls Game Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem ...
- SCU3037 Painting the Balls
Description Petya puts the \(N\) white balls in a line and now he wants to paint some of them in bla ...
- 基本概率分布Basic Concept of Probability Distributions 5: Hypergemometric Distribution
PDF version PMF Suppose that a sample of size $n$ is to be chosen randomly (without replacement) fro ...
- 加州大学伯克利分校Stat2.2x Probability 概率初步学习笔记: Section 1 The Two Fundamental Rules (1.5-1.6)
Stat2.2x Probability(概率)课程由加州大学伯克利分校(University of California, Berkeley)于2014年在edX平台讲授. PDF笔记下载(Acad ...
- 【Gym 100015B】Ball Painting
题 There are 2N white balls on a table in two rows, making a nice 2-by-N rectangle. Jon has a big pai ...
随机推荐
- SharePoint 2010 使用Install-SPSolution部署wsp包状态一直是”正在部署”
1.服务器场信息如下: 2.使用下面命令部署,状态一直是"正在部署" Install-SPSolution –Identity xxxx.wsp –WebApplication h ...
- cacti系列(二)之cacti添加对tomcat服务器的监控
cacti添加对tomcat的监控 1.首先下载监控tomcat的模板 TomcatStats-0.1.zip 2.导入模板 (cacti_host_template_tomcat_server ...
- CentOS 6.5自动化运维之基于DHCP和TFTP服务的PXE自动化安装centos操作系统详解
前言 如果要给很多台客户端主机安装操作系统,要是每一台都拿张安装光盘一台一台主机的去装系统那就太浪费时间和精力了.在生产环境中也不实际,要实现为多台主机自动安装操作系统,那我们怎么实现自动化安装 ...
- Java上传文件FTP服务器代码
1. 在实际的应用重,通常是通过程序来进行文件的上传. 2. 实现java上传文件到ftp服务器中 新建maven项目 添加依赖 <dependency> <groupId>c ...
- 洛谷 P4427 求和
传送门啦 思路: 开始不肿么容易想到用倍增,但是想到需要求 $ Lca $ ,倍增这种常数小而且快的方法就很方便了.求 $ Lca $ 就是一个最普通的板子.那现在考虑怎么求题目中的结果. 树上差分可 ...
- C++ code:位操作实例(bit operation example)
某任务需要在A.B.C.D.E这五个人中物色人员去完成,但派人受限于下列条件: (1)若A去,则B跟去 (2)D,E两人中必有人去 (3)B,C两人中必有人去,但只去一人 (4)C,D两人要么都去,要 ...
- web中切图、快速切图与web雪碧图制作的方法
声明: web小白的笔记,欢迎大神指点,联系QQ:1522025433. 工具:Photoshop 1.复制文字:点击文章工具后选择文字. 2.矩形选框工具 看信息 f8, 取消矩形选框 Ctrl+D ...
- python 全栈开发,Day44(IO模型介绍,阻塞IO,非阻塞IO,多路复用IO,异步IO,IO模型比较分析,selectors模块,垃圾回收机制)
昨日内容回顾 协程实际上是一个线程,执行了多个任务,遇到IO就切换 切换,可以使用yield,greenlet 遇到IO gevent: 检测到IO,能够使用greenlet实现自动切换,规避了IO阻 ...
- WinAFL
winafl 标签(空格分隔): fuzz 构成 afl-fuzz.c 主模块 读取文件 维护testcase queue 进行mutate fuzz_one 评估代码覆盖率 执行遗传算法 更新界面 ...
- HDU 1075 字符串映射(map)
Sample InputSTARTfrom fiwohello difhmars riwosfearth fnnvklike fiiwjENDSTARTdifh, i'm fiwo riwosf.i ...