[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. ubuntu 禁用 guest 账户

    第一步: run the command(s) below:        (编辑如下文件) sudo vi /usr/share/lightdm/lightdm.conf.d/50-ubuntu.c ...

  2. windows下设置计划任务自动执行PHP脚本

    背景: 环境部署在linux下或者windows中,可以使用windows的自动任务设置自动执行脚本执行一些日常运维任务 图形界面设置相对比较简单 准备工作: wamp(集成的PHP执行环境) 已经写 ...

  3. LinkedList源码分析笔记(jdk1.8)

    1.特点 LinkedList的底层实现是由一个双向链表实现的,可以从两端作为头节点遍历链表. 允许元素为null 线程不安全 增删相对ArrayList快,改查相对ArrayList慢(curd都会 ...

  4. 配置Sublime Text2的python运行环境(Sublime Text 3也类似)

    1. 前言 用Sublime Text 2 配置Python运用环境,有简单配置还有像IDLE一样的配置,本文分成第一部分和第二部分.   2. 配置 第一部分(简单配置)   1.只需要打开Pref ...

  5. h5新API之WebStorage解决页面数据通信问题

    localStorage相信大家都不陌生,今天我们要讨论的不是怎么存储数据,获取数据.而是看看WebStorage的一些妙用,相信大家在开发中遇到过这样一个场景,一个页面中嵌套一个iframe,ifr ...

  6. java 对象锁学习

    机制 锁机制是用来解决多线程共享资源时产生的冲突问题的.java 为每一个对象关联一个对象锁,通常把锁分为对象锁和类锁,他们的本质都是对象锁,只不过对象锁关联的是类的 Object 对象 (java. ...

  7. LeetCode(35):搜索插入位置

    Easy! 题目描述: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 示例 1: 输入: [1 ...

  8. python接口自动化测试十二:对返回的json的简单操作

    # 1.requests里面自带解析器转字典 print(r.json()) print(type(r.json())) # 取出json中的'result_sk_temp'字段 # {"r ...

  9. 查看Windows系统里的进程已运行的时间

    搜索 ProcessExplorer ,可以去微软下载它.右键点击项类,selcet conlumns...在 Process Performance 里 选择start time.有了进程的启动时间 ...

  10. 依赖倒置原则(Dependence Inversion Principle,DIP)

    依赖倒转原则就是 A.要依赖于抽象,不要依赖于实现.(Abstractions should not depend upon details. Details should depend upon a ...