ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1587. Flying Pig

WA17
Posted by vinnykz 30 Nov 2008 05:33
import java.io.*;
import java.math.*;
import java.util.*;

public class Main {


    static BigInteger ans, a2, a3;
    static int dans[], t[];

    static StreamTokenizer in;
    static PrintWriter out;
    static double C32  = Math.log(3) / Math.log(2);
    static double C23  = Math.log(2) / Math.log(3);

    static int nextInt() throws IOException
   {
      in.nextToken();
      return (int)in.nval;
   }

    public static void count(int p[]) {
        dans[0] = 2;
        t[2] = p[2] - dans[2];
        t[3] = p[3] - dans[3];

        if (t[2] >= 0 && t[3] >= 0) {
            dans[2] = p[2];
            dans[3] = p[3];
        } else
        if (t[2] < 0 && t[3] < 0);
        else {
            if (t[2] == 0 || t[3] == 0);
            else {
                if (t[2] > 0 && t[3] < 0)
                {
                    if ((double)t[2] * C23 + (double)t[3] > 0) {
                        dans[2] = p[2];
                        dans[3] = p[3];
                    }
                } else
                if (t[2] < 0 && t[3] > 0)
                {
                    if ((double)t[3] * C32 + (double)t[2] > 0) {
                        dans[2] = p[2];
                        dans[3] = p[3];
                    }
                }
            }
        }
    }

    public static void main(String[] args) throws Exception {

            in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
            PrintWriter print = new PrintWriter(System.out);

            BigInteger negStart, posStart;
            ans = BigInteger.ZERO;

            t = new int [4];
            dans = new int[4];
            dans[0] = 0; dans[1] = 0; dans[2] = 0; dans[3] = 0;

            int neg[] = new int[4];
            int pos[] = new int[4];
            neg[0] = 0; neg[1] = 0; neg[2] = 0; neg[3] = 0;
            pos[0] = 0; pos[1] = 0; pos[2] = 0; pos[3] = 0;

            int saveneg[] = new int[4];
            int savepos[] = new int[4];
            for(int j = 0; j < 4; j++) {
                saveneg[j] = neg[j];
                savepos[j] = pos[j];
            }

            int n = nextInt();
            int a, b = 0;

            for(int i = 1; i <= n; i++) {

                a = nextInt();
                if (i == 1) {
                    ans = BigInteger.valueOf(a);
                    b = a;
                } else if (b < a) b = a;

                if (neg[1] + neg[2] + neg[3] == 0 && a < 0) {
                    ++neg[-a];
                    neg[0] = 0;
                } else {
                    if (a == 0) {
                        // count
                        neg[0] = 0; neg[1] = 0; neg[2] = 0; neg[3] = 0;
                    } else
                    if (a < 0) {
                        ++neg[-a];
                        neg[0] = 2 - neg[0];
                    }
                    else if (neg[1] + neg[2] + neg[3] > 0) ++neg[a];
                }

                if (pos[1] + pos[2] + pos[3] == 0 && a > 0) {
                    ++pos[a];
                    pos[0] = 2;
                } else {
                    if (a == 0) {
                        // count
                        pos[0] = 0; pos[1] = 0; pos[2] = 0; pos[3] = 0;
                    } else
                        if (a < 0) {
                            if (pos[1] + pos[2] + pos[3] > 0) {
                                ++pos[-a];
                                pos[0] = 2 - pos[0];
                            }
                        } else ++pos[a];
                }

                if (neg[0] == 2) count(neg);
                if (pos[0] == 2) count(pos);

            }

            if (dans[0] == 2) {
                a2 = BigInteger.valueOf(2);
                a3 = BigInteger.valueOf(3);
                a2 = a2.pow(dans[2]);
                a3 = a3.pow(dans[3]);
                a2 = a2.multiply(a3);
                if (a2.compareTo(ans) > 0) ans = a2;
            }

            a2 = BigInteger.valueOf(b);
            if (ans.compareTo(a2) < 0) ans = a2;

            String r = ans.toString();
            System.out.println(r);
            print.close();

        }

    }