Submission #449905

# Submission time Handle Problem Language Result Execution time Memory
449905 2021-08-02T09:24:26 Z chuangsheep Job Scheduling (CEOI12_jobs) C++11
60 / 100
517 ms 26988 KB
#include <bits/stdc++.h>
using namespace std;

#define all(x) begin(x), end(x)
#define mp make_pair

using pi = pair<int, int>;
using vi = vector<int>;

int N, D, M;

// test if it is possible to finish the jobs using given # of machines
// return: first: possible or not, second: if possible, the schedule for the jobs
pair<bool, vector<vi>> isFeasible(const vector<pi> &jobs, int machineCount)
{
	vector<vi> schedule(N);
	int reqNum = 0;
	// we simulate from day 1 ultil the last day N
	// we move to the next day if all the machines are used or
	// there is no more job requests left on or before this day
	for (int day = 1; day <= N; day++)
	{
		for (int j = 0; j < machineCount; j++)
		{
			// if all jobs before and on this day are finished,
			// we can go to the next day, even if there are usable machines left
			// we can determine that since the vector jobs is sorted
			if (jobs[reqNum].first > day)
				break;
			// if the current date is before the deadline for the job
			// we can add this job to the schedule and move to the next job request
			if (jobs[reqNum].first + D >= day)
				schedule[day - 1].push_back(jobs[reqNum++].second);
			// otherwise, it is not feasible due to deadline
			else
				return mp(false, schedule);

			// if we have processed all the requests, we have found a feasible sol
			if (reqNum == M)
				return mp(true, schedule);
		}
	}
	// if not all the requests can be processed within the given N days,
	// then it is not feasible
	return mp(false, schedule);
}

int main()
{
	cin.tie(0)->sync_with_stdio(false);

	cin >> N >> D >> M;
	vector<pi> jobs(M);
	for (int i = 0; i < M; i++)
	{
		int day;
		cin >> day;
		// first: request date, second: index [1..M]
		jobs[i] = mp(day, i + 1);
	}
	// we sort the jobs by the request date in ascending order
	// sothat we can test them using isFeasible() in linear time whether they
	// can be done in given time using a certain amount of machines
	sort(all(jobs));

	vector<vi> result;
	// binary search on the number of machines for the minimum possible solution
	// left and right bound, l and r
	// (M + D) / (D + 1) as integer division is equivalent to ceiling(M / (D + 1))
	int l = 1, r = (M + D) / (D + 1);
	while (l < r)
	{
		int machineNum = (l + r) / 2;
		// test if the jobs would finish within the deadline
		// using the current # of machines, machineNum
		pair<bool, vector<vi>> curResult = isFeasible(jobs, machineNum);
		// if it's possible, we set the right bound as the tested machine number
		// and save the current schedule
		if (curResult.first)
		{
			r = machineNum;
			result = curResult.second;
		}
		// otherwise, we set the left bound to be the tested number + 1
		// and test the next machineNum again
		else
			l = machineNum + 1;
	}

	cout << l << "\n";
	for (int i = 0; i < N; i++)
	{
		for (int &idx : result[i])
			cout << idx << " ";
		cout << 0 << "\n";
	}
}
# Verdict Execution time Memory Grader output
1 Runtime error 28 ms 2680 KB Execution killed with signal 11
2 Runtime error 28 ms 2700 KB Execution killed with signal 11
3 Runtime error 28 ms 2700 KB Execution killed with signal 11
4 Runtime error 28 ms 2644 KB Execution killed with signal 11
5 Runtime error 28 ms 2712 KB Execution killed with signal 11
6 Runtime error 28 ms 2708 KB Execution killed with signal 11
7 Runtime error 35 ms 2652 KB Execution killed with signal 11
8 Runtime error 28 ms 2708 KB Execution killed with signal 11
9 Correct 91 ms 10068 KB Output is correct
10 Correct 102 ms 9912 KB Output is correct
11 Correct 48 ms 3112 KB Output is correct
12 Correct 80 ms 5504 KB Output is correct
13 Correct 138 ms 9280 KB Output is correct
14 Correct 176 ms 11516 KB Output is correct
15 Correct 240 ms 11556 KB Output is correct
16 Correct 328 ms 16432 KB Output is correct
17 Correct 357 ms 19264 KB Output is correct
18 Correct 376 ms 23824 KB Output is correct
19 Correct 517 ms 26988 KB Output is correct
20 Correct 335 ms 19228 KB Output is correct