Ultraviolet A 1422-processor (Binary + greedy + priority queue)

Source: Internet
Author: User

Sort the start time first, and leave the queue earlier than the end time using the priority queue;

Because the time is only 20000, We can enumerate each unit of time to see the task to be assigned,

If an end time in a queue is longer than the enumerated time, the result is displayed on the right of the MID.

# Include <stdio. h>

# Include <iostream>
# Include <string. h>
# Include <algorithm>
# Include <stdlib. h>
# Include <math. h>
# Include <queue>
Using namespace STD;
Int t, n;
Struct Node
{
Double R, D, W;
Bool operator <(const node & TT) const {
Return D> TT. D;
}
} Work [1, 10005];
// Bool CMP (node A, Node B)
//{
// Return a. r <B, R;
//}
Bool CMP (node A, Node B ){
Return a. r <B. R;
}
Void Init ()
{
Scanf ("% d", & N );
For (INT I = 0; I <n; I ++)
Scanf ("% lf", & work [I]. R, & work [I]. D, & work [I]. W );
Sort (work, work + N, CMP );
}
Int judge (INT mid)
{
Priority_queue <node> q;
Int wn = 0;
For (INT I = 1; I <= 20000; I ++)
{
Int sum = mid;
While (work [wn]. r <I & wn! = N) // the start time is less than I & the task is not fully determined, and the team enters the queue
Q. Push (work [wn ++]);
While (sum! = 0 &&! Q. Empty ())
{
Node save = Q. Top (); q. Pop ();
If (I> Save. d) return false;
If (save. W> sum) // The task time is greater than the given rate.
{
Save. W-= sum;
Sum = 0;
Q. Push (SAVE );
}
Else
Sum-= save. W;
}
If (wn = N & Q. Empty () break;
}
If (wn = N & Q. Empty ())
Return true;
Return false;


}
Int solve ()
{
Int L = 0, r = 10000000, mid;
While (L <R)
{
If (R-l = 1) break;
Mid = (L + r)> 1;
If (! Judge (MID) L = mid;
Else
R = mid;
}
Return R;
}
Int main ()
{
Scanf ("% d", & T );
While (t --)
{
Init ();
Printf ("% d \ n", solve ());
}
Return 0;
}

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.