Submission #1258217

#TimeUsernameProblemLanguageResultExecution timeMemory
1258217hawashraJob Scheduling (CEOI12_jobs)C++20
0 / 100
1094 ms16616 KiB
#include <bits/stdc++.h>
using namespace std;

#define vi vector<int>
#define ii pair<int,int>
#define vii vector<ii>
#define ALL(x) begin(x), end(x)

void solve() {
  int n, d, m; 
  cin >> n >> d >> m;

  // jobs[i] = {availability_day, job_id}
  vii jobs(m);
  for (int i = 0; i < m; ++i) {
    cin >> jobs[i].first;
    jobs[i].second = i + 1; // 1-based job id for printing
  }
  sort(ALL(jobs)); // by availability then id

  // dist[day] = list of job ids scheduled on that day (1..n)
  vector<vi> dist(n + 1);

  auto feasible_and_fill = [&](int machines) -> bool {
    for (int i = 0; i <= n; ++i) dist[i].clear();

    // multiset of "next free day" for each machine. Initially all are free on day 1 (i.e., next_free = 1)
    multiset<int> next_free;
    for (int i = 0; i < machines; ++i) next_free.insert(1);

    for (auto [a, id] : jobs) {
      // find a machine with next_free <= a + d
      auto it = next_free.upper_bound(a + d - 1);
      if (it == next_free.begin()) return false; // all machines next_free > a + d
      --it; // machine with the largest next_free <= a + d

      int t = *it;              // earliest day this machine can start
      int s = max(t, a);        // actual start day
      if (s > n) return false;  // out of horizon
      // s is guaranteed <= a + d by the upper_bound choice

      // schedule on day s
      dist[s].push_back(id);

      // update machine: next free becomes s + 1
      next_free.erase(it);
      next_free.insert(s + 1);
    }
    return true;
  };

  int l = 1, r = min(n, m), ans = m;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (feasible_and_fill(mid)) {
      ans = mid;
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }

  // Recompute schedule for the minimal ans so dist matches what we print
  feasible_and_fill(ans);

  cout << ans << '\n';
  for (int day = 1; day <= n; ++day) {
    for (int id : dist[day]) cout << id << ' ';
    cout << "0\n";
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int tt = 1;
  while (tt--) solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...