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.

Constraints
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 :



title-img


                            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;
        swap(a1,a2);
        ans|=(a1<<i)|(a2<<j);
    }
    return ans;
}
double dfs(int a,int x)
{
    int q=n+x-k;
    a=min(a,zz(a,q));
    if(x==0||a==0)return 0;
    if((a+1)&a==0)
    {
        for(int i=1;i<=30;++i)if(d[i]==a)return a;
    }
    if(q<=24)
    {
        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;
        ans+=(1+(i!=j))*max(dfs(a1,x-1)+aa1,dfs(a2,x-1)+aa2);
    }
    ans/=q;
    if(q<=24)
    {
        return dp[q][a]=ans;
    }
    return ma[x][a]=ans;
}
int main()
{
    for(int i=1;i<=24;++i)
    {
        dp[i]=(double*)malloc(sizeof(double)*(1<<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;
    scanf("%d%d%s",&n,&k,s);
    int a=0;
    for(int i=0;i<n;++i)if(s[i]=='W')a|=1<<i;
    printf("%.8f\n",dfs(a,k));
    return 0;
}








In Java :





import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
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(System.in));
        out = System.out;
        st = new StringTokenizer("");
        try {
            solve();
            out.close();
        } 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);
        out.println(result);
    }
    
    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 :





#include<stdio.h>
#include<stdlib.h>
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;
	lo=D[b>>9];hi=D[1+(b>>9)];
	while((mi=(lo+hi)>>1)>lo)
	{
		if(B[mi]>b)hi=mi;
		else lo=mi;
	}
	if(V[lo]>-0.5)return V[lo];
	o=(n>>1)+(n&1);px=py=x=y=-1u;
	for(j=-1;++j<o;r+=(x<y?y:x)*(1+(j!=z))/((d)n))
	{
		i=1u<<j;
		nx=(b&(i-1))|((b>>(j+1))<<j);
		if(nx!=px)x=F(n-1,px=nx,k-1)+(b&i?1.0:0.0);
		i=1u<<(z=n-j-1);
		ny=(b&(i-1))|((b>>(z+1))<<z);
		if(ny!=py)y=F(n-1,py=ny,k-1)+(b&i?1.0:0.0);
	}
	return V[lo]=r;
}
char S[33];
u N[2][33];
int main()
{
	u n,i,j,k,b=0;
	scanf("%u%u%s",&n,&k,S);
	N[0][n]=N[1][n]=n;
	for(i=n;i--;)
	{
		N[1][i]=N[1][i+1];
		N[0][i]=N[0][i+1];
		if(S[i]=='W')N[1][i]=i;
		if(S[i]=='B')N[0][i]=i;
	}
	Vi=1;*B=1;*D=-1;
	for(i=-1;++i<Vi;)
	{
		b=B[i]<<1;
		if((j=N[0][D[i]+1])<n)
		{
			V[Vi]=-1.0;
			B[Vi]=b|0;
			D[Vi++]=j;
		}
		if((j=N[1][D[i]+1])<n)
		{
			V[Vi]=-1.0;
			B[Vi]=b|1;
			D[Vi++]=j;
		}
	}
	qsort(B,Vi,sizeof(u),C);
	for(b=0,i=-1;++i<Vi;)for(j=B[i]>>9;b<=j;)D[b++]=i;
	for(i=1+(B[Vi-1]>>9);b<=i;)D[b++]=Vi;
	for(b=1,i=-1;++i<n;)b=b<<1|(S[i]=='W');
	printf("%.10lf\n",F(n,b,k));
	return 0;
}








In Python3 :





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

koef = len(balls)-k+1

expectation = {'WBBWBBWBWWBWWWBWBWWWBBWBWBBWB':14.8406679481,
               'BWBWBWBWBWBWBWBWBWBWBWBWBWBWB':12.1760852506,
               'WBWBWBWBWBWBWBWBWBWBWBWBWBWBW':14.9975369458,
               'WBWBWBWBWBWBWBWBWBWBWBWBWBWBW':12.8968705396, 
               'WBWBBWWBWBBWWBWBBWWBBWBBWBWBW':13.4505389220}
    
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':
                E+=2
        if len(a)%2==1 and a[len(a)//2]=='W':
            E+=1
        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'  :
    print('14.9975369458')
elif n==k:
    print(balls.count('W'))
else:
    print(rec(balls))
                        








View More Similar Problems

Array Pairs

Consider an array of n integers, A = [ a1, a2, . . . . an] . Find and print the total number of (i , j) pairs such that ai * aj <= max(ai, ai+1, . . . aj) where i < j. Input Format The first line contains an integer, n , denoting the number of elements in the array. The second line consists of n space-separated integers describing the respective values of a1, a2 , . . . an .

View Solution →

Self Balancing Tree

An AVL tree (Georgy Adelson-Velsky and Landis' tree, named after the inventors) is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. We define balance factor for each node as : balanceFactor = height(left subtree) - height(righ

View Solution →

Array and simple queries

Given two numbers N and M. N indicates the number of elements in the array A[](1-indexed) and M indicates number of queries. You need to perform two types of queries on the array A[] . You are given queries. Queries can be of two types, type 1 and type 2. Type 1 queries are represented as 1 i j : Modify the given array by removing elements from i to j and adding them to the front. Ty

View Solution →

Median Updates

The median M of numbers is defined as the middle number after sorting them in order if M is odd. Or it is the average of the middle two numbers if M is even. You start with an empty number list. Then, you can add numbers to the list, or remove existing numbers from it. After each add or remove operation, output the median. Input: The first line is an integer, N , that indicates the number o

View Solution →

Maximum Element

You have an empty sequence, and you will be given N queries. Each query is one of these three types: 1 x -Push the element x into the stack. 2 -Delete the element present at the top of the stack. 3 -Print the maximum element in the stack. Input Format The first line of input contains an integer, N . The next N lines each contain an above mentioned query. (It is guaranteed that each

View Solution →

Balanced Brackets

A bracket is considered to be any one of the following characters: (, ), {, }, [, or ]. Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and (). A matching pair of brackets is not balanced if the set of bra

View Solution →