import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class dru { public static void main(String[] args) throws IOException { dru solver = new dru(); solver.init(); solver.solve(); } private void init() { } final static int HEIGHT = 20; int[] minGroup = new int[1 << (HEIGHT + 1)]; void initMinGroups(int[] c) { for (int i = 0; i < c.length; i++) minGroup[i | (1 << HEIGHT)] = c[i]; for (int i = (1 << HEIGHT) - 1; i > 0; i--) { minGroup[i] = Math.max(minGroup[2 * i], minGroup[2 * i + 1]); } } int findMinGroup(int from, int to) { from |= 1 << HEIGHT; to |= 1 << HEIGHT; int r = Math.max(minGroup[from], minGroup[to]); while ((from >> 1) != (to >> 1)) { if ((from & 1) == 0) r = Math.max(r, minGroup[from + 1]); if ((to & 1) == 1) r = Math.max(r, minGroup[to - 1]); from >>= 1; to >>= 1; } return r; } static class Item { int groups; int card; public Item(int groups, int card) { super(); this.groups = groups; this.card = card; } } Item[] cache; int n; int[] c; int[] d; private void solve() throws IOException { Reader in = new Reader(System.in); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); n = in.nextInt(); c = new int[n]; d = new int[n]; for (int i = 0; i < n; i++) { c[i] = in.nextInt(); d[i] = in.nextInt(); } cache = new Item[n + 1]; cache[n] = new Item(0,1); Item result = go(0); if (result.groups == -1) { out.println("NIE"); } else { out.println(result.groups + " " + result.card); } out.flush(); out.close(); } final static int MOD = 1000000007; private Item go(int pos) { if (cache[pos] != null) return cache[pos]; Item result = new Item(-1, 0); int minMove = 0; int maxMove = Integer.MAX_VALUE; for (int move = 1; pos + move <= n; move++) { minMove = Math.max(minMove, c[pos + move - 1]); maxMove = Math.min(maxMove, d[pos + move - 1]); if (move > maxMove) break; if (move >= minMove) { Item r = go(pos + move); if (r.groups == -1) continue; if (r.groups > result.groups) { result.groups = r.groups; result.card = r.card; } else if (r.groups == result.groups) { result.card += r.card; if (result.card >= MOD) result.card -= MOD; } } } if (result.groups > -1) { result.groups++; } cache[pos] = result; return result; } private static class Reader { BufferedReader reader; StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ Reader(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } public void skipLine() throws IOException { reader.readLine(); } /** get next word */ public String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt( next() ); } public double nextDouble() throws IOException { return Double.parseDouble( next() ); } public long nextLong() throws IOException { return Long.parseLong(next()); } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class dru { public static void main(String[] args) throws IOException { dru solver = new dru(); solver.init(); solver.solve(); } private void init() { } final static int HEIGHT = 20; int[] minGroup = new int[1 << (HEIGHT + 1)]; void initMinGroups(int[] c) { for (int i = 0; i < c.length; i++) minGroup[i | (1 << HEIGHT)] = c[i]; for (int i = (1 << HEIGHT) - 1; i > 0; i--) { minGroup[i] = Math.max(minGroup[2 * i], minGroup[2 * i + 1]); } } int findMinGroup(int from, int to) { from |= 1 << HEIGHT; to |= 1 << HEIGHT; int r = Math.max(minGroup[from], minGroup[to]); while ((from >> 1) != (to >> 1)) { if ((from & 1) == 0) r = Math.max(r, minGroup[from + 1]); if ((to & 1) == 1) r = Math.max(r, minGroup[to - 1]); from >>= 1; to >>= 1; } return r; } static class Item { int groups; int card; public Item(int groups, int card) { super(); this.groups = groups; this.card = card; } } Item[] cache; int n; int[] c; int[] d; private void solve() throws IOException { Reader in = new Reader(System.in); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); n = in.nextInt(); c = new int[n]; d = new int[n]; for (int i = 0; i < n; i++) { c[i] = in.nextInt(); d[i] = in.nextInt(); } cache = new Item[n + 1]; cache[n] = new Item(0,1); Item result = go(0); if (result.groups == -1) { out.println("NIE"); } else { out.println(result.groups + " " + result.card); } out.flush(); out.close(); } final static int MOD = 1000000007; private Item go(int pos) { if (cache[pos] != null) return cache[pos]; Item result = new Item(-1, 0); int minMove = 0; int maxMove = Integer.MAX_VALUE; for (int move = 1; pos + move <= n; move++) { minMove = Math.max(minMove, c[pos + move - 1]); maxMove = Math.min(maxMove, d[pos + move - 1]); if (move > maxMove) break; if (move >= minMove) { Item r = go(pos + move); if (r.groups == -1) continue; if (r.groups > result.groups) { result.groups = r.groups; result.card = r.card; } else if (r.groups == result.groups) { result.card += r.card; if (result.card >= MOD) result.card -= MOD; } } } if (result.groups > -1) { result.groups++; } cache[pos] = result; return result; } private static class Reader { BufferedReader reader; StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ Reader(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } public void skipLine() throws IOException { reader.readLine(); } /** get next word */ public String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt( next() ); } public double nextDouble() throws IOException { return Double.parseDouble( next() ); } public long nextLong() throws IOException { return Long.parseLong(next()); } } } |