Submission #1051509

# Submission time Handle Problem Language Result Execution time Memory
1051509 2024-08-10T07:39:33 Z NeroZein Fire (BOI24_fire) C++17
0 / 100
1 ms 600 KB
#include "bits/stdc++.h"
using namespace std;

#ifdef Nero
#include "Deb.h"
#else
#define debug(...)
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vector<int> s(n), e(n);
  vector<int> normal;
  vector<int> non_normal;
  
  for (int i = 0; i < n; ++i) {
    cin >> s[i] >> e[i];
    if (s[i] <= e[i]) {
      normal.push_back(i);
    } else {
      assert(e[i] == 0);
      non_normal.push_back(i); 
    }
  }
  
  sort(normal.begin(), normal.end(), [&](int i, int j) {
    return s[i] < s[j];
  });
  
  int res = 0;
  int lst = 0; 
  for (int i = 0, mx = 0; i < (int) normal.size(); ++i) {
    
    if (s[normal[i]] > lst) {
      if (mx < s[normal[i]]) {
        break; 
      } else {
        res++; 
        lst = mx; 
        mx = max(mx, e[normal[i]]);
      }
    } else {
      mx = max(mx, e[normal[i]]);
    }
    if (i == (int) normal.size() - 1) {
      lst = mx; 
      res++;
    }
  }
  if (lst < m - 1) {
    res = n + 1; 
  }
  
  int ans = res; 
  int ind = 0;
  for (int i = 0; i < (int) non_normal.size(); ++i) {
    if (s[non_normal[i]] < s[non_normal[ind]]) {
      ind = i; 
    }
  }
  if (!non_normal.empty()) {
    for (int i = ind; i <= ind; ++i) {
      
      vector<pair<int, int>> ranges;
      for (int j = 0; j < (int) non_normal.size(); ++j) {
        if (i != j) {
          ranges.push_back({0, e[non_normal[j]]});
        }
      }
      
      for (int j = 0; j < (int) normal.size(); ++j) {
        ranges.push_back({s[normal[j]], e[normal[j]]});
      }
      
      sort(ranges.begin(), ranges.end()); 
      res = 1;
      lst = e[non_normal[i]];
      
      for (int j = 0, mx = e[non_normal[i]]; j < (int) ranges.size() && lst < s[non_normal[i]]; ++j) {
        auto [l, r] = ranges[j];
        if (l > lst) {
          if (mx < min(l, s[non_normal[i]])) {
            break;
          } else {
            res++;
            lst = mx; 
            mx = max(mx, r); 
          }
        } else {
          mx = max(mx, r); 
        }
        if (j == (int) ranges.size() - 1 && lst < s[non_normal[i]]) {
          lst = mx;
          res++; 
        }
      }
      if (lst < s[non_normal[i]]) {
        res = n + 1; 
      }
      ans = min(ans, res); 
    }
    if (ans > n) {
      ans = -1; 
    }
  }
  cout << ans << '\n';
  return 0;
}
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 600 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 600 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 600 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 600 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -