Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* http://gosucoder.com/problem/P3 */
- /* We can divide the sculpture into layers (levels). Then we'll use following facts:
- * 1. On each level, all weights must be equal
- * 2. Weights on the next level must be half the weight of the ones layer down
- * Therefore:
- * 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.
- * 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.
- */
- using System;
- class Test
- {
- static void Main()
- {
- int n = int.Parse(Console.ReadLine());
- string[] a = new string[n];
- for (int i = 0; i < n; i++)
- a[i] = Console.ReadLine();
- for (int i = 0; i < n; i++)
- {
- int lvl, j, total = 0, x, maxlvl = 0;
- bool maxFound, valid;
- lvl = 0;
- maxFound = false;
- valid = true;
- for (j = 1; j < a[i].Length; j++)
- {
- if (a[i][j] == '[') // lvl up
- {
- if (++lvl > maxlvl)
- maxlvl++;
- }
- else if (a[i][j] == ']') lvl--; // lvl down
- else if (a[i][j] >= '0' && a[i][j] <= '9') // whoo we got a number
- {
- if (maxFound)
- {
- x = a[i][j] - '0';
- while (a[i][++j] >= '0' && a[i][j] <= '9')
- x = 10 * x + a[i][j] - '0'; // gotta deal with reading large numbers one digit at a time
- if (x * Power2(lvl) != total)
- {
- valid = false;
- break;
- }
- j--;
- }
- else
- {
- total = a[i][j] - '0';
- while (a[i][++j] >= '0' && a[i][j] <= '9')
- total = 10 * total + a[i][j] - '0';
- total *= Power2(lvl);
- maxFound = true;
- j--;
- }
- }
- }
- Console.WriteLine(((valid && ( total % Power2(maxlvl) == 0)) ? "YES" : "NO"));
- }
- }
- // A cool way of finding 2^n
- static int Power2(int n)
- {
- return 1 << n;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement