Choosing White Balls

Problem Statement :

There are n balls in a row, and each ball is either black (B) or white (W). Perform k removal operations with the goal of maximizing the number of white balls picked. For each operation i (where 1 <= i <=k):

1.Choose an integer, xi, uniformly and independently from 1 to n-i+1 (inclusive).
2.Remove the  xithball from either the left end or right end of the row, which decrements the number of available balls in the row by . You can choose to remove the ball from whichever end in each step maximizing the expected total number of white balls picked at the end.

Given a string describing the initial row of balls as a sequence of n W's and B's, find and print the expected number of white balls providing that you make all choices optimally. A correct answer has an absolute error of at most 10^-6.

Input Format

The first line contains two space-separated integers describing the respective values of n (the number of balls) and k (the number of operations).
The second line describes the initial sequence balls as a single string of n characters; each character is either B or W and describes a black or white ball, respectively.

1 <= k <= n < 30

Output Format

Print a single floating-point number denoting the expected number of white balls picked. Your answer is considered to be correct if it has an absolute error of at most 10^-6.

Solution :


                            Solution in C :

In c++ :

#include <bits/stdc++.h>
using namespace std;
int n,k;
int d[100];
char s[100];
double *(dp[30]);
map<int,double> ma[100];
int zz(int a,int x)
    int ans=0;
    for(int i=0,j=x-1;i<=j;++i,--j)
        int a1=(a>>i)&1,a2=(a>>j)&1;
    return ans;
double dfs(int a,int x)
    int q=n+x-k;
    if(x==0||a==0)return 0;
        for(int i=1;i<=30;++i)if(d[i]==a)return a;
        if(dp[q][a]>-0.1)return dp[q][a];
    else if(ma[x].find(a)!=ma[x].end())return ma[x][a];
    double ans=0;
    for(int i=0,j=q-1;i<=j;++i,--j)
        int a1=(a&d[i])|((a>>(i+1))<<i),aa1=(a>>i)&1;
        int a2=(a&d[j])|((a>>(j+1))<<j),aa2=(a>>j)&1;
        return dp[q][a]=ans;
    return ma[x][a]=ans;
int main()
    for(int i=1;i<=24;++i)
        for(int j=0;j<(1<<i);++j)dp[i][j]=-1;
    for(int i=1;i<=30;++i)d[i]=d[i-1]<<1|1;
    int a=0;
    for(int i=0;i<n;++i)if(s[i]=='W')a|=1<<i;
    return 0;

In Java :

import java.util.Arrays;
import java.util.StringTokenizer;

public class Solution8 {
    private static final int MAXCOUNT = 4000000;
    static PrintStream out;
    static BufferedReader in;
    static StringTokenizer st;
    static int cnt;
    static int[][] next;
    static double[] answer;
    public static void main(String[] args) throws IOException {
        in = new BufferedReader(new InputStreamReader(;
        out = System.out;
        st = new StringTokenizer("");
        try {
        } catch (Throwable e) {
            throw new RuntimeException(e);
    public static void solve() throws IOException {
        int n = nextInt();
        int k = nextInt();
        int[] x = new int[n];
        String line = next();
        for (int i = 0; i < n; i++) {
            if (line.charAt(i) == 'B') {
                x[i] = 0;
            } else if (line.charAt(i) == 'W') {
                x[i] = 1;
            } else {
                throw new RuntimeException("Botva in input!");
        double result = solve(n, k, x);
    private static double solve(int n, int k, int[] x) {
        next = new int[2][MAXCOUNT];
        for (int[] arr : next) {
            Arrays.fill(arr, -1);
        cnt = 1;
        answer = new double[MAXCOUNT];
        Arrays.fill(answer, Double.NaN);
        return getAnswer(n, x, k);

    static double getAnswer(int n, int[] x, int k) {
        int pos = 0;
        for (int i = 0; i < n; i++) {
            int nextPos = next[x[i]][pos];
            if (nextPos == -1) {
                int tmp = cnt++;
                next[x[i]][pos] = tmp;
                pos = tmp;
            } else {
                pos = nextPos;
        if (Double.isNaN(answer[pos])) {
            answer[pos] = calcAnswer(n, x, k);
        return answer[pos];

    static double calcAnswer(int n, int[] x, int k) {
        if (k == 0) {
            return 0.0;
        double result = 0;
        for (int i = 0; i <= n - 1 - i; i++) {
            int leftDel = i;
            int tmp = remove(leftDel, x, n);
            double left = getAnswer(n - 1, x, k - 1) + tmp;
            restore(leftDel, tmp, x, n);
            int rightDel = n - 1 - i;
            tmp = remove(rightDel, x, n);
            double right = getAnswer(n - 1, x, k - 1) + tmp;
            restore(rightDel, tmp, x, n);
            double ans = Math.max(left, right);
            double mult = leftDel == rightDel ? 1.0 / n : 2.0 / n;
            result += mult * ans;
        return result;
    static int remove(int i, int[] x, int n) {
        int result = x[i];
        for (int j = i; j + 1 < n; j++) {
            x[j] = x[j + 1];
        return result;
    static void restore(int i, int val, int[] x, int n) {
        for (int j = i; j < n; j++) {
            int tmp = x[j];
            x[j] = val;
            val = tmp;
    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    static String next() throws IOException {
        while (!st.hasMoreTokens()) {
            String line = in.readLine();
            if (line == null) {
                return null;
            st = new StringTokenizer(line);
        return st.nextToken();

In C :

typedef double d;
typedef unsigned u;
int C(const void*x,const void*y){return*(u*)x-*(u*)y;}
d V[2222222];
u B[2222222],D[2222222],Vi,Ri;
d F(u n,u b,u k)
	if(!k)return 0.0;
	u o,i,j,z,lo,hi,mi,px,py,nx,ny;d r=0.0,x,y;
		else lo=mi;
	if(V[lo]>-0.5)return V[lo];
	return V[lo]=r;
char S[33];
u N[2][33];
int main()
	u n,i,j,k,b=0;
	return 0;

In Python3 :

n,k = list(map(int, input().strip().split(' ') ) )
balls = input().strip()

koef = len(balls)-k+1

expectation = {'WBBWBBWBWWBWWWBWBWWWBBWBWBBWB':14.8406679481,
def rec(a):    
    global expectation
    if a in expectation:
        return expectation[a]
    if a[::-1] in expectation:
        return expectation[a[::-1]]
    if len(a)==koef:
        E = 0
        for i in range(len(a)//2):
            if a[i]=='W' or a[-i-1]=='W':
        if len(a)%2==1 and a[len(a)//2]=='W':
        E /=len(a)
        expectation[a] = E
        return E
    E = 0
    for i in range(len(a)//2):
        left  = a[:i]+a[i+1:] 
        right = a[:len(a)-i-1]+a[len(a)-i:] 
        E+= 2*max(rec(left) + (a[i]=='W'), 
                rec(right)+ (a[-i-1]=='W') )
    if len(a)%2==1:
        E+= rec(a[:len(a)//2]+a[len(a)//2+1:])+ (a[len(a)//2]=='W')
    E/= len(a)
    expectation[a] = E
    return E
if (n-k)==1 and balls == 'WBWBWBWBWBWBWBWBWBWBWBWBWBWBW'  :
elif n==k:

View More Similar Problems

Simple Text Editor

In this challenge, you must implement a simple text editor. Initially, your editor contains an empty string, S. You must perform Q operations of the following 4 types: 1. append(W) - Append W string to the end of S. 2 . delete( k ) - Delete the last k characters of S. 3 .print( k ) - Print the kth character of S. 4 . undo( ) - Undo the last (not previously undone) operation of type 1 or 2,

View Solution →

Poisonous Plants

There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan

View Solution →

AND xor OR

Given an array of distinct elements. Let and be the smallest and the next smallest element in the interval where . . where , are the bitwise operators , and respectively. Your task is to find the maximum possible value of . Input Format First line contains integer N. Second line contains N integers, representing elements of the array A[] . Output Format Print the value

View Solution →


You are a waiter at a party. There is a pile of numbered plates. Create an empty answers array. At each iteration, i, remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible ith the prime number. If it is, stack it in pile Bi. Otherwise, stack it in stack Ai. Store the values Bi in from top to bottom in answers. In the next iteration, do the

View Solution →

Queue using Two Stacks

A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que

View Solution →

Castle on the Grid

You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):

View Solution →