洛谷P2751 工序安排Job Processing

题目

任务调度贪心。

需要明确一点,任务调度贪心题,并不是简单地应用排序的贪心,而是动态的运用堆,使每次选择是都能保持局部最优,并更新状态使得下次更新答案可以取到正确的最小值。

这是A过程的解。

然后考虑B过程则需要从最后的物体开始操作,可以使时间最小,取每个物体最后完成的最大值。而且使每个物体都花费最小时间,总的时间肯定也是最小的。

#include <bits/stdc++.h>
using namespace std;
int n, m1, m2, ans[1000010], ans2;
struct c{
	int t, cos;
	bool operator < (c a) const {
		return a.t < t;
	}
}A[43], B[43];
priority_queue <c> q;
int main()
{
	scanf("%d%d%d", &n, &m1, &m2);
	for (int i = 1; i <= m1; i++)
		scanf("%d", &A[i].cos), A[i].t = A[i].cos, q.push(A[i]);
	for (int i = 1; i <= n; i++)
	{
		c now = q.top();
		q.pop();
		ans[i] = now.t;
		now.t += now.cos;
		q.push(now);
	}
	while ( q.size() )
		q.pop();
	printf("%d ", ans[n]);
	// ans[i]表示第i个物品完成的时间 
	for (int i = 1; i <= m2; i++)
		scanf("%d", &B[i].cos), B[i].t = B[i].cos, q.push(B[i]);
	for (int i = n; i >= 1; i--)//先处理后完成的,然后用一般的任务调度问题来解决 
	{
		c now = q.top();
		q.pop();
		ans2 = max(ans2, now.t + ans[i]);//最晚的活动时间 
		now.t += now.cos;
		q.push(now); 
	}
	printf("%d", ans2);
	return 0;
}
原文地址:https://www.cnblogs.com/liuwenyao/p/11507133.html