UVA 11389 The Bus Driver Problem 贪心水题

题目链接:UVA - 11389

题意描述:有n个司机,n个早班路线和n个晚班路线,给每个司机安排一个早班路线和一个晚班路线,使得每个早班路线和晚班路线只属于一个司机。如果一个司机早班和晚班总的驾驶时间超过d,那么超出的时间按每小时r元付给司机。求最小的费用。

算法分析:一枚贪心的小水题。对早班路线的时间按照从大到小排序,对晚班路线的时间按照从小到大排序,然后就可以了。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<algorithm>
 7 #define inf 0x7fffffff
 8 using namespace std;
 9 const int maxn=100+10;
10 
11 int n,d,r,an[maxn],bn[maxn];
12 
13 int cmp(int i,int j) {return i>j; }
14 
15 int main()
16 {
17     while (scanf("%d%d%d",&n,&d,&r)!=EOF)
18     {
19         if (n==0 && d==0 && r==0) break;
20         for (int i=0 ;i<n ;i++) scanf("%d",&an[i]);
21         for (int i=0 ;i<n ;i++) scanf("%d",&bn[i]);
22         int ans=0;
23         sort(an,an+n);
24         sort(bn,bn+n,cmp);
25         for (int i=0 ;i<n ;i++) if (an[i]+bn[i]>d) ans+=(an[i]+bn[i]-d)*r;
26         printf("%d
",ans);
27     }
28     return 0;
29 }
原文地址:https://www.cnblogs.com/huangxf/p/4404923.html