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<iostream>

include <stdio.h>

include <algorithm>

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<<endl;
}
return 0;
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在C语言中,五种基本数据类型存储空间长度的排列顺序是: A)char B)char=int<=float C)ch...
    夏天再来阅读 8,707评论 0 2
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,136评论 0 10
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 6,146评论 0 2
  • 亲爱的女子: 见信好! 30天的训练已经结束,感觉怎么样? 原本训练开始的时候你都没有明确目标,现在结束了,有收获...
    上官草字头阅读 2,875评论 0 3
  • 看了几天电视剧平凡的世界,小说和剧还是不一样,但各有各的好处,跟随双水村一家人及远亲近邻重温那个年代,也想起自己家...
    轻风不相识阅读 1,339评论 0 0