import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.ListIterator; import java.util.StringTokenizer; public class ple { public static String solve(Reader in) throws IOException { int num = in.nextInt(); Plemie[] all = new Plemie[num]; for (int i=0; i<num; i++) { all[i] = new Plemie(in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt()); } Arrays.sort(all, new x1Comparator()); boolean changes = true; while (changes) { changes = false; for (int left = 0; left < num-1; left++) { Plemie leftPlemie = all[left]; if (leftPlemie != null) { for (int next = left + 1; next < num; next++) { Plemie nextPlemie = all[next]; if (nextPlemie != null) { if (xOverlaps(leftPlemie, nextPlemie)) { if (yOverlaps(leftPlemie, nextPlemie)) { // merge merge(leftPlemie, nextPlemie); all[next] = null; changes = true; } } else { break; // koniec nakladajacego sie przedzialu iksa } } } } } } Arrays.sort(all, new finalComparator()); StringBuilder sb = new StringBuilder(); int count = 0; for (Plemie p: all) { if (p == null) continue; count++; sb.append("\n"); sb.append(p.x1); sb.append(" "); sb.append(p.x2); sb.append(" "); sb.append(p.y1); sb.append(" "); sb.append(p.y2); } return count + sb.toString(); } private static void merge(Plemie leftPlemie, Plemie nextPlemie) { leftPlemie.x1 = Math.min(leftPlemie.x1, nextPlemie.x1); leftPlemie.y1 = Math.min(leftPlemie.y1, nextPlemie.y1); leftPlemie.x2 = Math.max(leftPlemie.x2, nextPlemie.x2); leftPlemie.y2 = Math.max(leftPlemie.y2, nextPlemie.y2);; } private static boolean yOverlaps(Plemie leftPlemie, Plemie nextPlemie) { return leftPlemie.y1 < nextPlemie.y2 && leftPlemie.y2 > nextPlemie.y1; } private static boolean xOverlaps(Plemie leftPlemie, Plemie nextPlemie) { return leftPlemie.x1 < nextPlemie.x2 && leftPlemie.x2 > nextPlemie.x1; } private static class x1Comparator implements Comparator<Plemie> { @Override public int compare(Plemie o1, Plemie o2) { return o1.x1.compareTo(o2.x1); } } private static class finalComparator implements Comparator<Plemie> { @Override public int compare(Plemie o1, Plemie o2) { if (o1 == null) { return -1; } else if (o2 == null) { return 1; } else if (o1.x1.compareTo(o2.x1) != 0) { return o1.x1.compareTo(o2.x1); } else if (o1.x2.compareTo(o2.x2) != 0) { return o1.x2.compareTo(o2.x2); } else if (o1.y1.compareTo(o2.y1) != 0) { return o1.y1.compareTo(o2.y1); } else { return o1.y2.compareTo(o2.y2); } } } private static class Plemie { Integer x1; Integer x2; Integer y1; Integer y2; public Plemie(Integer x1, Integer x2, Integer y1, Integer y2) { this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2; } @Override public String toString() { return "Plemie [x1=" + x1 + ", x2=" + x2 + ", y1=" + y1 + ", y2=" + y2 + "]"; } } public static void main(String[] args) throws IOException { Reader reader = new Reader(System.in); Writer writer = new Writer(System.out); String solution = solve(reader); writer.println(solution); writer.flush(); } /** Class for buffered reading int and double values */ public static class Reader { BufferedReader reader; StringTokenizer tokenizer; public Reader(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } /** get next word */ String next() throws IOException { while (!tokenizer.hasMoreTokens()) { String str = reader.readLine(); if (str == null) { return null; } tokenizer = new StringTokenizer(str); } return tokenizer.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } } /** Class for buffered writing int and double values */ public static class Writer { BufferedWriter writer; public Writer(OutputStream out) { writer = new BufferedWriter(new OutputStreamWriter(out), 512); } void print(char c) throws IOException { writer.write(c); } void print(String str) throws IOException { writer.write(str); } void println(String str) throws IOException { writer.write(str); writer.write("\n"); } void println() throws IOException { writer.write("\n"); } void flush() throws IOException { writer.flush(); } @Override protected void finalize() { try { writer.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.ListIterator; import java.util.StringTokenizer; public class ple { public static String solve(Reader in) throws IOException { int num = in.nextInt(); Plemie[] all = new Plemie[num]; for (int i=0; i<num; i++) { all[i] = new Plemie(in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt()); } Arrays.sort(all, new x1Comparator()); boolean changes = true; while (changes) { changes = false; for (int left = 0; left < num-1; left++) { Plemie leftPlemie = all[left]; if (leftPlemie != null) { for (int next = left + 1; next < num; next++) { Plemie nextPlemie = all[next]; if (nextPlemie != null) { if (xOverlaps(leftPlemie, nextPlemie)) { if (yOverlaps(leftPlemie, nextPlemie)) { // merge merge(leftPlemie, nextPlemie); all[next] = null; changes = true; } } else { break; // koniec nakladajacego sie przedzialu iksa } } } } } } Arrays.sort(all, new finalComparator()); StringBuilder sb = new StringBuilder(); int count = 0; for (Plemie p: all) { if (p == null) continue; count++; sb.append("\n"); sb.append(p.x1); sb.append(" "); sb.append(p.x2); sb.append(" "); sb.append(p.y1); sb.append(" "); sb.append(p.y2); } return count + sb.toString(); } private static void merge(Plemie leftPlemie, Plemie nextPlemie) { leftPlemie.x1 = Math.min(leftPlemie.x1, nextPlemie.x1); leftPlemie.y1 = Math.min(leftPlemie.y1, nextPlemie.y1); leftPlemie.x2 = Math.max(leftPlemie.x2, nextPlemie.x2); leftPlemie.y2 = Math.max(leftPlemie.y2, nextPlemie.y2);; } private static boolean yOverlaps(Plemie leftPlemie, Plemie nextPlemie) { return leftPlemie.y1 < nextPlemie.y2 && leftPlemie.y2 > nextPlemie.y1; } private static boolean xOverlaps(Plemie leftPlemie, Plemie nextPlemie) { return leftPlemie.x1 < nextPlemie.x2 && leftPlemie.x2 > nextPlemie.x1; } private static class x1Comparator implements Comparator<Plemie> { @Override public int compare(Plemie o1, Plemie o2) { return o1.x1.compareTo(o2.x1); } } private static class finalComparator implements Comparator<Plemie> { @Override public int compare(Plemie o1, Plemie o2) { if (o1 == null) { return -1; } else if (o2 == null) { return 1; } else if (o1.x1.compareTo(o2.x1) != 0) { return o1.x1.compareTo(o2.x1); } else if (o1.x2.compareTo(o2.x2) != 0) { return o1.x2.compareTo(o2.x2); } else if (o1.y1.compareTo(o2.y1) != 0) { return o1.y1.compareTo(o2.y1); } else { return o1.y2.compareTo(o2.y2); } } } private static class Plemie { Integer x1; Integer x2; Integer y1; Integer y2; public Plemie(Integer x1, Integer x2, Integer y1, Integer y2) { this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2; } @Override public String toString() { return "Plemie [x1=" + x1 + ", x2=" + x2 + ", y1=" + y1 + ", y2=" + y2 + "]"; } } public static void main(String[] args) throws IOException { Reader reader = new Reader(System.in); Writer writer = new Writer(System.out); String solution = solve(reader); writer.println(solution); writer.flush(); } /** Class for buffered reading int and double values */ public static class Reader { BufferedReader reader; StringTokenizer tokenizer; public Reader(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } /** get next word */ String next() throws IOException { while (!tokenizer.hasMoreTokens()) { String str = reader.readLine(); if (str == null) { return null; } tokenizer = new StringTokenizer(str); } return tokenizer.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } } /** Class for buffered writing int and double values */ public static class Writer { BufferedWriter writer; public Writer(OutputStream out) { writer = new BufferedWriter(new OutputStreamWriter(out), 512); } void print(char c) throws IOException { writer.write(c); } void print(String str) throws IOException { writer.write(str); } void println(String str) throws IOException { writer.write(str); writer.write("\n"); } void println() throws IOException { writer.write("\n"); } void flush() throws IOException { writer.flush(); } @Override protected void finalize() { try { writer.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } |