码迷,mamicode.com
首页 > 其他好文 > 详细

General Problem Solving Techniques [Intermediate-1]~G - The Bus Driver Problem

时间:2015-10-09 22:46:55      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:

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

解题思路:有n个司机,每个司机都有相同的路程分配量,且每个司机都要上午和下午各开一次车。要求怎样分配才能使司机的加班费用最少,可以考虑贪心,平均分配。使得每个司机都能开一次长路程和短路程(相对情况下)。

程序代码:

#include"stdio.h"
#include"algorithm"
#include"math.h"
using namespace std;
const int N=110;
int a[N],b[N];
int cmp(int x,int y)
{
    return x>y;
}
int main()
{
    int n,d,r,i;
    while(scanf("%d%d%d",&n,&d,&r)&&n&&d&&r)
    {
        fill(a,a+n,0);
        fill(b,b+n,0);
        for(i=0;i<n;i++)
            scanf("%d",&a[i]);
        for(i=0;i<n;i++)
            scanf("%d",&b[i]);
        sort(a,a+n);
        sort(b,b+n,cmp);
        int sum=0;
        for(i=0;i<n;i++)
            sum=sum+max(0,a[i]+b[i]-d);
        printf("%d\n",sum*r);
    }
    return 0;
}

General Problem Solving Techniques [Intermediate-1]~G - The Bus Driver Problem

标签:

原文地址:http://www.cnblogs.com/chenchunhui/p/4865225.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!