import java.awt.Point; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Scanner; public class ple { public static void main(String[] args) throws FileNotFoundException { Scanner odczyt = new Scanner(new BufferedInputStream(System.in)); int iloscPlemion = odczyt.nextInt(); ArrayList<Plemie> listaPlemion = new ArrayList<Plemie>(); for (int idPlemienia = 0; idPlemienia < iloscPlemion; idPlemienia++) { int x1 = odczyt.nextInt(); int x2 = odczyt.nextInt(); int y1 = odczyt.nextInt(); int y2 = odczyt.nextInt(); Plemie plemie = new Plemie(x1, y1, x2, y2); boolean czyDodano = false; for (int i = listaPlemion.size()-1; i>=0; i--){ if (listaPlemion.get(i).czyNachodziInnePlemie(plemie)){ Plemie zawierajace = listaPlemion.get(i).zawierajaceOba(plemie); listaPlemion.set(i,zawierajace); czyDodano = true; break; } } if (!czyDodano) listaPlemion.add(plemie); } odczyt.close(); for (int i = listaPlemion.size()-1; i>=0 ; i--){ for (int j = i-1; j>=0; j--){ if (listaPlemion.get(i).czyNachodziInnePlemie(listaPlemion.get(j))){ listaPlemion.set(j, listaPlemion.get(i).zawierajaceOba(listaPlemion.get(j))); listaPlemion.remove(i); i = listaPlemion.size(); break; } } } Collections.sort(listaPlemion, new Leksykograficzna()); PrintWriter r = new PrintWriter(new BufferedOutputStream(System.out)); r.println(listaPlemion.size()); for (int i = 0; i<listaPlemion.size(); i++){ r.println(listaPlemion.get(i).toString()); } r.flush(); } } class Leksykograficzna implements Comparator<Plemie> { @Override public int compare(Plemie p, Plemie d) { return p.toString().compareTo(d.toString()); } } class Plemie { Point ld = null; Point pg = null; Plemie(int x1, int y1, int x2, int y2) { this.ld = new Point(x1, y1); this.pg = new Point(x2, y2); } boolean czyNachodziInnePlemie(Plemie inne) { return (this.ld.x < inne.pg.x && this.pg.x > inne.ld.x && this.ld.y < inne.pg.y && this.pg.y > inne.ld.y); } Plemie zawierajaceOba(Plemie inne){ return new Plemie(Math.min(ld.x, inne.ld.x), Math.min(ld.y, inne.ld.y), Math.max(pg.x, inne.pg.x), Math.max(pg.y, inne.pg.y)); } public String toString(){ return ld.x + " " + pg.x + " " + ld.y + " " + pg.y; } }
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 | import java.awt.Point; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Scanner; public class ple { public static void main(String[] args) throws FileNotFoundException { Scanner odczyt = new Scanner(new BufferedInputStream(System.in)); int iloscPlemion = odczyt.nextInt(); ArrayList<Plemie> listaPlemion = new ArrayList<Plemie>(); for (int idPlemienia = 0; idPlemienia < iloscPlemion; idPlemienia++) { int x1 = odczyt.nextInt(); int x2 = odczyt.nextInt(); int y1 = odczyt.nextInt(); int y2 = odczyt.nextInt(); Plemie plemie = new Plemie(x1, y1, x2, y2); boolean czyDodano = false; for (int i = listaPlemion.size()-1; i>=0; i--){ if (listaPlemion.get(i).czyNachodziInnePlemie(plemie)){ Plemie zawierajace = listaPlemion.get(i).zawierajaceOba(plemie); listaPlemion.set(i,zawierajace); czyDodano = true; break; } } if (!czyDodano) listaPlemion.add(plemie); } odczyt.close(); for (int i = listaPlemion.size()-1; i>=0 ; i--){ for (int j = i-1; j>=0; j--){ if (listaPlemion.get(i).czyNachodziInnePlemie(listaPlemion.get(j))){ listaPlemion.set(j, listaPlemion.get(i).zawierajaceOba(listaPlemion.get(j))); listaPlemion.remove(i); i = listaPlemion.size(); break; } } } Collections.sort(listaPlemion, new Leksykograficzna()); PrintWriter r = new PrintWriter(new BufferedOutputStream(System.out)); r.println(listaPlemion.size()); for (int i = 0; i<listaPlemion.size(); i++){ r.println(listaPlemion.get(i).toString()); } r.flush(); } } class Leksykograficzna implements Comparator<Plemie> { @Override public int compare(Plemie p, Plemie d) { return p.toString().compareTo(d.toString()); } } class Plemie { Point ld = null; Point pg = null; Plemie(int x1, int y1, int x2, int y2) { this.ld = new Point(x1, y1); this.pg = new Point(x2, y2); } boolean czyNachodziInnePlemie(Plemie inne) { return (this.ld.x < inne.pg.x && this.pg.x > inne.ld.x && this.ld.y < inne.pg.y && this.pg.y > inne.ld.y); } Plemie zawierajaceOba(Plemie inne){ return new Plemie(Math.min(ld.x, inne.ld.x), Math.min(ld.y, inne.ld.y), Math.max(pg.x, inne.pg.x), Math.max(pg.y, inne.pg.y)); } public String toString(){ return ld.x + " " + pg.x + " " + ld.y + " " + pg.y; } } |