Simon and Garfunkel Corporation (SG Corp.) is a large steel-making company with thousand of customers. Keeping the customer satisfied is one of the major objective of Paul and Art, the managers.
Customers issue orders that are characterized by two integer values q , the amount of steel required (in tons) and d , the due date (a calender date converted in seconds). The due date has to be met if SG Corp. accepts the order. Stated another way, when an order is accepted, the corresponding amount of steel has to be produced before its due date. Of course, the factory can process no more than one order at a time.
Although the manufacturing process is rather complex, it can be seen as a single production line with a constant throughput. In the following, we assume that producing q tons of steel takes exactly q seconds(i.e., throughput is 1). The factory runs on a monthly production plan. Before the beginning of the month, all customers' orders are collected and Paul and Art determine which of them are going to be accepted and which ones are to be rejected in the next production period. A production schedule is then designed. To keep customers satisfied, Paul and Art want to minimize the total number of orders that are rejected. In the following, we assume that the beginning of the next production plan (i.e., the first day of the next month) corresponds to date 0.
Hogdson and Moore have been appointed as Chief Scientific Officers and you are requested to help them to compute an optimal solution and to build a schedule of all accepted orders (starting time and completion time).
Small Example
Consider the following data set made of 6 orders J1,..., J6 . For a given order, Jj , qj denotes the amount of steel required and dj is the associated due date.
Order | qj | dj |
J1 | 6 | 8 |
J2 | 4 | 9 |
J3 | 7 | 15 |
J4 | 8 | 20 |
J5 | 3 | 21 |
J6 | 5 | 22 |
You can check by hand that all orders cannot be accepted and it's very unlikely you could find a solution with less than two rejected orders. Here is an optimal solution: Reject J1 and J4 , accept all other orders and process them as follows.
Accepted Order | Starting Time | Completion Time |
J2 | 0 | 4 |
J3 | 4 | 11 |
J5 | 11 | 14 |
J6 | 14 | 19 |
Note that the production line is never idle.
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
Data Each test case is described by one input file that contains all the relevant data: The first line contains the number n of orders (n can be as large as 800000 for some test cases). It is followed by nlines. Each of which describes an order made of two integer values: the amount of steel (in tons) required for the order (lower than 1000) and its due date (in seconds; lower than 2 x 106 ).
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
You are required to compute an optimal solution and your program has to write the number of orders that are accepted.
1 6 7 15 8 20 6 8 4 9 3 21 5 22
4
Some Hints from Hogdson and Moore
Keep the Customer Satisfied
Gee but it's great to be back home Home is where I want to be. I've been on the road so long my friend, And if you came along I know you couldn't disagree. It's the same old story Everywhere I go, I get slandered, Libeled, I hear words I never heard In the bible And I'm on step ahead of the shoe shine Two steps away from the county line Just trying to keep my customers satisfied, Satisfied. Deputy sheriff said to me Tell me what you come here for, boy. You better get your bags and flee. You're in trouble boy, And you're heading into more.
©Simon & Garfunkel
题意:n个任务,每个任务有完成需要时间,和过期时间,求一个完成任务的顺序使得尽可能完成多的任务。
思路:n有80W。贪心+优先队列优化,过期时间小的优先入队,如果遇到一个不能完成的任务(总时间超过过期时间),就把队列中用时最多的任务出队。
代码:
#include <stdio.h> #include <string.h> #include <algorithm> #include <queue> using namespace std; const int N = 888888; int t, n; struct D { int t, dt; friend bool operator < (D a, D b) { return a.t < b.t; } } d[N]; int cmp(D a, D b) { return a.dt < b.dt; } void init() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d%d", &d[i].t, &d[i].dt); } int solve() { priority_queue<D>Q; sort(d, d + n, cmp); int time = 0, ans = 0; for (int i = 0; i < n; i++) { time += d[i].t; Q.push(d[i]); ans ++; if (time > d[i].dt) { time -= Q.top().t; Q.pop(); ans --; } } return ans; } int main() { scanf("%d", &t); while (t--) { init(); printf("%d\n", solve()); if (t) printf("\n"); } return 0; }