import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
import java.util.StringTokenizer;
/*public*/class BalloonsTest {
// @Test
public void test_ordered_5_5() {
int[] cs = toIntArray( "1 2 3 4 5 1 2 3 4 5" );
int[] ps = toIntArray( "1 2 3 4 5 6 7 8 9 10" );
double[] sum = new double[cs.length + 1];
double[] cnt = new double[cs.length + 1];
for (int mask = 0; mask < 1 << cs.length; mask++) {
Set<Integer> set = new HashSet<>();
double tsum = 0;
for (int bit = 0; bit < cs.length; bit++) {
if ( ( mask & ( 1 << bit ) ) > 0 ) {
set.add( cs[bit] );
tsum += ps[bit];
}
}
++cnt[set.size()];
sum[set.size()] += tsum;
}
double[] sums = new double[cs.length + 1];
double[] cnts = new double[cs.length + 1];
for (int i = 0; i < sums.length; i++) {
for (int j = i; j < cnts.length; j++) {
sums[i] += sum[j];
cnts[i] += cnt[j];
}
}
for (int i = 0; i <= 5; i++) {
// double act = Balloons.solve( cs.length, i, cs, ps );
double exp = sums[i] / cnts[i];
System.
out.
printf( "M=%d => %f\n", i, exp
); // Assert.assertEquals( "i=" + i, exp, act, 1e-6 );
}
}
// @Test
public void test_ordered_6_123() {
int[] cs = toIntArray( "1 2 2 3 3 3" );
int[] ps = toIntArray( "2 3 4 5 6 7" );
double[] sum = new double[cs.length + 1];
double[] cnt = new double[cs.length + 1];
for (int mask = 0; mask < 1 << cs.length; mask++) {
Set<Integer> set = new HashSet<>();
double tsum = 0;
for (int bit = 0; bit < cs.length; bit++) {
if ( ( mask & ( 1 << bit ) ) > 0 ) {
set.add( cs[bit] );
tsum += ps[bit];
}
}
++cnt[set.size()];
sum[set.size()] += tsum;
}
double[] sums = new double[cs.length + 1];
double[] cnts = new double[cs.length + 1];
for (int i = 0; i < sums.length; i++) {
for (int j = i; j < cnts.length; j++) {
sums[i] += sum[j];
cnts[i] += cnt[j];
}
}
for (int i = 0; i <= 3; i++) {
// double act = Balloons.solve( cs.length, i, cs, ps );
double exp = sums[i] / cnts[i];
System.
out.
printf( "M=%d => %f\n", i, exp
); // Assert.assertEquals( "i=" + i, exp, act, 1e-6 );
}
}
// @Test
public void test_ordered_3() {
int[] cs = toIntArray( "1 2 3" );
int[] ps = toIntArray( "1 2 3" );
double[] sum = new double[cs.length + 1];
double[] cnt = new double[cs.length + 1];
for (int mask = 0; mask < 1 << cs.length; mask++) {
int idx
= Integer.
bitCount( mask
); // int tsum = 0;
for (int bit = 0; bit < cs.length; bit++) {
if ( ( mask & ( 1 << bit ) ) > 0 ) {
sum[idx] += ps[bit];
}
}
++cnt[idx];
}
double[] sums = new double[cs.length + 1];
double[] cnts = new double[cs.length + 1];
for (int i = 0; i < sums.length; i++) {
for (int j = i; j < cnts.length; j++) {
sums[i] += sum[j];
cnts[i] += cnt[j];
}
}
for (int i = 0; i <= cs.length; i++) {
// double act = Balloons.solve( cs.length, i, cs, ps );
double exp = sums[i] / cnts[i];
System.
out.
printf( "M=%d => %f\n", i, exp
); // Assert.assertEquals( "i=" + i, exp, act, 1e-6 );
}
}
// @Test
public void test_ordered_10() {
int[] cs = toIntArray( "1 2 3 4 5 6 7 8 9 10" );
int[] ps = toIntArray( "1 2 3 4 5 6 7 8 9 10" );
double[] sum = new double[cs.length + 1];
double[] cnt = new double[cs.length + 1];
for (int mask = 0; mask < 1 << cs.length; mask++) {
int idx
= Integer.
bitCount( mask
); // int tsum = 0;
for (int bit = 0; bit < cs.length; bit++) {
if ( ( mask & ( 1 << bit ) ) > 0 ) {
sum[idx] += ps[bit];
}
}
++cnt[idx];
}
double[] sums = new double[cs.length + 1];
double[] cnts = new double[cs.length + 1];
for (int i = 0; i < sums.length; i++) {
for (int j = i; j < cnts.length; j++) {
sums[i] += sum[j];
cnts[i] += cnt[j];
}
}
for (int i = 0; i <= cs.length; i++) {
// double act = Balloons.solve( cs.length, i, cs, ps );
double exp = sums[i] / cnts[i];
System.
out.
printf( "M=%d => %f\n", i, exp
); // Assert.assertEquals( "i=" + i, exp, act, 1e-6 );
}
}
private int[] toIntArray
( String s
) { LinkedList<Integer> list = new LinkedList<>();
while ( tok.hasMoreElements() ) {
list.
add( Integer.
parseInt( tok.
nextToken() ) ); }
int[] res = new int[list.size()];
int i = 0;
for (int item : list) {
res[i++] = item;
}
return res;
}
public static void main
( String[] args
) { new BalloonsTest().test_ordered_3();
System.
out.
println( "~~~ ~~~ ~~~" ); new BalloonsTest().test_ordered_10();
System.
out.
println( "~~~ ~~~ ~~~" ); new BalloonsTest().test_ordered_5_5();
System.
out.
println( "~~~ ~~~ ~~~" ); new BalloonsTest().test_ordered_6_123();
}
}