ACM汽车行程问题求最少的支付钱数

Description

In a city there are n bus drivers. Also there are n morning bus routes & afternoon bus routes with various lengths. Each driver is assigned one morning route & 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 hours at a flat taka / 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 integers nand r, as described above. In the second line, there are space separated integers which are the lengths of the morning routes given in meters. Similarly the third line has 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

Output for Sample Input

2 20 5

10 15

10 15

2 20 5

10 10

10 10

0 0 0

50

0

 

 

Problem setter: Mohammad Mahmudur Rahman

解题思路:首先确定程序循环条件,建立两个数组a和b分别存放早晚路线长度。用sort函数将a数组按从小到大的顺序排列,在重新定义一下sort函数让它可以按从大到小的顺序排列b数组。定义一个Do函数,在这个函数中实现的功能是将a数组的最小数加上b数组的最大值,如此一直加下去,并且每一次相加后都要判断一次相加的结果有没有大于d,如果超过了路程就要把超过的路程乘上r,否则不用。并且要用一个t来累加需要额外支付的钱的总数。最后由这个函数返回一个需要支付的最少的钱数t。在主函数中调用这个函数来求解需要支付的最少的钱数。

 1 #include <iostream>

 2 #include <stdio.h>

 3 #include <algorithm>

 4 using namespace std;

 5 const int maxn=105;

 6 int a[maxn],b[maxn];

 7 bool fun(int a,int b)

 8 {

 9     return a>b;

10 }

11 int Do(int *a,int *b,int n,int d,int r)

12 {

13     int s,t=0;

14     for(int j=0;j<n;j++)

15     {

16         s=a[j]+b[j];

17         if(s>d)

18             t=t+(s-d)*r;

19     }

20     return t;

21 }

22 int main()

23 {

24     int n,d,r;

25     while(scanf("%d%d%d",&n,&d,&r)&&n&&d&&r)

26     {

27         for(int i=0;i<n;i++)

28             scanf("%d",&a[i]);

29     

30         for(int k=0;k<n;k++)

31             scanf("%d",&b[k]);

32         

33         sort(a,a+n);

34         sort(b,b+n,fun);

35         printf("%d\n",Do(a,b,n,d,r));

36     }

37     return 0;

38 }
View Code

 

 

 

你可能感兴趣的:(ACM)