main.cc 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <queue>
  4. using namespace std;
  5. const int INF = 1 << 30;
  6. const int MAXL = 1000000;
  7. const int MAXN = 1000;
  8. int dp[MAXL + 1];
  9. int has_cow[MAXL + 1];
  10. int N, L, A, B;
  11. // If f(x) means the min cnt of sprinklers to cover [0...x], then we have
  12. // f(x) = INF, if x is odd
  13. // = INF, if x < 2A
  14. // = INF, if has_cow[x]
  15. // = 1, if 2A <= x <= 2B and !has_cow[x]
  16. // = min{f(y): x - 2B <= y <= x - 2A} + 1, if 2B < x and !has_cow[y]
  17. struct Fx {
  18. int x;
  19. int f;
  20. Fx(int x = 0, int f = 0) : x(x), f(f) {}
  21. bool operator<(const Fx &that) const { return this->f > that.f; }
  22. };
  23. int main() {
  24. scanf("%d %d %d %d", &N, &L, &A, &B);
  25. A <<= 1, B <<= 1;
  26. for (int i = 0; i < N; i++) {
  27. int s, e;
  28. scanf("%d %d", &s, &e);
  29. has_cow[s + 1]++;
  30. has_cow[e]--;
  31. }
  32. for (int i = 0, cows = 0; i <= L; i++) {
  33. dp[i] = INF;
  34. cows += has_cow[i];
  35. has_cow[i] = 0 < cows;
  36. }
  37. priority_queue<Fx> pq;
  38. for (int x = A; x <= B; x += 2) {
  39. if (!has_cow[x]) {
  40. dp[x] = 1;
  41. // Ensure that for each x, the y in pq has y <= x - A
  42. if (x <= B + 2 - A) pq.push(Fx{x, 1});
  43. }
  44. }
  45. for (int x = B + 2; x <= L; x += 2) {
  46. if (!has_cow[x]) {
  47. Fx fx;
  48. while (!pq.empty()) {
  49. fx = pq.top();
  50. if (fx.x < x - B)
  51. pq.pop();
  52. else
  53. break;
  54. }
  55. if (!pq.empty()) dp[x] = fx.f + 1;
  56. }
  57. // Push next Fx to queue.
  58. if (dp[x + 2 - A] != INF) pq.push(Fx(x + 2 - A, dp[x + 2 - A]));
  59. }
  60. int cnt = dp[L] == INF ? -1 : dp[L];
  61. printf("%d\n", cnt);
  62. return 0;
  63. }