Codeforces Round #397 E. Tree Folding
問題
ツリーがあって、ある頂点から同じ長さの枝が2本出ている時、そのうち1本を取り除くことが出来ます。この操作を繰り返してツリーを直線に出来る場合、その最小の長さを出力してください。
解法
葉から登っていき、自分より下方向にある枝の種類を保存しながら登っていく。自分より下の枝が3種類以上ある場合は即アウト。
コード
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.NoSuchElementException; import java.util.TreeSet; public class E2 { private ArrayList<Integer>[] tree; //branches[i] := iにぶら下がっている枝の数 private TreeSet<Integer>[] branches; private int[] reached; private boolean[] used; private int solve() { int N = tree.length; ArrayDeque<Integer> deque = new ArrayDeque<>(); //葉をキューに詰める for (int i = 0; i < N; i++) { if (tree[i].size() == 1) { deque.add(i); } } while (!deque.isEmpty()) { int v = deque.poll(); used[v] = true; //vに3種類以上の長さの枝がぶら下がっている場合どうやっても不可能なので終了する if (branches[v].size() >= 3) { return -1; } //v以外の枝のマージが全て終わったとき、ツリーは直線になっているはずである if (reached[v] == tree[v].size()) { int ans = 0; for (int branch : branches[v]) { ans += branch; } //半分に折れる限り折る while (ans % 2 == 0) { ans /= 2; } return ans; } if (branches[v].size() == 2) { return -1; } int length = branches[v].isEmpty() ? 1 : branches[v].first() + 1; for (int parent : tree[v]) { if (used[parent]) { continue; } reached[parent]++; branches[parent].add(length); //parentより下の枝のマージが終わったらキューに詰める if (reached[parent] + 1 == tree[parent].size()) { deque.add(parent); } } } throw new IllegalStateException(); } private void solve(FastScanner in, PrintWriter out) { int N = in.nextInt(); tree = new ArrayList[N]; branches = new TreeSet[N]; reached = new int[N]; used = new boolean[N]; for (int i = 0; i < N; i++) { tree[i] = new ArrayList<>(); branches[i] = new TreeSet<>(); } for (int i = 0; i < N - 1; i++) { int u = in.nextInt() - 1; int v = in.nextInt() - 1; tree[v].add(u); tree[u].add(v); } out.println(solve()); } public static void main(String[] args) { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); new E2().solve(in, out); out.close(); } private static class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int bufferLength = 0; private boolean hasNextByte() { if (ptr < bufferLength) { return true; } else { ptr = 0; try { bufferLength = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (bufferLength <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) { return buffer[ptr++]; } else { return -1; } } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } private void skipUnprintable() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) { ptr++; } } boolean hasNext() { skipUnprintable(); return hasNextByte(); } public String next() { if (!hasNext()) { throw new NoSuchElementException(); } StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } long nextLong() { if (!hasNext()) { throw new NoSuchElementException(); } long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } double nextDouble() { return Double.parseDouble(next()); } double[] nextDoubleArray(int n) { double[] array = new double[n]; for (int i = 0; i < n; i++) { array[i] = nextDouble(); } return array; } double[][] nextDoubleMap(int n, int m) { double[][] map = new double[n][]; for (int i = 0; i < n; i++) { map[i] = nextDoubleArray(m); } return map; } public int nextInt() { return (int) nextLong(); } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = nextInt(); } return array; } public long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) { array[i] = nextLong(); } return array; } public String[] nextStringArray(int n) { String[] array = new String[n]; for (int i = 0; i < n; i++) { array[i] = next(); } return array; } public char[][] nextCharMap(int n) { char[][] array = new char[n][]; for (int i = 0; i < n; i++) { array[i] = next().toCharArray(); } return array; } public int[][] nextIntMap(int n, int m) { int[][] map = new int[n][]; for (int i = 0; i < n; i++) { map[i] = nextIntArray(m); } return map; } } }
AtCoder Regular Contest E - Snuke Line
解法
とても重要な事実として、M以下のxの倍数を、x=1..Mについて列挙すると、その数はM logMくらいになります。
自然数xのM以下の倍数の数はM/x個なので、これをx=1..Mについて全て足し合わせると、以下のようないい加減な見積もりにより求まります。
よって、各dについてdの倍数を全て列挙しても大丈夫そうです。
dの時の答えを求めるには、各区間の累積和を作っておいて、dのとき、2dのとき、3dのとき、...と足し合わせていきたいところですが、kdのときに数えた区間を、(k+1)dのときにも数えてしまわないようにしたいです。
kdのときに数えた区間を、(k+1)dのときにも数えてしまうような区間は、長さがdより大きい区間ですが、そのような区間は数えるまでもなく必ず通るはずなので、dの時に数える区間は長さがd以下の区間に限れば良いです。
コード
import java.io.PrintWriter; import java.util.ArrayList; import java.util.Scanner; @SuppressWarnings("unchecked") public class Main { private void solve(Scanner in, PrintWriter out) { int N = in.nextInt(); int M = in.nextInt(); FenwickTree bit = new FenwickTree(M + 2); int[] l = new int[N]; int[] r = new int[N]; ArrayList<Integer>[] buckets = new ArrayList[M + 1]; for (int i = 1; i <= M; i++) { buckets[i] = new ArrayList<>(); } for (int i = 0; i < N; i++) { l[i] = in.nextInt(); r[i] = in.nextInt(); int w = r[i] - l[i] + 1; buckets[w].add(i); } int gone = 0; for (int d = 1; d <= M; d++) { for (int i : buckets[d]) { gone++; bit.add(l[i], 1); bit.add(r[i] + 1, -1); } int ans = N - gone; for (int m = d; m <= M; m += d) { ans += bit.sum(m + 1); } out.println(ans); } } public class FenwickTree { int N; long[] data; FenwickTree(int N) { this.N = N + 1; data = new long[N + 1]; } void add(int k, long val) { for (int x = k; x < N; x |= x + 1) { data[x] += val; } } // [0, k) long sum(int k) { if (k >= N) { k = N - 1; } long ret = 0; for (int x = k - 1; x >= 0; x = (x & (x + 1)) - 1) { ret += data[x]; } return ret; } } public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); new Main().solve(in, out); in.close(); out.close(); } }
log4j2.xmlの置き場所
Gradle環境ではlog4j2の設定ファイルlog4j2.xmlはsrc/main/resourcesに置くっぽい。
8VC Venture Cup 2017 - Elimination Round F. PolandBall and Gifts
問題
http://codeforces.com/contest/755/problem/F
プレゼント交換をします。iさんはp[i]さんにプレゼントを渡します。複数の人が同じ人にプレゼントを渡すことはありません。
K人がプレゼントを忘れました。プレゼントを忘れた人はプレゼントを受け取れません。また、プレゼントを忘れられた人も当然プレゼントを受け取れません。
プレゼントを受け取れない人数の最大値と最小値を求めてください。
解法
以下のような5人のサイクルを考えます。
A->B->C->D->E->A
このとき、Aがプレゼントを忘れると、AとB両方がプレゼントを受け取ることができなくなります。
2人忘れたとき、AとBが忘れたパターンと、AとCが忘れたパターンで、最終的な受け取れない人数が変わります。このように考えると最大値は簡単に求まります。
最小値は、サイクルの長さの集合 {L_i} からいくつか選んでKが作れるかを判定する部分和問題に帰着できます。(作れれば最小値はK、そうでなければK+1)
touristさんのコードをみるとこのナップザック問題をbitsetで高速に解いていて面白かったので真似しました。
コード
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.BitSet; import java.util.NoSuchElementException; public class F { /** * @param cnt cnt[l] := 大きさlのものの個数 * @param K 作りたい大きさ * @return Kが作れるかどうか */ private boolean knapsack(int[] cnt, int K) { int N = cnt.length - 1; BitSet bitSet = new BitSet(); bitSet.set(N); for (int i = 1; i <= N; i++) { if (cnt[i] == 0) continue; int j = 1; while (cnt[i] > 0) { j = Math.min(j, cnt[i]); int u = i * j; bitSet.or(bitSet.get(u, bitSet.length())); cnt[i] -= j; j *= 2; } } return bitSet.get(N - K); } private void solve(FastScanner in, PrintWriter out) { int N = in.nextInt(); int K = in.nextInt(); boolean[] vis = new boolean[N]; int[] p = new int[N]; for (int i = 0; i < N; i++) { p[i] = in.nextInt() - 1; } int[] cnt = new int[N + 1]; int solo = 0, pair = 0; for (int i = 0; i < N; i++) { if (vis[i]) continue; int cur = i; int length = 0; while (!vis[cur]) { vis[cur] = true; length++; cur = p[cur]; } cnt[length]++; solo += length % 2; pair += length / 2; } int min = K; if (!knapsack(cnt, K)) { min++; } int max = 0; max += 2 * Math.min(K, pair); K -= Math.min(K, pair); max += Math.min(K, solo); out.println(min + " " + max); } public static void main(String[] args) { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); new F().solve(in, out); out.close(); } private static class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int bufferLength = 0; private boolean hasNextByte() { if (ptr < bufferLength) { return true; } else { ptr = 0; try { bufferLength = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (bufferLength <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } private void skipUnprintable() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; } boolean hasNext() { skipUnprintable(); return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } double nextDouble() { return Double.parseDouble(next()); } double[] nextDoubleArray(int n) { double[] array = new double[n]; for (int i = 0; i < n; i++) { array[i] = nextDouble(); } return array; } double[][] nextDoubleMap(int n, int m) { double[][] map = new double[n][]; for (int i = 0; i < n; i++) { map[i] = nextDoubleArray(m); } return map; } public int nextInt() { return (int) nextLong(); } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) array[i] = nextInt(); return array; } public long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) array[i] = nextLong(); return array; } public String[] nextStringArray(int n) { String[] array = new String[n]; for (int i = 0; i < n; i++) array[i] = next(); return array; } public char[][] nextCharMap(int n) { char[][] array = new char[n][]; for (int i = 0; i < n; i++) array[i] = next().toCharArray(); return array; } public int[][] nextIntMap(int n, int m) { int[][] map = new int[n][]; for (int i = 0; i < n; i++) { map[i] = nextIntArray(m); } return map; } } }
AtCoder Regular Contest 067 E - Grouping
解法
dp[i][member] := i人をmember人未満のグループに分ける組合せ
としてDPします。
コード
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.NoSuchElementException; import java.util.Scanner; public class Main { private final long MOD = (long) (1e9 + 7); public class Combination { private long[] fact; private long[] invFact; public Combination(int max) { long[] inv = new long[max + 1]; fact = new long[max + 1]; invFact = new long[max + 1]; inv[1] = 1; for (int i = 2; i <= max; i++) inv[i] = inv[(int) (MOD % i)] * (MOD - MOD / i) % MOD; fact[0] = invFact[0] = 1; for (int i = 1; i <= max; i++) fact[i] = fact[i - 1] * i % MOD; for (int i = 1; i <= max; i++) invFact[i] = invFact[i - 1] * inv[i] % MOD; } public long get(int x, int y) { return fact[x] * invFact[y] % MOD * invFact[x - y] % MOD; } } private void solve(Scanner in, PrintWriter out) { int N = in.nextInt(); int A = in.nextInt(); int B = in.nextInt(); int C = in.nextInt(); int D = in.nextInt(); Combination combination = new Combination(N); long[][] dp = new long[N + 2][N + 2]; dp[0][A] = 1; //dp[i][member] := i人を使ってmember人未満のチームで分ける方法 for (int i = 0; i <= N; i++) { for (int member = A; member <= B; member++) { if (dp[i][member] <= 0) continue; dp[i][member + 1] += dp[i][member]; dp[i][member + 1] %= MOD; long d = dp[i][member]; for (int num = 1; num <= D; num++) { if (i + num * member > N) break; int remain = N - (i + (num - 1) * member); d *= combination.get(remain, member); d %= MOD; if (num < C) continue; long e = d * combination.invFact[num]; e %= MOD; dp[i + num * member][member + 1] += e; dp[i + num * member][member + 1] %= MOD; } } } out.println(dp[N][B + 1]); } public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); new Main().solve(in, out); in.close(); out.close(); } }
Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) D. Felicity's Big Secret Revealed
問題
http://codeforces.com/contest/757/problem/D
0と1からなるn文字の文字列が与えられます。この文字列の好きな位置(先頭や末尾も含むN+1個)に仕切りを入れることができます。仕切りはいくつ入れても構いませんが、同じ位置に2個以上入れることはできません。仕切りを入れ終わったら、仕切りに挟まれている数字を10進数になおし、setに入れます。setの最大の数字をmとした時に、setに1〜mが全て含まれているような仕切りの入れ方をvalidとします。validな仕切りの入れ方をmod 1e9+7 で答えてください。
解法
nが75以下なので、mは最大でも20であることが分かります。
次のようなbitDPを考えます。
dp[pos][status] := pos文字目まで見た時に、[1, 20] の数字を含む状態のbitが status であるような組合せ
どこから始めても良いので初期状態は dp[i][0]=1 (0<=i
コード
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.NoSuchElementException; public class D { private final int MOD = (int) (1e9 + 7); private void solve(FastScanner in, PrintWriter out) { int N = in.nextInt(); String S = in.next(); int M = 1 << 20; int[][] dp = new int[N + 1][M]; for (int pos = 0; pos < N; pos++) { dp[pos][0] = 1; } for (int pos = 0; pos < N; pos++) { int cur = 0; for (int end = pos + 1; end <= N; end++) { cur = cur * 2 + (S.charAt(end - 1) - '0'); if (cur > 20) break; if (cur == 0) continue; for (int state = 0; state < M; state++) { int next = state | (1 << (cur - 1)); dp[end][next] += dp[pos][state]; dp[end][next] %= MOD; } } } long ans = 0; for (int end = 0; end <= N; end++) { for (int h = 1; h <= 20; h++) { int state = (1 << h) - 1; ans += dp[end][state]; ans %= MOD; } } out.println(ans); } public static void main(String[] args) { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); new D().solve(in, out); out.close(); } private static class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int bufferLength = 0; private boolean hasNextByte() { if (ptr < bufferLength) { return true; } else { ptr = 0; try { bufferLength = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (bufferLength <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } private void skipUnprintable() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; } boolean hasNext() { skipUnprintable(); return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } double nextDouble() { return Double.parseDouble(next()); } double[] nextDoubleArray(int n) { double[] array = new double[n]; for (int i = 0; i < n; i++) { array[i] = nextDouble(); } return array; } double[][] nextDoubleMap(int n, int m) { double[][] map = new double[n][]; for (int i = 0; i < n; i++) { map[i] = nextDoubleArray(m); } return map; } public int nextInt() { return (int) nextLong(); } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) array[i] = nextInt(); return array; } public long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) array[i] = nextLong(); return array; } public String[] nextStringArray(int n) { String[] array = new String[n]; for (int i = 0; i < n; i++) array[i] = next(); return array; } public char[][] nextCharMap(int n) { char[][] array = new char[n][]; for (int i = 0; i < n; i++) array[i] = next().toCharArray(); return array; } public int[][] nextIntMap(int n, int m) { int[][] map = new int[n][]; for (int i = 0; i < n; i++) { map[i] = nextIntArray(m); } return map; } } }
Codeforces Round #390 (Div. 2) D. Fedor and coupons
問題
http://codeforces.com/contest/754/problem/D
[l, r] のような範囲の組が N 個あります。このうちK個を選び、K個すべてが重なる範囲を最大にしたいです。最大値と、その時選ぶ範囲のidを出力してください。
解法
非想定解法っぽい Treap
コード
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.NoSuchElementException; import java.util.Random; public class D { private void solve(FastScanner in, PrintWriter out) { int N = in.nextInt(); int K = in.nextInt(); long[] l = new long[N]; long[] r = new long[N]; long min = 0; for (int i = 0; i < N; i++) { l[i] = in.nextInt(); r[i] = in.nextInt(); min = Math.min(min, l[i]); } for (int i = 0; i < N; i++) { l[i] -= min; r[i] -= min; l[i] = l[i] * 2 * N + i; r[i] = r[i] * 2 * N + i + N; } long[][] events = new long[2 * N][2]; for (int i = 0; i < N; i++) { events[2 * i][0] = l[i]; events[2 * i][1] = i; events[2 * i + 1][0] = r[i]; events[2 * i + 1][1] = -i - 1; } Arrays.sort(events, (o1, o2) -> { if (o1[0] == o2[0]) return Long.compare(o1[1], o2[1]); return Long.compare(o1[0], o2[0]); }); Treap treap = new Treap(); long from = -1; long max = 0; long[] range = null; int coupons = 0; for (long[] e : events) { int i = (int) e[1]; if (i >= 0) { coupons++; treap.insert(l[i]); if (coupons == K) { from = l[i] / (2 * N); } } else { i = -(i + 1); long tr = r[i] / (2 * N); long tl = l[i] / (2 * N); if (tl <= from && coupons >= K) { long curRange = tr - from + 1; if (max < curRange) { max = curRange; range = new long[]{from, tr}; } treap.erase(l[i]); if (coupons > K) { long kth = treap.rank(K - 1); from = kth / (2 * N); } } else { treap.erase(l[i]); } coupons--; } } out.println(max); if (max == 0) { for (int i = 0; i < K; i++) { out.print((i + 1) + " "); } out.println(); return; } for (int i = 0; i < N; i++) { long L = l[i] / (2 * N); long R = r[i] / (2 * N); if (L <= range[0] && range[1] <= R) { out.print((i + 1) + " "); K--; if (K == 0) break; } } out.println(); } public class Treap { Random random = new Random(); private class Node { Node left, right; long key; int priority; int count; Node(long key) { this.key = key; priority = random.nextInt(); left = null; right = null; count = 1; } } private Node root = null; public void clear() { root = null; } public boolean isEmpty() { return count(root) == 0; } private int count(Node n) { return n == null ? 0 : n.count; } private void update(Node c) { c.count = 1 + count(c.left) + count(c.right); } private Node leftRotate(Node c) { Node r = c.right; c.right = r.left; r.left = c; update(c); return r; } private Node rightRotate(Node c) { Node l = c.left; c.left = l.right; l.right = c; update(c); return l; } private Node insert(Node c, long key) { if (c == null) return new Node(key); if (c.key < key) { c.right = insert(c.right, key); if (c.right.priority < c.priority) c = leftRotate(c); } else { c.left = insert(c.left, key); if (c.left.priority < c.priority) c = rightRotate(c); } update(c); return c; } private Node getMinNode(Node c) { while (c.left != null) c = c.left; return c; } private Node erase(Node c, long key) { if (key == c.key) { if (c.left == null) return c.right; if (c.right == null) return c.left; Node minNode = getMinNode(c.right); c.key = minNode.key; c.right = erase(c.right, minNode.key); } else { if (c.key < key) c.right = erase(c.right, key); else c.left = erase(c.left, key); } update(c); return c; } public void insert(long key) { if (contains(key)) return; root = insert(root, key); } public void erase(long key) { root = erase(root, key); } public int size() { return count(root); } public boolean contains(long key) { return find(root, key) >= 0; } public int find(long key) { return find(root, key); } private int find(Node c, long key) { if (c == null) return -1; if (c.key == key) return count(c.left); if (key < c.key) return find(c.left, key); int pos = find(c.right, key); if (pos < 0) return pos; return count(c.left) + 1 + pos; } private Node rank(Node c, int rank) { while (c != null) { int leftCount = count(c.left); if (leftCount == rank) return c; if (leftCount < rank) { rank -= leftCount + 1; c = c.right; } else { c = c.left; } } return null; } public long rank(int rank) { if (root == null) throw new NullPointerException(); Node r = rank(root, rank); if (r == null) throw new NullPointerException(); return r.key; } } public static void main(String[] args) { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); new D().solve(in, out); out.close(); } private static class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int bufferLength = 0; private boolean hasNextByte() { if (ptr < bufferLength) { return true; } else { ptr = 0; try { bufferLength = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (bufferLength <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } private void skipUnprintable() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; } boolean hasNext() { skipUnprintable(); return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } double nextDouble() { return Double.parseDouble(next()); } double[] nextDoubleArray(int n) { double[] array = new double[n]; for (int i = 0; i < n; i++) { array[i] = nextDouble(); } return array; } double[][] nextDoubleMap(int n, int m) { double[][] map = new double[n][]; for (int i = 0; i < n; i++) { map[i] = nextDoubleArray(m); } return map; } public int nextInt() { return (int) nextLong(); } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) array[i] = nextInt(); return array; } public long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) array[i] = nextLong(); return array; } public String[] nextStringArray(int n) { String[] array = new String[n]; for (int i = 0; i < n; i++) array[i] = next(); return array; } public char[][] nextCharMap(int n) { char[][] array = new char[n][]; for (int i = 0; i < n; i++) array[i] = next().toCharArray(); return array; } public int[][] nextIntMap(int n, int m) { int[][] map = new int[n][]; for (int i = 0; i < n; i++) { map[i] = nextIntArray(m); } return map; } } }