Create a gist now

Instantly share code, notes, and snippets.

@tanzaku /CanidsSeesaw.java Secret
Last active Jul 24, 2017

What would you like to do?
TCO17 Round 2C Easy CanidsSeesaw
import java.util.*;
public class CanidsSeesaw {
int count(int[] w, int[] f, int[] ps) {
int s1 = 0, s2 = 0;
int p = 0;
for (int i = 0; i < w.length; i++) {
s1 += w[i];
s2 += f[ps[i]];
if (s1 < s2) p++;
}
return p;
}
public int[] construct(int[] wolf, int[] fox, int k) {
int[] res = new int[fox.length];
for (int i = 0; i < fox.length; i++) {
res[i] = i;
}
for (int i = 0; i < fox.length; i++) {
int[] unused = Arrays.copyOfRange(res, i, fox.length);
for (int j = 0; j < unused.length; j++) {
for (int l = unused.length - 1; l > j; l--) {
if (fox[unused[l]] < fox[unused[l-1]]) {
int t = unused[l];
unused[l] = unused[l-1];
unused[l-1] = t;
}
}
}
for (int j = 0; j < unused.length; j++) {
res[i] = unused[j];
for (int l = i + 1, m = unused.length - 1; l < fox.length; m--) {
if (m != j) {
res[l++] = unused[m];
}
}
// dump(count(wolf, fox, res), res, i, j);
if (count(wolf, fox, res) >= k) break;
}
}
// dump(count(wolf, fox, res), res, k);
if (count(wolf, fox, res) != k) {
return new int[0];
}
return res;
}
static void dump(Object... o) { System.err.println(Arrays.deepToString(o)); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment