H - 8 UVA - 11389

In a city there are n bus drivers. Also there are n morning bus routes and n afternoon bus routes with
various lengths. Each driver is assigned one morning route and one evening route. For any driver, if
his total route length for a day exceeds d, he has to be paid overtime for every hour after the first d
hours at a flat r taka / hour. Your task is to assign one morning route and 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 integers n, d and r, as described above. In the second line,
there are n space separated integers which are the lengths of the morning routes given in meters.
Similarly the third line has n space 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
2 20 5
10 15
10 15
2 20 5
10 10
10 10
0 0 0
Sample Output
50
0
问题链接:https://vjudge.net/contest/279637#problem/H
问题简述:有上午路线和下午路线各n个。分配每个司机早上和晚上的路,给出路线的时间,司机开车的时间如果大于d,设超时的部分为k,则要付加班费k*r。求付最少的加班费是多少,
问题分析:先对早上的路和晚上的路进行排序,路线搭配的方案是第一个司机取一个最大的和一个最小的路线,第二个司机取次大的和次小的路线,依次类推。
程序说明:贪心算法
AC通过的C++程序如下:

include

include

include

using namespace std;
int c1(int a, int b)
{
return a < b;
}
int c2(int a, int b)
{
return a > b;
}
int main()
{
int n, d, r, a[110], b[110];
while (cin>>n>>d>>r)
{
if (n == 0 && d == 0 && r == 0)
break;
int s = 0;
for (int i = 0; i < n; i++)
cin>>a[i];
for (int i = 0; i < n; i++)
cin>>b[i];
sort(a, a + n, c1);
sort(b, b + n, c2);
for (int i = 0; i < n; i++)
{
if (a[i] + b[i] > d)
s += (a[i] + b[i] - d)*r;
}
cout << s< }
return 0;
}

你可能感兴趣的:(H - 8 UVA - 11389)