[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的更多相关文章

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

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

  3. HDU 5194 DZY Loves Balls

    DZY Loves Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

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

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

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

  7. 基本概率分布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 ...

  8. 加州大学伯克利分校Stat2.2x Probability 概率初步学习笔记: Section 1 The Two Fundamental Rules (1.5-1.6)

    Stat2.2x Probability(概率)课程由加州大学伯克利分校(University of California, Berkeley)于2014年在edX平台讲授. PDF笔记下载(Acad ...

  9. 【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 ...

随机推荐

  1. Python3学习笔记20-获取对象信息

    当我们拿到一个对象的引用时,如何知道这个对象是什么类型.有哪些方法呢? 基本类型都可以用type()判断: print(type(123)) print(type('str')) print(type ...

  2. dellR720服务器设置光盘引导流程安装cenos7

    1.开机,按F10,进入系统引导界面,选择加载系统选项,并选择redhat 7.1选项 系统提示不支持,选择仍然继续,根据提示设置BIOS设置启动,重启 2.根据提示按F11进入BIOS启动设置,选择 ...

  3. 了解的CAP和BASE等理论

    CAP,BASE和最终一致性是NoSQL数据库存在的三大基石.而五分钟法则是内存数据存储的理论依据.这个是一切的源头. 几个名词解释: 网络分区:俗称“脑裂”.当网络发生异常情况,导致分布式系统中部分 ...

  4. 如何理解深度学习中的Transposed Convolution?

    知乎上的讨论:https://www.zhihu.com/question/43609045?sort=created 不过看的云里雾里,越看越糊涂. 直到看到了这个:http://deeplearn ...

  5. SQL开发测试使用基础

    目录 一.客户端配置与使用    1.oracle(PLSQL Developer)    2.hive(hive cli)及命令    3.postgre(pgAdmin)   二.注意事项及基础 ...

  6. 【ES】match_phrase与regexp

    刚开始接触es,由于弄不清楚match_phrase和regexp导致很多查询结果与预想的不同.在这整理一下. regexp:针对的是单个词项 match_phrase:针对的是多个词项的相对位置 它 ...

  7. docker安装sonarqube及实际应用

    由于平台的多样化,在不同环境的安装方式可能也不一样,为了避免环境不一致带来的差异,特记一笔容器安装: 一.Sonar可以从以下七个维度检测代码质量,而作为开发人员至少需要处理前5种代码质量问题. 1. ...

  8. The last packet sent successfully to the server was 0 milliseconds ago.[nutch---mysql ]

    今天在使用JDBC操作mysql时遇到下面的异常信息: 引用 The last packet sent successfully to the server was 0 milliseconds ag ...

  9. win10定时执行php脚本

    转自http://www.cnblogs.com/wenhainan/p/6962089.html 第一步:确认windows上是否配置好了php环境变量,我用xampp安装的lamp环境,默认已经配 ...

  10. java:从指定问价中读取80个字节写入指定文件中

    import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; public class F ...