Adding some more judges, here and there.
[and.git] / NEERC / kequiv / kequiv_md.java
blob91e7de8261c785f3129293d005a98fd0b839fe35
1 import java.io.*;
2 import java.util.*;
4 public class kequiv_md {
5 private static String fileName = kequiv_md.class.getSimpleName().replaceFirst("_.*", "");
6 private static String inputFileName = fileName + ".in";
7 private static String outputFileName = fileName + ".out";
8 private static Scanner in;
9 private static PrintWriter out;
10 private final static int TEN = 10;
12 class Intervals {
13 ArrayList<Long> low = new ArrayList<Long>();
14 ArrayList<Long> high = new ArrayList<Long>();
15 int hash = 0;
17 public void add(long lo, long hi) {
18 if (lo < hi) {
19 low.add(lo);
20 high.add(hi);
24 public Intervals setDigit(long ten, int d) {
25 Intervals that = new Intervals();
26 for (int i = 0; i < low.size(); i++) {
27 that.add(above(low.get(i), ten, d), above(high.get(i), ten, d));
29 return that.normalize();
32 private Intervals normalize() {
33 if (low.isEmpty()) {
34 return this;
36 Intervals that = new Intervals();
37 that.low.add(low.get(0));
38 for (int i = 0; i < low.size() - 1; i++) {
39 if (high.get(i) < low.get(i + 1)) {
40 that.high.add(high.get(i));
41 that.low.add(low.get(i + 1));
44 that.high.add(high.get(high.size() - 1));
45 return that;
48 @Override
49 public boolean equals(Object obj) {
50 Intervals that = (Intervals) obj;
51 return low.equals(that.low) && high.equals(that.high);
54 @Override
55 public int hashCode() {
56 if (hash == 0) {
57 hash = low.hashCode() * 179 + high.hashCode();
59 return hash;
63 public Long above(long n, long ten, int d) {
64 long a = n / ten;
65 int c = (int) (a % 10);
66 a /= 10;
67 long b = n % ten;
68 if (c < d) {
69 return a * ten;
71 if (c > d) {
72 return (a + 1) * ten;
74 return a * ten + b;
77 public void run() {
78 Intervals a = new Intervals();
79 int n = in.nextInt();
80 for (int i = 0; i < n; i++) {
81 a.add(in.nextLong(), in.nextLong() + 1);
84 boolean[][] differ = new boolean[TEN][TEN];
85 for (long ten = 1; ten < a.high.get(a.high.size() - 1) && ten > 0; ten *= TEN) {
86 int[] val = new int[TEN];
87 HashMap<Intervals, Integer> results = new HashMap<Intervals, Integer>();
88 for (int d = 1; d < TEN; d++) {
89 Intervals b = a.setDigit(ten, d);
90 Integer v = results.get(b);
91 if (v == null) {
92 v = results.size();
93 results.put(b, v);
95 val[d] = v;
96 for (int e = 1; e < d; e++) {
97 if (val[d] != val[e]) {
98 differ[e][d] = true;
104 boolean[] printed = new boolean[TEN];
105 for (int d = 1; d < TEN; d++) {
106 if (printed[d]) {
107 continue;
109 for (int e = d; e < TEN; e++) {
110 if (!differ[d][e]) {
111 out.print(e);
112 printed[e] = true;
115 out.println();
119 public static void main(String[] args) throws IOException {
120 Locale.setDefault(Locale.US);
121 in = new Scanner(new FileReader(inputFileName));
122 out = new PrintWriter(outputFileName);
123 new kequiv_md().run();
124 in.close();
125 out.close();