main.cc 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include <algorithm>
  2. #include <cstdio>
  3. #include <queue>
  4. using namespace std;
  5. struct Cow {
  6. int i;
  7. int s;
  8. int e;
  9. bool operator<(const Cow &that) const { return this->s < that.s; }
  10. };
  11. struct Stall {
  12. int i;
  13. int e;
  14. // The < op here means the rank of this is LESS than the rank of that.
  15. bool operator<(const Stall that) const { return this->e > that.e; }
  16. };
  17. const int N = 50000;
  18. int main() {
  19. int n, res[N];
  20. Cow c[N];
  21. while (scanf("%d", &n) != EOF) { // Multi group of input
  22. for (int i = 0; i < n; i++) {
  23. scanf("%d %d", &c[i].s, &c[i].e);
  24. c[i].i = i;
  25. }
  26. sort(c, c + n);
  27. priority_queue<Stall> q;
  28. int id = 0;
  29. res[c[0].i] = ++id;
  30. q.push({id, c[0].e});
  31. for (int i = 1; i < n; i++) {
  32. Stall s = q.top();
  33. if (s.e < c[i].s) { // Is NOT <=, but < only
  34. res[c[i].i] = s.i;
  35. s.e = c[i].e;
  36. q.pop();
  37. } else {
  38. res[c[i].i] = ++id;
  39. s = {id, c[i].e};
  40. }
  41. q.push(s);
  42. }
  43. printf("%d\n", id);
  44. for (int i = 0; i < n; i++) printf("%d\n", res[i]);
  45. }
  46. return 0;
  47. }