Guest User

SPOJ Histogram

a guest
Jun 21st, 2016
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.07 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.InputMismatchException;
  3.  
  4. class Histogram
  5. {
  6.     public static void main(String[] args)
  7.     {
  8.         InputReader in = new InputReader(System.in);
  9.         OutputWriter out = new OutputWriter(System.out);
  10.  
  11.         Solver solver = new Solver(in, out);
  12.  
  13.         solver.solve(1);
  14.  
  15.         out.flush();
  16.  
  17.         in.close();
  18.         out.close();
  19.     }
  20.  
  21.     static class Solver
  22.     {
  23.         int n, heights[], heightSumCount[];
  24.         InputReader in;
  25.         OutputWriter out;
  26.  
  27.         void solve(int testNumber)
  28.         {
  29.             while (true)
  30.             {
  31.                 n = in.nextInt();
  32.  
  33.                 if (n == 0)
  34.                     break;
  35.  
  36.                 heights = new int[n];
  37.  
  38.                 for (int i = 0; i < n; i++)
  39.                     heights[i] = in.nextInt();
  40.  
  41.                 heightSumCount = new int[2200];
  42.  
  43.                 count(0, 0, 0);
  44.  
  45.                 for (int i = 2100; i >= 0; i--)
  46.                 {
  47.                     if (heightSumCount[i] > 0)
  48.                     {
  49.                         out.println((i + 2 * n) + " " + heightSumCount[i]);
  50.  
  51.                         break;
  52.                     }
  53.                 }
  54.             }
  55.         }
  56.  
  57.         long count(int prevHeight, int heightSum, int mask)
  58.         {
  59.             int usedCount = usedCount(mask);
  60.  
  61.             if (usedCount == n - 1)
  62.             {
  63.                 int temp = 1;
  64.  
  65.                 for (int i = 0; i < n; i++)
  66.                 {
  67.                     if ((mask & temp) == 0)
  68.                         heightSumCount[heightSum + Math.abs(heights[n - i - 1] - prevHeight) + heights[n - i - 1]]++;
  69.  
  70.                     temp <<= 1;
  71.                 }
  72.  
  73.                 return 1;
  74.             }
  75.  
  76.             long count = 0;
  77.             int temp = 1;
  78.  
  79.             for (int i = 0; i < n; i++)
  80.             {
  81.                 if ((mask & temp) == 0)
  82.                     count += count(heights[n - i - 1], heightSum + Math.abs(heights[n - i - 1] - prevHeight), mask |
  83.                             temp);
  84.  
  85.                 temp <<= 1;
  86.             }
  87.  
  88.             return count;
  89.         }
  90.  
  91.         int usedCount(int mask)
  92.         {
  93.             int count = 0;
  94.  
  95.             while (mask != 0)
  96.             {
  97.                 count += mask & 1;
  98.                 mask /= 2;
  99.             }
  100.  
  101.             return count;
  102.         }
  103.  
  104.         public Solver(InputReader in, OutputWriter out)
  105.         {
  106.             this.in = in;
  107.             this.out = out;
  108.         }
  109.  
  110.     }
  111.  
  112.     static class InputReader
  113.     {
  114.         private InputStream stream;
  115.         private byte[] buf = new byte[1024];
  116.         private int curChar;
  117.         private int numChars;
  118.  
  119.         public InputReader(InputStream stream)
  120.         {
  121.             this.stream = stream;
  122.         }
  123.  
  124.         public int read()
  125.         {
  126.             if (numChars == -1)
  127.                 throw new InputMismatchException();
  128.  
  129.             if (curChar >= numChars)
  130.             {
  131.                 curChar = 0;
  132.                 try
  133.                 {
  134.                     numChars = stream.read(buf);
  135.                 }
  136.                 catch (IOException e)
  137.                 {
  138.                     throw new InputMismatchException();
  139.                 }
  140.                 if (numChars <= 0)
  141.                     return -1;
  142.             }
  143.  
  144.             return buf[curChar++];
  145.         }
  146.  
  147.         public int nextInt()
  148.         {
  149.             int c = read();
  150.  
  151.             while (isSpaceChar(c))
  152.                 c = read();
  153.  
  154.             int sgn = 1;
  155.  
  156.             if (c == '-')
  157.             {
  158.                 sgn = -1;
  159.                 c = read();
  160.             }
  161.  
  162.             int res = 0;
  163.  
  164.             do
  165.             {
  166.                 if (c < '0' || c > '9')
  167.                     throw new InputMismatchException();
  168.  
  169.                 res *= 10;
  170.                 res += c & 15;
  171.  
  172.                 c = read();
  173.             } while (!isSpaceChar(c));
  174.  
  175.             return res * sgn;
  176.         }
  177.  
  178.         public int[] nextIntArray(int arraySize)
  179.         {
  180.             int array[] = new int[arraySize];
  181.  
  182.             for (int i = 0; i < arraySize; i++)
  183.                 array[i] = nextInt();
  184.  
  185.             return array;
  186.         }
  187.  
  188.         public long nextLong()
  189.         {
  190.             int c = read();
  191.  
  192.             while (isSpaceChar(c))
  193.                 c = read();
  194.  
  195.             int sign = 1;
  196.  
  197.             if (c == '-')
  198.             {
  199.                 sign = -1;
  200.  
  201.                 c = read();
  202.             }
  203.  
  204.             long result = 0;
  205.  
  206.             do
  207.             {
  208.                 if (c < '0' || c > '9')
  209.                     throw new InputMismatchException();
  210.  
  211.                 result *= 10;
  212.                 result += c & 15;
  213.  
  214.                 c = read();
  215.             } while (!isSpaceChar(c));
  216.  
  217.             return result * sign;
  218.         }
  219.  
  220.         public long[] nextLongArray(int arraySize)
  221.         {
  222.             long array[] = new long[arraySize];
  223.  
  224.             for (int i = 0; i < arraySize; i++)
  225.                 array[i] = nextLong();
  226.  
  227.             return array;
  228.         }
  229.  
  230.         public float nextFloat() // problematic
  231.         {
  232.             float result, div;
  233.             byte c;
  234.  
  235.             result = 0;
  236.             div = 1;
  237.             c = (byte) read();
  238.  
  239.             while (c <= ' ')
  240.                 c = (byte) read();
  241.  
  242.             boolean isNegative = (c == '-');
  243.  
  244.             if (isNegative)
  245.                 c = (byte) read();
  246.  
  247.             do
  248.             {
  249.                 result = result * 10 + c - '0';
  250.             } while ((c = (byte) read()) >= '0' && c <= '9');
  251.  
  252.             if (c == '.')
  253.                 while ((c = (byte) read()) >= '0' && c <= '9')
  254.                     result += (c - '0') / (div *= 10);
  255.  
  256.             if (isNegative)
  257.                 return -result;
  258.  
  259.             return result;
  260.         }
  261.  
  262.         public double nextDouble() // not completely accurate
  263.         {
  264.             double ret = 0, div = 1;
  265.             byte c = (byte) read();
  266.  
  267.             while (c <= ' ')
  268.                 c = (byte) read();
  269.  
  270.             boolean neg = (c == '-');
  271.  
  272.             if (neg)
  273.                 c = (byte) read();
  274.  
  275.             do
  276.             {
  277.                 ret = ret * 10 + c - '0';
  278.             } while ((c = (byte) read()) >= '0' && c <= '9');
  279.  
  280.             if (c == '.')
  281.                 while ((c = (byte) read()) >= '0' && c <= '9')
  282.                     ret += (c - '0') / (div *= 10);
  283.  
  284.             if (neg)
  285.                 return -ret;
  286.  
  287.             return ret;
  288.         }
  289.  
  290.         public String next()
  291.         {
  292.             int c = read();
  293.  
  294.             while (isSpaceChar(c))
  295.                 c = read();
  296.  
  297.             StringBuilder res = new StringBuilder();
  298.  
  299.             do
  300.             {
  301.                 res.appendCodePoint(c);
  302.  
  303.                 c = read();
  304.             } while (!isSpaceChar(c));
  305.  
  306.             return res.toString();
  307.         }
  308.  
  309.         public boolean isSpaceChar(int c)
  310.         {
  311.             return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  312.         }
  313.  
  314.         public String nextLine()
  315.         {
  316.             int c = read();
  317.  
  318.             StringBuilder result = new StringBuilder();
  319.  
  320.             do
  321.             {
  322.                 result.appendCodePoint(c);
  323.  
  324.                 c = read();
  325.             } while (!isNewLine(c));
  326.  
  327.             return result.toString();
  328.         }
  329.  
  330.         public boolean isNewLine(int c)
  331.         {
  332.             return c == '\n';
  333.         }
  334.  
  335.         public void close()
  336.         {
  337.             try
  338.             {
  339.                 stream.close();
  340.             }
  341.             catch (IOException e)
  342.             {
  343.                 e.printStackTrace();
  344.             }
  345.         }
  346.  
  347.     }
  348.  
  349.     static class OutputWriter
  350.     {
  351.         private PrintWriter writer;
  352.  
  353.         public OutputWriter(OutputStream stream)
  354.         {
  355.             writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
  356.                     stream)));
  357.         }
  358.  
  359.         public OutputWriter(Writer writer)
  360.         {
  361.             this.writer = new PrintWriter(writer);
  362.         }
  363.  
  364.         public void println(int x)
  365.         {
  366.             writer.println(x);
  367.         }
  368.  
  369.         public void print(int x)
  370.         {
  371.             writer.print(x);
  372.         }
  373.  
  374.         public void println(int array[], int size)
  375.         {
  376.             for (int i = 0; i < size; i++)
  377.                 println(array[i]);
  378.         }
  379.  
  380.         public void print(int array[], int size)
  381.         {
  382.             for (int i = 0; i < size; i++)
  383.                 print(array[i] + " ");
  384.         }
  385.  
  386.         public void println(long x)
  387.         {
  388.             writer.println(x);
  389.         }
  390.  
  391.         public void print(long x)
  392.         {
  393.             writer.print(x);
  394.         }
  395.  
  396.         public void println(long array[], int size)
  397.         {
  398.             for (int i = 0; i < size; i++)
  399.                 println(array[i]);
  400.         }
  401.  
  402.         public void print(long array[], int size)
  403.         {
  404.             for (int i = 0; i < size; i++)
  405.                 print(array[i]);
  406.         }
  407.  
  408.         public void println(float num)
  409.         {
  410.             writer.println(num);
  411.         }
  412.  
  413.         public void print(float num)
  414.         {
  415.             writer.print(num);
  416.         }
  417.  
  418.         public void println(double num)
  419.         {
  420.             writer.println(num);
  421.         }
  422.  
  423.         public void print(double num)
  424.         {
  425.             writer.print(num);
  426.         }
  427.  
  428.         public void println(String s)
  429.         {
  430.             writer.println(s);
  431.         }
  432.  
  433.         public void print(String s)
  434.         {
  435.             writer.print(s);
  436.         }
  437.  
  438.         public void println()
  439.         {
  440.             writer.println();
  441.         }
  442.  
  443.         public void printSpace()
  444.         {
  445.             writer.print(" ");
  446.         }
  447.  
  448.         public void flush()
  449.         {
  450.             writer.flush();
  451.         }
  452.  
  453.         public void close()
  454.         {
  455.             writer.close();
  456.         }
  457.  
  458.     }
  459.  
  460.     static class CMath
  461.     {
  462.         static long power(long number, long power)
  463.         {
  464.             if (number == 1 || number == 0 || power == 0)
  465.                 return 1;
  466.  
  467.             if (power == 1)
  468.                 return number;
  469.  
  470.             if (power % 2 == 0)
  471.                 return power(number * number, power / 2);
  472.             else
  473.                 return power(number * number, power / 2) * number;
  474.         }
  475.  
  476.         static long modPower(long number, long power, long mod)
  477.         {
  478.             if (number == 1 || number == 0 || power == 0)
  479.                 return 1;
  480.  
  481.             number = mod(number, mod);
  482.  
  483.             if (power == 1)
  484.                 return number;
  485.  
  486.             long square = mod(number * number, mod);
  487.  
  488.             if (power % 2 == 0)
  489.                 return modPower(square, power / 2, mod);
  490.             else
  491.                 return mod(modPower(square, power / 2, mod) * number, mod);
  492.         }
  493.  
  494.         static long moduloInverse(long number, long mod)
  495.         {
  496.             return modPower(number, mod - 2, mod);
  497.         }
  498.  
  499.         static long mod(long number, long mod)
  500.         {
  501.             return number - (number / mod) * mod;
  502.         }
  503.  
  504.         static int gcd(int a, int b)
  505.         {
  506.             if (b == 0)
  507.                 return a;
  508.             else
  509.                 return gcd(b, a % b);
  510.         }
  511.  
  512.     }
  513.  
  514. }
Add Comment
Please, Sign In to add comment