Uva-1153-keep the Customer satisfied

Source: Internet
Author: User

Simon and Garfunkel Corporation (SG Corp.) are 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 is characterized by both integer values q , the amount of steel required (in tons) an D D , the due date (a calender date converted in seconds). The due date has been met if SG Corp accepts the order. Stated another, when an order is accepted, the corresponding amount of steel have to being 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 being seen as a single production line with a constant THROUGHPU T. 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 is collected and Paul and Art determine which of them is going To ISacceptedand which ones isrejectedIn the next production period. A Production schedule is then designed. To keep customers satisfied, Paul and Art want toMinimize The total number of orders that is rejected. In the following, we assume this beginning of the next production plan (i.e., the first day of the next month) Corresp Onds to date 0.

Hogdson and Moore has been appointed as Chief scientific officers and you is requested to help them to compute an Optima L 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 21st
J6 5 22

You can check by hand this all orders cannot is accepted and it's very unlikely you could find a solution with less than t Wo rejected orders. Optimal solution:reject J1 and J4 , accept all other orders and process th Em 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.

Input

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 was followed by a blank line, and there was also a blank line between the consecutive inputs.


Data Each test case was described by one input file, 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 a of the values:the amount of steel (in tons) required for the order (Lower T Han) and its due date (in seconds; lower than 2 x 106 ).

Output

For each test case, the output must follow the description below. The outputs of the consecutive cases would be separated to a blank line.


You is required to compute a optimal solution and your program have to write the number of orders that is accepted.

Sample Input

167 158 206 84 93 215 22

Sample Output

4


Some Hints from Hogdson and Moore

    • Hogdson and Moore claim that it's optimal to sequence accepted orders in non-decreasing order of due dates.
    • They also claim that there be an optimal solution such so for any of the orders Ju and Jv with qu > qvand du < dv , if Ju is accepted and then Jv is also accepted.
    • Finally, Hogdson and Moore advise you to ' Keep the Customer satisfied '


Keep the Customer satisfied


Gee but it's great to being back home home was where I want to be. I ' ve been on the road so long my friend, and if you came alongi know you couldn ' t disagree.  It's the same old stories 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, steps away from the county line Just trying to keep my customers satisfied, satisfied.deputy Sheriff said to me tell me about come here for, boy. You better get your bags and flee. You're ' re in Trouble Boy, and you ' re heading to more.

? Simon & Garfunkel


Use pair as the basic structure to save Q and D, using an array vector<pair<int, int> > to store the input data. Then sort by the wait time D, note that sorting by sort is by default with first, where first is Q, so rewrite a comparison function and sort by second.

Next is the greedy algorithm, with priority queue Priority_queue<pair<int, int> > Storage results, here reflects the advantages of using pair, just the priority queue is first high priority.

Queue up, if the total time exceeds the current limit time, then the team spends the longest time element (that is, q the largest) deleted.

AC Code:

#include <iostream> #include <cstdio> #include <cstdlib> #include <cctype> #include <cstring > #include <string> #include <sstream> #include <vector> #include <set> #include <map># Include <algorithm> #include <stack> #include <queue> #include <bitset> #include <cassert > #include <cmath> #include <functional>using namespace Std;typedef pair<int, int> pair;vector< Pair> State;int N, ans, tmp;//the default Pair sort is first, but the order here needs D, that is second, so write a comparison function bool CMP (const pair &AMP;A, const pair &A MP;B) {return A.second < B.second;} void Solve () {priority_queue<pair> q;//with priority queue, which is the first comparison size of ans = TMP = 0;//Remember to initialize ... for (int i = 0; i < n; i++) {//greedy solution tmp + = State[i].first;q.push (State[i]); ANS++;IF (tmp > State[i].second) {//IF Ultra-current wait time pair T = q.top (); Max is q the largest element Q.pop (); ans--;tmp-= T.first;}} cout << ans << endl;} int main () {Ios::sync_with_stdio (false); int T;cin >> T;whiLe (t--) {state.clear (); Cin >> N;int Q, d;for (int i = 0; i < n; i++) {cin >> Q >> d;state.push_back ( Make_pair (q, D));} Sort (State.begin (), State.end (), CMP); Sort solve () according to the size of D, if (T) {cout << Endl;}} return 0;}




Uva-1153-keep the Customer satisfied

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.