fork(7) download
  1. import java.util.*;
  2. class Main {
  3. public static void main (String[] args) {
  4. List<Marker> l = new ArrayList<Marker>();
  5.  
  6. l.add(new Marker(0, true));
  7. l.add(new Marker(10, false));
  8.  
  9. l.add(new Marker(5, true));
  10. l.add(new Marker(20, false));
  11.  
  12. l.add(new Marker(2, true));
  13. l.add(new Marker(30, false));
  14.  
  15. Collections.sort(l);
  16. int total = -1, overlaps = 0;
  17. for(int i = 0; i < l.size(); i++) {
  18. if(l.get(i).green) {
  19. total++;
  20. if(total > 0) overlaps += total;
  21. }
  22. else {
  23. total--;
  24. }
  25. }
  26. System.out.println(overlaps);
  27. }
  28. }
  29. class Marker implements Comparable<Marker> {
  30. int n;
  31. boolean green;
  32. public Marker(int a, boolean b) {
  33. n = a;
  34. green = b;
  35. }
  36. public int compareTo(Marker other) {
  37. return n < other.n ? - 1 : (n > other.n ? 1 : (green ? -1 : 1));
  38. }
  39. }
Success #stdin #stdout 0.02s 245632KB
stdin
Standard input is empty
stdout
3