[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. UML和模式应用5:细化阶段(8)---逻辑架构和UML包图

    1.前言 本章是从面向分析的工作过度到软件设计 典型的OO系统设计的基础是若干架构层,如UI层.应用逻辑(领域)层 本章简要考察逻辑分层架构和相关UML表示法 2.逻辑架构和层 逻辑架构 逻辑架构是软 ...

  2. ES系列十三、Elasticsearch Suggester API(自动补全)

    1.概念 1.补全api主要分为四类 Term Suggester(纠错补全,输入错误的情况下补全正确的单词) Phrase Suggester(自动补全短语,输入一个单词补全整个短语) Comple ...

  3. eclipse设置代码模板和格式

    该设置可以在保存文件时自动根据模板调整代码格式. 首先准备华为代码格式化文件: FEFO-Formatter.xml FEFO-codetemplates.xml 设置格式Preferences-Ja ...

  4. javaScript——原型继承四步曲

    <script> //js模拟类的创建以及继承 //第一步:创建父类 function Parent(name){ this.name = name; } //给父类添加属性方法 Pare ...

  5. Nodejs 实现ESL内联FreeSWITCH设定说明

    一.背景说明: SIP Server IP (Centos):192.168.11.61  ,服务器IP(Windows):192.168.11.19 二.目的: 能够从192.168.11.19上通 ...

  6. 转载:2.2 Nginx配置的通用语法《深入理解Nginx》(陶辉)

    原文:https://book.2cto.com/201304/19625.html Nginx的配置文件其实是一个普通的文本文件.下面来看一个简单的例子.user  nobody; worker_p ...

  7. Node.js Error: listen EADDRNOTAVAIL

    1 前言 nodejs部署在云服务器,外网用域名加端口访问不进来,但在服务器本地用127.0.0.1加端口可以访问,并且端口已经放开,然后只能排查配置.此文章仅作为记录使用. 如果端口和另一个的端口一 ...

  8. Mess it up!搞乱代码

    Mess it up! 作者: Laruence(   ) 本文地址: http://www.laruence.com/2009/01/07/656.html 转载请注明出处 恩,这段代码还能再乱点, ...

  9. java util Aarrys 类

    toString方法  产生一维数组的二维版本 public static String toString(type[] a) { //type 是传入数据类型 if (a == null) retu ...

  10. Java 清理和垃圾回收

    java.lang.ref.cleaner包 finalize()//该方法已过时,有风险,慎用 1.对象不可能被垃圾回收 2.垃圾回收并不等于"析构" 只有当垃圾回收发生时fin ...