Created
May 4, 2015 21:04
-
-
Save volyx/1f5c1772c1a65592e501 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
class Main { | |
public static void main(String[] args) { | |
Scanner s = new Scanner(System.in); | |
String nextLine = null; | |
int n = 0; | |
int i = 1; | |
List<Line> lines = null; | |
String[] ar = null; | |
Line line = null; | |
while (s.hasNext()) { | |
nextLine = s.next(); | |
if (i == 1) { | |
n = Integer.parseInt(nextLine.trim()); | |
lines = new ArrayList<>(n); | |
} else { | |
if (i % 2 == 0) { | |
line = new Line(); | |
line.x = Integer.parseInt(nextLine); | |
} else { | |
line.y = Integer.parseInt(nextLine); | |
lines.add(line); | |
if (i == n*2 + 1) { | |
break; | |
} | |
} | |
} | |
i++; | |
} | |
// System.out.println(lines); | |
Collections.sort(lines, new Comparator<Line>() { | |
@Override | |
public int compare(Line o1, Line o2) { | |
return Integer.compare(o1.y, o2.y); | |
} | |
}); | |
// System.out.println(lines); | |
Set<Integer> dots = new HashSet<Integer>(n); | |
for (Line l : lines) { | |
int c = l.y; | |
if (!l.visited) { | |
dots.add(c); | |
l.visited = true; | |
} | |
int j = lines.indexOf(l) + 1; | |
while (j < lines.size()) { | |
Line next = lines.get(j); | |
j++; | |
if (next.visited) { | |
continue; | |
} | |
if (next.contains(c)) { | |
next.visited = true; | |
} | |
} | |
} | |
System.out.println(dots.size()); | |
for (int dot : dots) { | |
System.out.print(dot + " "); | |
} | |
} | |
private static class Line { | |
int x; | |
int y; | |
boolean visited = false; | |
public boolean contains(int d ) { | |
return (d >= x && d <= y); | |
} | |
@Override | |
public String toString() { | |
return "{" + | |
x + | |
", " + y + | |
'}'; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (o == null || getClass() != o.getClass()) return false; | |
Line line = (Line) o; | |
if (x != line.x) return false; | |
if (y != line.y) return false; | |
return true; | |
} | |
@Override | |
public int hashCode() { | |
int result = x; | |
result = 31 * result + y; | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment