Advertisement
Miklak

Balanced Art

Oct 21st, 2013
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.87 KB | None | 0 0
  1. /* http://gosucoder.com/problem/P3 */
  2. /* We can divide the sculpture into layers (levels). Then we'll use following facts:
  3.  * 1. On each level, all weights must be equal
  4.  * 2. Weights on the next level must be half the weight of the ones layer down
  5.  * Therefore:
  6.  * A. If we know one of the weights, we know all of the weights. This means we can easily calculate the total weight of the sculpture: total = weight * 2^level. If we get different results then boom everything collapses.
  7.  * B. '?'s are restricted to positive integers, so the total weight must be evenly divisible by 2^level. We only need to checking if it's divisible by 2^(maximum level). If it isn't, then some weighs would have to be 0.5kg but little Jack can't afford these so boom everything collapses.
  8. */
  9.  
  10. using System;
  11.  
  12.     class Test
  13.     {
  14.         static void Main()
  15.         {
  16.             int n = int.Parse(Console.ReadLine());
  17.             string[] a = new string[n];
  18.             for (int i = 0; i < n; i++)
  19.                 a[i] = Console.ReadLine();
  20.            
  21.  
  22.             for (int i = 0; i < n; i++)
  23.             {
  24.                 int lvl, j, total = 0, x, maxlvl = 0;
  25.                 bool maxFound, valid;
  26.                 lvl = 0;
  27.                 maxFound = false;
  28.                 valid = true;
  29.                 for (j = 1; j < a[i].Length; j++)
  30.                 {
  31.                     if (a[i][j] == '[') // lvl up
  32.                     {
  33.                         if (++lvl > maxlvl)
  34.                             maxlvl++;
  35.                     }
  36.                     else if (a[i][j] == ']') lvl--; // lvl down
  37.                     else if (a[i][j] >= '0' && a[i][j] <= '9') // whoo we got a number
  38.                     {
  39.                         if (maxFound)
  40.                         {
  41.                             x = a[i][j] - '0';
  42.                             while (a[i][++j] >= '0' && a[i][j] <= '9')
  43.                 x = 10 * x + a[i][j] - '0'; // gotta deal with reading large numbers one digit at a time
  44.                             if (x * Power2(lvl) != total)
  45.                             {
  46.                                 valid = false;
  47.                                 break;
  48.                             }
  49.                             j--;
  50.                         }
  51.                         else
  52.                         {
  53.                             total = a[i][j] - '0';
  54.                             while (a[i][++j] >= '0' && a[i][j] <= '9')
  55.                 total = 10 * total + a[i][j] - '0';
  56.                             total *= Power2(lvl);
  57.                             maxFound = true;
  58.                             j--;
  59.                         }
  60.                     }
  61.                 }
  62.  
  63.                 Console.WriteLine(((valid && ( total % Power2(maxlvl) == 0)) ? "YES" : "NO"));
  64.  
  65.             }
  66.         }
  67.  
  68.     // A cool way of finding 2^n
  69.         static int Power2(int n)
  70.         {
  71.             return 1 << n;
  72.         }
  73.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement