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

```                            ```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.IOException;
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 StringTokenizer st;

static int cnt;
static int[][] next;

public static void main(String[] args) throws IOException {
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;
}

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;
}
}
}
}

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()) {
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))```
```

## 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,

## 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

## 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

## Waiter

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

## 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

## 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):