public void solve(final int testNumber, final InputReader in, final PrintWriter out) { int counter = in.nextInt(); double[] max = in.nextDoubleArray(counter); double[] min = in.nextDoubleArray(counter); double[] smin = new double[counter + 1]; double[] smax = new double[counter + 1]; for (int i = 1; i <= counter; ++i) { smin[i] = smin[i - 1] + min[i - 1]; smax[i] = smax[i - 1] + max[i - 1]; } double[] p = new double[counter + 1]; p[0] = 0; for (int i = 1; i <= counter; ++i) { double B = (smin[i] + smax[i]); double C = smax[i]; double D = B * B - 4 * C; p[i] = (B + Math.sqrt(D)) / 2.0; } for (int i = counter - 1; i > 0; --i) { p[i + 1] -= p[i]; if (p[i + 1] < 1e-9) p[i + 1] = 0; } for (int i = 1; i <= counter; ++i) { out.print(p[i] + " "); } out.println(); for (int i = 1; i <= counter; ++i) { out.print((min[i - 1] + max[i - 1] - p[i]) + " "); } }
public void solve(int testNumber, InputReader in, PrintWriter out) { int counter = in.nextInt(); int maxm = 0; int mw = 0; int maxw = 0; int wm = 0; for (int i = 0; i < counter; ++i) { int M = in.nextInt(); int W = in.nextInt(); int S = in.nextInt(); int m = Math.min(M, S); int w = Math.min(W, Math.max(0, S - m)); maxm += m; mw += w; w = Math.min(W, S); m = Math.min(M, Math.max(0, S - w)); maxw += w; wm += m; } if (maxm <= mw) { out.println(maxm); } else if (maxw <= wm) { out.println(maxw); } else { out.println(Math.min((maxm + mw) / 2, (maxw + wm) / 2)); } }
public void solve(int testNumber, InputReader in, PrintWriter out) { int blocks = in.nextInt(); Block[] list = new Block[blocks]; for (int i = 0; i < blocks; ++i) { list[i] = new Block(in.next(), i); } Arrays.sort(list); int sum = 0; for (int i = 0; i < blocks; ++i) { sum += list[i].balance; if (sum < 0) { out.println("NO"); return; } } if (sum != 0) { out.println("NO"); } else { out.println("YES"); for (Block block : list) { out.print(block.idx + 1); out.print(' '); } out.println(); } }
public void solve(int testNumber, InputReader in, PrintWriter out) { int length = in.nextInt(); int alphabetSize = in.nextInt(); int[] string = new int[length + 1]; for (int i = 0; i < length; ++i) { string[i] = in.nextInt(); } int[] suffixArray = StringUtils.buildSuffixArray(string); int[] lcp = StringUtils.buildLCP(string, suffixArray); int[] stack = new int[lcp.length]; int top = 0; int bestPosition = -1; int bestLength = -1; for (int i = 0; i < lcp.length; ++i) { while (top > 0 && lcp[stack[top - 1]] >= lcp[i]) { int current = stack[top - 1]; int previous = top == 1 ? -1 : stack[top - 2]; int currentLength = i - previous; if (bestPosition == -1 || (long) lcp[bestPosition] * bestLength < (long) lcp[current] * currentLength) { bestPosition = current; bestLength = currentLength; } top--; } stack[top++] = i; } if ((long) lcp[bestPosition] * bestLength < length) { out.println(length); out.println(length); for (int i = 0; i < length; ++i) { if (i > 0) out.print(' '); out.print(string[i]); } } else { out.println((long) lcp[bestPosition] * bestLength); out.println(lcp[bestPosition]); for (int i = 0; i < lcp[bestPosition]; ++i) { if (i > 0) out.print(' '); out.print(string[suffixArray[bestPosition] + i]); } } }
public void solve(final int testNumber, final InputReader in, final PrintWriter out) { long a = in.nextLong() % MODULO; long b = in.nextLong() % MODULO; int n = in.nextInt(); // long a = 1_000_000_000_000_000_000L % MODULO; // long b = 1_000_000_000_010_000_000L % MODULO; // int n = 10_000_000; MagicQueue queue = new MagicQueue(n); for (int i = 0; i < n; ++i) { queue.push((int) ((a + i) % MODULO)); } long answer = queue.getProduct(); for (int cur = (int) a; cur != b; cur = (cur + 1) % MODULO) { queue.pop(); queue.push(cur + n); answer += queue.getProduct(); } out.println(answer % MODULO); }
public void solve(int testNumber, InputReader in, PrintWriter out) { int initial = in.nextInt(); int min, max; min = max = initial; for (int i = 0; i < Integer.toString(initial).length(); ++i) { for (int j = 0; j < i; ++j) { assert initial >= powers[i]; int a = initial / powers[i] % 10; int b = initial / powers[j] % 10; if (i == Integer.toString(initial).length() - 1 && b == 0) continue; int tmp = initial + (b - a) * powers[i] + (a - b) * powers[j]; min = Math.min(min, tmp); max = Math.max(max, tmp); } } out.printf("Case #%d: %d %d\n", testNumber, min, max); }