Uva11389 (Greedy) and uva11389 greedy
Ultraviolet A 11389
Time Limit:1000 MSMemory Limit:0 KB64bit IO Format:% Lld & % llu
Description
IIUC ONLINECONTEST 2008 |
The Bus Driver Problem |
Input: standard input Output: standard output |
|
In a city there areNBus drivers. Also there areNMorning bus routes &NAfternoon bus routes with varous lengths. Each driver is assigned one morning route & one evening route. For any driver, if his total route length for a day exceedsD, He has to be paid overtime for every hour after the firstDHours at a flatRTaka/hour. Your task is to assign one morning route & one evening route to each bus driver so that the total overtime amount that the authority has to pay is minimized. |
Input |
The first line of each test case has three integersN,DAndR, As described above. In the second line, there areNSpace separated integers which are the lengths of the morning routes given in meters. Similarly the third line hasNSpace separated integers denoting the evening route lengths. The lengths are positive integers less than or equal to 10000. The end of input is denoted by a case with three 0 s. |
Output |
For each test case, print the minimum possible overtime amount that the authority must pay. |
Constraints |
-1 ≤ n ≤ 100 -1 ≤ d ≤ 10000 -1 ≤ r ≤ 5 |
Sample Input |
Output for Sample Input |
2 20 5 10 15 10 15 2 20 5 10 10 10 10 0 0 0 |
50 0 |
|
|
|
Question: This question is to solve the actual problem. There are n morning tasks and afternoon tasks assigned to the driver. If the total working time exceeds d, overtime is required for the excess tasks;
Now let you schedule the task and ask for the minimum overtime fee.
Analysis: greedy problem. Sort the two tasks in ascending and descending order, and each group of corresponding edge numbers is enough.
Set the pairs of elements in the sequence to m1, a1, m2, a2, m1 ≤ m2, a1 ≤ a2;
The matching method is <m1, a2>, <m2, a1> better than <m1, a1>, <m2, a2>
The Code is as follows:
# Include <iostream>
# Include <algorithm>
Using namespace std;
Int main ()
{
Int I, x, y, z, a [10], B [10];
While (cin> x> y> z & x & y & z)
{
Int s = 0; // each time it is set to 0
For (I = 1; I <= x; I ++)
{
In> a [I];
}
For (I = 1; I <= x; I ++)
{
Cin> B [I];
}
Sort (a, a + x );
Sort (B, B + x );
For (I = 1; I <= x; I ++)
{
If (a [I] + B [x + 1-i]> y)
{
S = (a [I] + B [x-I + 1]-y) * z;
S + = s;
}
}
Cout <s <endl;
}
Return 0;
}