Created
July 12, 2015 06:56
-
-
Save joriki/5ae058d5d3e9458b9bbd to your computer and use it in GitHub Desktop.
Try to solve a game of consolidating numbers; see http://math.stackexchange.com/questions/1341929
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class Question1341929 { | |
public static void main (String [] args) { | |
for (int n = 2;;n++) { | |
int nslots = 2 * n - 1; | |
int [] p = new int [nslots]; | |
int max = 0; | |
int [] best = null; | |
for (int bits = (1 << (nslots - 2));bits < 1 << (nslots - 1);bits += 2) { | |
int hi = nslots - 1; | |
int lo = 0; | |
for (int i = nslots - 1;i >= 0;i--) | |
p [i] = (bits & (1 << i)) != 0 ? hi-- : lo++; | |
int value = value (p); | |
if (value > max) { | |
max = value; | |
best = p.clone (); | |
} | |
} | |
for (int i = 0;i < nslots;i++) | |
best [i]++; // add 1 for humans | |
System.out.println (n + " " + max + " " + Arrays.toString (best)); | |
} | |
} | |
static int max = 1000; | |
static int [] v = new int [max]; | |
static int [] left = new int [max]; | |
static int [] right = new int [max]; | |
static int value (int [] p) { | |
int n = (p.length + 1) >> 1; | |
for (int i = 1;i <= n;i++) | |
v [n + i - 2] = v [n - i] = i; | |
for (int i = 1;i < p.length;i++) { | |
left [i] = i - 1; | |
right [i - 1] = i; | |
} | |
left [0] = right [p.length - 1] = -1; | |
for (int i : p) | |
play (i); | |
return v [p [p.length - 1]]; | |
} | |
static void play (int i) { | |
play (i,left,right); | |
play (i,right,left); | |
} | |
static void play (int i,int [] n1,int [] n2) { | |
if (n1 [i] != -1) { | |
v [n1 [i]] += v [i]; | |
n2 [n1 [i]] = n2 [i]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment