Exemplo n.º 1
0
  void solve(InputReader in) {

    try {

      long start = System.currentTimeMillis();

      F[0] = BigInteger.ONE;
      for (i = 1; i <= 30; ++i) F[i] = F[i - 1].multiply(BigInteger.valueOf(i));

      for (i = 1; i <= 30; ++i) {
        for (j = 1; j <= i; ++j) {
          C[i][j] = getResult(i, j);
        }
      }

      t = in.nextInt();
      while (t-- > 0) {
        for (i = 1; i <= 30; ++i) g[i] = new ArrayList();
        n = in.nextInt();
        for (i = 1; i <= n; ++i) a[i] = in.nextInt();
        root = a[1];
        p = 2;
        Construct(root, 1, 30);
        Pair ans = Calculate(root);
        System.out.println(ans.x);
      }

      long end = System.currentTimeMillis();
      if (time) System.out.println("Execution time: " + (double) (end - start) / 1000.0);
    } catch (Exception e) {
      System.out.println("Error: " + e);
    }
    ;
  }
Exemplo n.º 2
0
 public static void main(String args[]) {
   long pow = 1;
   for (int i = 0; i < tenPow.length; i++) {
     tenPow[i] = pow;
     pow = (pow * 10) % MOD;
   }
   String S = in.next();
   int N = in.nextInt();
   nodes = new Node[10];
   Node root = new Node(-1);
   for (int i = 0; i < S.length(); i++) {
     root.children.add(get((int) S.charAt(i) - '0'));
   }
   for (int i = 0; i < N; i++) {
     String Q = in.next();
     int d = (int) Q.charAt(0) - '0';
     if (nodes[d] != null) {
       Node nd = nodes[d];
       nodes[d] = null;
       for (int j = 3; j < Q.length(); j++) {
         nd.children.add(get((int) Q.charAt(j) - '0'));
       }
       if (Q.length() == 3) {
         nd.children.add(Node.EMPTY_LEAF);
       }
     }
   }
   out.println(root.ans());
 }
Exemplo n.º 3
0
  public static void main(String[] args) {
    InputReader in = new InputReader(System.in);
    OutputWriter out = new OutputWriter(System.out);
    int tc = in.nextInt();
    while (tc-- > 0) {
      String a = in.nextString();
      String b = in.nextString();
      int[][] dp = new int[2][b.length() + 1];
      dp[1][0] = 1;
      for (int i = 0; i <= b.length(); i++) {
        dp[0][i] = i;
      }

      for (int i = 1, r = a.length(); i <= r; i++) {
        dp[i % 2][0] = i;
        for (int j = 1, c = b.length(); j <= c; j++) {
          if (a.charAt(i - 1) == b.charAt(j - 1)) {
            dp[i % 2][j] = dp[(i - 1) % 2][j - 1];
          } else {
            dp[i % 2][j] = 1 + min(dp[(i - 1) % 2][j], dp[(i - 1) % 2][j - 1], dp[i % 2][j - 1]);
          }
        }
      }

      out.println(dp[a.length() % 2][b.length()]);
    }

    out.flush();
    out.close();
  }
Exemplo n.º 4
0
  public static void main(String[] args) {
    InputReader inputReader = new InputReader(System.in);
    PrintWriter printWriter = new PrintWriter(System.out, true);

    int n = inputReader.nextInt();
    int m = inputReader.nextInt();

    //        int n = inputReader.nextInt();
    //        String colors = inputReader.next();
    //        int[] nums = new int[3];
    //        boolean[] res = new boolean[3];
    //
    //        int i = 0;
    //        while (nums[0] < 2 && nums[1] < 2 && nums[2] < 2 && i < n) {
    //            switch (colors.charAt(i)) {
    //                case 'R':
    //                    nums[0]++;
    //                    break;
    //                case 'G':
    //                    nums[1]++;
    //                    break;
    //                case 'B':
    //                    nums[2]++;
    //                    break;
    //            }
    //            i++;
    //        }
    //
    //        for (int j = 0; j < 3; j++) {
    //            if (nums[i] >= 1)
    //        }

    //        if ((nums[0] >= 1 && nums[1] >= 1 && nums[2] >= 1) ||
    //                (nums[0] == 2 && nums[1] == 2) ||
    //                (nums[1] == 2 && nums[2] == 2) ||
    //                (nums[0] == 2 && nums[2] == 2)) {
    //            printWriter.print("BGR");
    //            printWriter.close();
    //            return;
    //        } else if (nums[0] == 0 && nums[1] == 0 && nums[2] >= 1) {
    //
    //        } else {
    //            if (nums[0] == 0 && nums[1] == 0 && nums[2] >= 1) {
    //                res[2] = true;
    //            }
    //            if (nums[0] == 0 && nums[1] >= 1 && nums[2] == 0) {
    //                res[1] = true;
    //            }
    //            if (nums[0] >= 1 && nums[1] == 0 && nums[2] == 0) {
    //                res[0] = true;
    //            }
    //        }

    //        if (res[2]) printWriter.print('B');
    //        if (res[1]) printWriter.print('G');
    //        if (res[0]) printWriter.print('R');

    printWriter.close();
  }
  public static void main(String[] args) {
    InputReader in = new InputReader(System.in);
    OutputWriter out = new OutputWriter(System.out);
    Solver solver = new Solver(in, out);

    solver.solve();
    in.close();
    out.flush();
    out.close();
  }
Exemplo n.º 6
0
    public static void main(String[] argv) throws IOException
    {
        Task t;
        boolean home = argv.length > 0 && argv[0].equals("test");
        InputStream inputStream;
        OutputStream outputStream;
        if (home) {
            inputStream = new FileInputStream(Task.filename + ".in");
            outputStream = new FileOutputStream(Task.filename + ".out");
        } else {
            switch (Task.read) {
                case 0:
                    inputStream = new FileInputStream(Task.filename + ".in");
                    outputStream = new FileOutputStream(Task.filename + ".out");
                    break;
                case 1:
                    inputStream = new FileInputStream("input.txt");
                    outputStream = new FileOutputStream("output.txt");
                    break;
                default:
                    inputStream = System.in;
                    outputStream = System.out;
                    break;

            }

        }
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream, home);


        if (home)
            do {
                long time = System.currentTimeMillis();
                t = new Task();
                t.in = in;
                t.out = out;
                t.run();

                out.writeln();
                out.writeln("=====Time:" + (System.currentTimeMillis() - time));
                out.flush();
            } while (in.toNextTest());
        else {
            t = new Task();
            t.in = in;
            t.out = out;
            t.run();
        }

        out.close();

    }
Exemplo n.º 7
0
  public static void main(String[] args) {
    InputReader in = new InputReader(System.in);
    OutputWriter out = new OutputWriter(System.out);
    int tc = in.nextInt();
    while (tc-- > 0) {
      int n = in.nextInt();
      out.println(countTri(n));
    }

    out.flush();
    out.close();
  }
Exemplo n.º 8
0
 void solve() {
   int universitiesCount = in.readInt();
   int maxGrants = in.readInt();
   int grantsCount = in.readInt();
   double[] profit = new double[grantsCount + 1];
   for (int i = 0; i < universitiesCount; i++) {
     double[] currentProfit = new double[maxGrants + 1];
     for (int j = 0; j <= maxGrants; j++) currentProfit[j] = in.readDouble();
     for (int j = grantsCount; j >= 0; j--) {
       for (int k = 0; k <= maxGrants && k <= j; k++)
         profit[j] = Math.max(profit[j], profit[j - k] + currentProfit[k]);
     }
   }
   out.printf("%.2f\n", profit[grantsCount]);
 }
Exemplo n.º 9
0
  public void solve(InputReader in, PrintWriter out) throws IOException {

    int tests = in.nextInt();

    for (int i = 0; i < tests; i++) {
      n = in.nextInt();
      D = new int[n];

      for (int j = 0; j < n; j++) {
        D[j] = in.nextInt();
      }

      long minGrass = getMinimumGrass();
      out.println(minGrass);
    }
  }
Exemplo n.º 10
0
 public static void readIntArrays(InputReader in, int[]... arrays) {
   for (int i = 0; i < arrays[0].length; i++) {
     for (int j = 0; j < arrays.length; j++) {
       arrays[j][i] = in.nextInt();
     }
   }
 }
Exemplo n.º 11
0
 public static void readIntMatrix(InputReader in, int[][] matrix) {
   for (int i = 0; i < matrix.length; i++) {
     for (int j = 0; j < matrix[i].length; j++) {
       matrix[i][j] = in.nextInt();
     }
   }
 }
Exemplo n.º 12
0
 public static void readCharMatrix(InputReader in, char[][] matrix) {
   for (int i = 0; i < matrix.length; i++) {
     String row = in.next();
     for (int j = 0; j < matrix[i].length; j++) {
       matrix[i][j] = row.charAt(j);
     }
   }
 }
Exemplo n.º 13
0
  public static void main(String[] args) {
    InputReader in = new InputReader(System.in);
    OutputWriter out = new OutputWriter(System.out);
    while (true) {
      int n = in.nextInt();
      if (n == 0) break;
      int c = 0;
      while (n > 1) {
        n /= 2;
        c++;
      }

      out.println(c);
    }
    out.flush();
    out.close();
  }
Exemplo n.º 14
0
 public void solve(int testNumber, InputReader in, OutputWriter out) {
   int n = in.nextInt(), m = in.nextInt();
   boolean[] tab = new boolean[255 * 255 * 2];
   for (int p = 0; p <= m; ++p) {
     for (int q = 0; q <= p; ++q) {
       tab[p * p + q * q] = true;
     }
   }
   List<Integer> lst = new ArrayList<Integer>();
   for (int i = 0; i < tab.length; ++i) {
     if (tab[i]) {
       lst.add(i);
     }
   }
   boolean found = false;
   int b = 1;
   while (true) {
     if ((n - 1) * b > m * m * 2) {
       break;
     }
     for (int idx = 0; idx + n <= lst.size(); ++idx) {
       int a = lst.get(idx);
       if (a + (n - 1) * b > m * m * 2) {
         break;
       }
       // out.println(a, b);
       boolean flag = true;
       for (int i = n - 1; i >= 0; --i) {
         int x = a + i * b;
         if (!tab[x]) {
           flag = false;
           break;
         }
       }
       if (flag) {
         out.println(a, b);
         found = true;
       }
     }
     b++;
   }
   if (!found) {
     out.println("NONE");
   }
 }
Exemplo n.º 15
0
 public static void main(String args[]) {
   int N = in.nextInt();
   int[] keys = new int[26];
   String S = in.next();
   int tot = 0;
   // out.println(N + " " + S);
   for (int i = 0; i < N - 1; i++) {
     // out.println(S + " " + i);
     int key = (int) S.charAt(i * 2) - 'a';
     keys[key]++;
     int door = (int) S.charAt(i * 2 + 1) - 'A';
     if (keys[door] > 0) {
       keys[door]--;
     } else tot++;
   }
   out.println(tot);
   out.close();
 }
    void solve(int testNumber) {
      n = in.nextInt();
      q = in.nextInt();

      tree = new Node[4 * n];

      buildTree(1, 0, n - 1);

      for (int i = 0; i < q; i++) {
        int type, from, to;

        type = in.nextInt();
        from = in.nextInt();
        to = in.nextInt();

        if (type == 0) update(1, 0, n - 1, from, to);
        else out.println(query(1, 0, n - 1, from, to));
      }
    }
Exemplo n.º 17
0
	void solve() {
		int n = in.readInt();
		int[] x = new int[2 * n];
		final int[] y = new int[2 * n];
		for (int i = 0; i < n; i++) {
			x[2 * i] = in.readInt();
			x[2 * i + 1] = in.readInt();
			y[2 * i] = y[2 * i + 1] = in.readInt();
		}
		Integer[] o = new Integer[2 * n];
		for (int i = 0; i < 2 * n; i++)
			o[i] = i;
		Arrays.sort(o, new Comparator<Integer>() {
			public int compare(Integer o1, Integer o2) {
				return y[o1] - y[o2];
			}
		});
		int[][] result = new int[2 * n][2 * n];
		for (int[] row : result)
			Arrays.fill(row, Integer.MIN_VALUE);
		result[o[0]][o[0]] = 0;
		result[o[1]][o[1]] = 0;
		for (int ii = 2; ii < 2 * n; ii++) {
			int i = o[ii];
			int maxIndex = ii - ii % 2;
			for (int jj = 0; jj < maxIndex; jj++) {
				for (int kk = 0; kk < maxIndex; kk++) {
					int j = o[jj];
					int k = o[kk];
					if (valid(k, j, i, x, y)) {
						result[i][j] = Math.max(result[i][j], result[k][j] + triangleSquare(i, j, k, x, y));
						result[k][i] = Math.max(result[k][i], result[k][j] + triangleSquare(i, j, k, x, y));
					}
				}
			}
		}
		int answer = 0;
		for (int i = 0; i < 2 * n; i++) {
			for (int j = 0; j < 2 * n; j++)
				answer = Math.max(answer, result[i][j]);
		}
		out.printf("%.1f\n", answer * .5);
	}
Exemplo n.º 18
0
 public void run() {
   try {
     int n = reader.nextInt();
     Point[] points = new Point[n];
     for (int i = 0; i < n; ++i) {
       int x = reader.nextInt();
       int y = reader.nextInt();
       points[i] = new Point(x, y);
     }
     double answer = solve(points);
     for (int i = 0; i < n; ++i) {
       points[i] = points[i].flip();
     }
     answer += solve(points);
     writer.println(String.format("%.8f", answer));
   } catch (IOException ex) {
   }
   writer.close();
 }
  public static void main(String[] args) {
    in = new InputReader(System.in);
    out = new OutputWriter(System.out);

    solve();

    out.flush();

    in.close();
    out.close();
  }
    void solve() {
      n = in.nextInt();
      k = in.nextInt();
      bit = new int[n + 1];

      for (int i = 0; i < k; i++) {
        int a, b;

        a = in.nextInt();
        b = in.nextInt();
        add(a, 1);
        add(b + 1, -1);
      }

      int[] ans = new int[n];

      for (int i = 1; i <= n; i++) ans[i - 1] = query(i);

      Arrays.sort(ans);
      out.println(ans[n / 2]);
    }
Exemplo n.º 21
0
  public void solve(InputReader in, PrintWriter out) throws IOException {

    int tests = in.nextInt();

    for (int i = 0; i < tests; i++) {
      N = in.nextInt();
      K = in.nextInt();

      customers = new Customer[N];

      for (int j = 0; j < N; j++) {
        int s = in.nextInt();
        int f = in.nextInt();
        int p = in.nextInt();
        customers[j] = new Customer(s, f, p);
      }

      int customersServed = getServedCustomers();
      out.println(customersServed);
    }
  }
Exemplo n.º 22
0
 void solve() {
   String equip = in.readString();
   String[] equipItems = equip.split(",");
   Map<String, Integer> preparation = new HashMap<String, Integer>();
   for (String item : equipItems) {
     int semicolonIndex = item.indexOf(':');
     String name = item.substring(0, semicolonIndex);
     int preparationTime = Integer.parseInt(item.substring(semicolonIndex + 1));
     preparation.put(name, preparationTime);
   }
   String[] sets = in.readString().split("[|]");
   int minTime = Integer.MAX_VALUE;
   for (String set : sets) {
     String[] items = set.split("[&]");
     int setPreparationTime = 0;
     for (String item : items)
       setPreparationTime = Math.max(setPreparationTime, preparation.get(item));
     minTime = Math.min(minTime, setPreparationTime);
   }
   out.println(minTime);
 }
Exemplo n.º 23
0
  void run() {
    InputReader in = new InputReader(System.in);
    PrintWriter out = new PrintWriter(System.out);

    int n = in.nextInt(), q = in.nextInt();
    int[] v = new int[n], c = new int[n];
    for (int i = 0; i < n; ++i) v[i] = in.nextInt();
    for (int i = 0; i < n; ++i) c[i] = in.nextInt();

    for (int i = 0; i < q; ++i) {
      int a = in.nextInt(), b = in.nextInt();
      long[] dp = new long[n + 1];
      Node[] heap = new Node[2];
      Arrays.fill(dp, -INF);
      for (int j = 0; j < 2; ++j) heap[j] = new Node(-INF, -1);
      for (int j = 0; j < n; ++j) {
        long val = v[j], maxv = val * b;
        int color = c[j];
        maxv = Math.max(dp[color] + val * a, maxv);
        maxv = Math.max(choose(heap, color) + val * b, maxv);
        dp[color] = Math.max(maxv, dp[color]);
        update(heap, dp[color], color);
      }

      long ret = 0;
      for (int j = 1; j <= n; ++j) ret = Math.max(ret, dp[j]);
      out.println(ret);
    }

    out.close();
  }
    void solve() {
      n = in.nextInt();
      m = in.nextInt();
      days = new int[n + 1];
      req = new int[m + 1];

      for (int i = 1; i <= n; i++) days[i] = in.nextInt();

      for (int i = 1; i <= m; i++) req[i] = in.nextInt();

      if (!isPossible(n)) {
        out.println(-1);

        return;
      }

      int low, high, mid;

      low = 1;
      high = n;
      mid = 1;

      while (low <= high) {
        mid = low + high >> 1;

        boolean poss = isPossible(mid);

        if (poss) {
          boolean prev = isPossible(mid - 1);

          if (prev) high = mid - 1;
          else break;
        } else low = mid + 1;
      }

      out.println(mid);
    }
    void solve(int testNumber) {
      t = in.nextInt();

      while (t-- > 0) {
        n = in.nextInt();
        Stack<Integer> stack = new Stack<>();

        for (int i = 0; i < n; i++) stack.add(in.nextInt());

        stack.sort(
            new Comparator<Integer>() {
              @Override
              public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1, o2);
              }
            });

        int total = 0;

        while (true) {
          int size = stack.size();

          if (size == 0) break;
          else if (size == 1) total += stack.pop();
          else if (size == 2) total += stack.pop() + stack.pop();
          else if (size == 3) total += stack.pop() + stack.pop() + stack.pop() * 0;
          else {
            total += stack.pop() + stack.pop();
            stack.pop();
            stack.pop();
          }
        }

        out.println(total);
      }
    }
Exemplo n.º 26
0
  public static void main(String[] args) {
    InputReader inputReader = new InputReader(System.in);
    PrintWriter printWriter = new PrintWriter(System.out, true);

    int n = inputReader.nextInt();
    int m = inputReader.nextInt();
    int[] a = inputReader.nextIntArray(n);
    int[] b = inputReader.nextIntArray(m);

    mergeSort(a, 0, n);

    for (int i : b) {
      int low = 0;
      int high = n - 1;
      while (low < high) {
        int mid = (low + high + 1) / 2;
        if (a[mid] <= i) low = mid;
        else high = mid - 1;
      }
      if (a[low] > i) low--;
      printWriter.print((low + 1) + " ");
    }
    printWriter.close();
  }
    void solve(int testNumber) {
      n = in.nextInt();
      m = in.nextInt();
      c = in.nextInt();
      bit = new long[n + 1];

      for (int i = 0; i < m; i++) {
        char[] chars = in.next().toCharArray();

        if (chars[0] == 'S') {
          int left, right, k;

          left = in.nextInt();
          right = in.nextInt();
          k = in.nextInt();

          add(left, k);
          add(right + 1, -k);
        } else out.println(c + query(in.nextInt()));
      }
    }
    void solve(int testNumber) {
      n = in.nextInt();
      gain = new int[n];
      lose = new int[n];

      for (int i = 0; i < n; i++) gain[i] = in.nextInt();

      for (int i = 0; i < n; i++) lose[i] = in.nextInt();

      int max = 0;
      int maxStartingPos = -1;
      boolean isInfinity = false;
      int infinityFrom = -1;
      int crossedN = 0;

      outer:
      for (int start = 0; start < n; ) {
        int curr = gain[start];
        int startingPoint = start;
        int currLen = 1;
        System.out.println(
            "start : "
                + start
                + ", currLen : "
                + currLen
                + ", currMaxLen : "
                + max
                + ", sP : "
                + startingPoint);

        while (start < n && curr >= lose[start]) {
          curr -= lose[start];
          start++;
          currLen++;

          if (start == startingPoint) {
            isInfinity = true;
            infinityFrom = start;

            break outer;
          }

          if (start == n) {
            start = 0;
            crossedN++;
          }

          curr += gain[start];
        }

        if (currLen > max) {
          max = currLen;
          maxStartingPos = startingPoint;
        }

        start++;

        if (start >= n) {
          crossedN++;

          if (crossedN >= 2) break;
        }
      }

      if (isInfinity) out.println("Infinity, from position " + infinityFrom);
      else out.println("Max dist from position " + maxStartingPos);
    }
  static void solve() {
    n = in.nextInt();

    arr = new int[1001][1001];

    for (int i = 0; i < n; i++) {
      int x, y;

      x = in.nextInt() - 1;
      y = in.nextInt() - 1;

      arr[x][y]++;
    }

    int i, j;
    int start = 0;
    long left, right, answer;

    left = right = answer = 0;

    while (start < 1000) {
      i = 0;
      j = start;
      left = 0;

      while (i < 1000 && j < 1000) {
        if (arr[i][j] > 0) left++;

        i++;
        j++;
      }

      answer += (left * (left - 1) / 2);
      start++;
    }

    start = 1;

    while (start < 1000) {
      i = start;
      j = 0;
      left = 0;

      while (i < 1000 && j < 1000) {
        if (arr[i][j] > 0) left++;

        i++;
        j++;
      }

      answer += (left * (left - 1) / 2);
      start++;
    }

    start = 999;

    while (start >= 0) {
      i = start;
      j = 0;
      right = 0;

      while (i >= 0 && j < 1000) {
        if (arr[i][j] > 0) right++;

        i--;
        j++;
      }

      answer += (right * (right - 1) / 2);
      start--;
    }

    start = 1;

    while (start < 1000) {
      i = 999;
      j = start;
      right = 0;

      while (i >= 0 && j < 1000) {
        if (arr[i][j] > 0) right++;

        i--;
        j++;
      }

      answer += (right * (right - 1) / 2);
      start++;
    }

    out.println(answer);
  }