Hi all,

I have written the solution for ESAb ATAd question, and it works in local 
when I manually tested it. But it keep throws RE when I submitted it.
I have thought hard and still have no idea why it fails. It could fail 
because of interactive programming part, I really have no idea what caused 
it.
Anyone could advise why it fail? Thank you very much.

import java.util.Scanner;

public class Solution {
    static int e, ne;
    static int[] a;
    public static void solve(Scanner input, int b) {
        e = -1; ne = -1;
        a = new int[b];
        int i = 1, pos = 0, bit = -1, bit2 = -1;
        boolean e_change = false, ne_change = false;
        while (i <= 150 && pos <= (b - 1) / 2) {
            if (i > 1 && i % 10 == 1) {
                if (e > -1) {
                    System.out.println(e + 1);
                    bit = input.nextInt();
                    e_change = a[e] != bit;
                } else {
                    System.out.println("1");
                    bit = input.nextInt();
                }
                if (ne > -1) {
                    System.out.println(ne + 1);
                    bit = input.nextInt();
                    ne_change = a[ne] != bit;
                    i++;
                } else {
                    System.out.println("1");
                    bit = input.nextInt();
                }
                i += 2;

                if (e > -1 && ne == -1) {
                    if (e_change) comp(pos, b);
                } else if (e == -1 && ne > -1) {
                    if (ne_change) reverse(pos, b);
                } else {
                    if (e_change && ne_change) comp(pos, b);
                    else if (e_change && !ne_change) {comp(pos, b); 
reverse(pos, b);}
                    else if (!e_change && ne_change) reverse(pos, b);
                }
            } else {
                System.out.println(pos + 1); // query pair: i + 1 <-> b - i
                bit = input.nextInt();
                a[pos] = bit;
                System.out.println(b - pos);
                bit2 = input.nextInt();
                a[b - pos - 1] = bit2;

                if (bit == bit2) e = pos;
                else ne = pos;
                i += 2;
                pos++;
            }
        }


        StringBuilder sb = new StringBuilder();
        for (int aa : a) sb.append(aa);
        System.out.println(sb.toString());
        if (input.next().equals("N")) throw new RuntimeException("test 
fail");
    }

    // reverse up to index k
    private static void reverse(int k, int b) {
        for (int i = 0; i < k; i++) {
            int temp = a[i];
            a[i] = a[b - i - 1];
            a[b - i - 1] = temp;
        }
    }

    // complement up to index k
    private static void comp(int k, int b) {
        for (int i = 0; i < k; i++) {
            toggle(i);
            toggle(b - i - 1);
        }
    }

    private static void toggle(int k) {
        if (a[k] == 1) a[k] = 0;
        else a[k] = 1;
    }

    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        int T = input.nextInt();
        int B = input.nextInt();
        for (int ks = 1; ks <= T; ks++) {
            solve(input, B);
        }
    }
}


-- 
You received this message because you are subscribed to the Google Groups 
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to google-code+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-code/55eb87b9-0218-4e30-b955-81c867441367%40googlegroups.com.

Reply via email to