The Bus Driver Problem

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2384

题目大意:

n个司机,设定每位司机上午下午各有一条行驶路线,现给出n条上午行驶路线时长和n条下午行驶路线时长,规定的工作时长为d小时,如果司机工作时长超过d小时,则超出的时间按每小时r塔卡(孟加拉国货币单位)计算加班费用,问:分配行驶路线后老板支付的加班费用最少为多少?

题目分析:

要使加班费用最少,需要让每位司机尽量工作时间靠近d小时,则安排路线只需要安排一条长行驶路线时长和一条短行驶路线时长即可。求出最少的加班时长,再乘上超时的单价即可。

源代码:

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 const int m=100;
 5 int main()
 6 {
 7     int n,d,r,i,j,min,count,a[m],b[m];
 8     while(cin>>n>>d>>r&&n&&d&&r)
 9     {
10         min=0;
11         count=0;
12         for(i=0;i<n;i++)
13             cin>>a[i];//输入给出的上午路线时长
14         for(j=0;j<n;j++)
15             cin>>b[j];//输入给出的下午路线时长
16         sort(a,a+n);//排序
17         sort(b,b+n);//排序
18         for(i=0,j=n-1;i<n,j>=0;i++,j--)
19             if(a[i]+b[j]>d)
20               count+=a[i]+b[j]-d;//计算加班时长
21         min=r*count;//计算最小加班费用
22         cout<<min<<endl;
23     }
24     return 0;
25 }
时间: 2024-08-03 08:10:07

The Bus Driver Problem的相关文章

UVA11389-The Bus Driver Problem

题目链接 题意:n个司机,n个下午路线和n个夜间的行驶时间.每个司机恰好分配到一个下午路线和一个夜间路线.司机行驶如果超过规定的行驶总时间,每个单位时间要多付给司机r元,求最小要给所有司机的加班费. 思路:贪心,将两个时间段的行驶时间排序,然后依次取最大和最小相加减去规定时间.那么超过的时间将最小,加班费也会最小. #include <iostream> #include <cstdio> #include <cstring> #include <algorith

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

In a city there are n bus drivers. Also there are n morning bus routes and n afternoon bus routes withvarious lengths. Each driver is assigned one morning route and one evening route. For any driver, ifhis total route length for a day exceeds d, he h

UVa 11389 (贪心) The Bus Driver Problem

题意: 有司机,下午路线,晚上路线各n个.给每个司机恰好分配一个下午路线和晚上路线. 给出行驶每条路线的时间,如果司机开车时间超过d,则要付加班费d×r. 问如何分配路线才能使加班费最少. 分析: 感觉上是要先排序,然后时间最长的路线配另一个时间最短的路线. 这里就严格证明一下这样贪心的正确性. 以两条路线为例,其他情况都是类似的: 不妨假设:A1≥A2,B1≤B2,水平线代表d 情况一: 如图,司机一要付加班费,司机二不用,如果我们将B1.B2交换: 因为B1≤B2,所以付给司机一的加班费不会

UVA 11389 The Bus Driver Problem 贪心水题

题目链接:UVA - 11389 题意描述:有n个司机,n个早班路线和n个晚班路线,给每个司机安排一个早班路线和一个晚班路线,使得每个早班路线和晚班路线只属于一个司机.如果一个司机早班和晚班总的驾驶时间超过d,那么超出的时间按每小时r元付给司机.求最小的费用. 算法分析:一枚贪心的小水题.对早班路线的时间按照从大到小排序,对晚班路线的时间按照从小到大排序,然后就可以了. 1 #include<iostream> 2 #include<cstdio> 3 #include<cs

【策略】UVa 11389 - The Bus Driver Problem

题意: 有司机,下午路线,晚上路线各n个.给每个司机恰好分配一个下午路线和晚上路线.给出行驶每条路线的时间,如果司机开车时间超过d,则要付加班费d×r.问如何分配路线才能使加班费最少. 虽然代码看起来很水.但一直不理解为什么,找到这位大神的解释与大家参考.http://www.cnblogs.com/AOQNRMGYXLMV/p/4122236.html 不妨假设:A1≥A2,B1≤B2,水平线代表d 情况一: 如图,司机一要付加班费,司机二不用,如果我们将B1.B2交换: 因为B1≤B2,所以

UVa 11389 - The Bus Driver Problem

题目:有n个上午的任务和下午的任务,分配给司机,如果工作总时间超过d,超过的部分要给加班费: 现在让你安排任务,问最小的加班分花费. 分析:贪心.将两个任务分别按递增和递减序排序,每个对应边号的一组即可. 设序列中的两组配对的元素为m1,a1,m2,a2 且 m1≤m2,a1≤a2: 则配对方式<m1,a2>,<m2,a1>优于<m1,a1>,<m2,a2>(浪费的可能更少). 说明:(⊙_⊙). #include <algorithm> #in

大白书第一章

UVA 11292 The Dragon of Loowater(简单贪心) 题意: n条恶龙,m个勇士,用勇士来杀恶龙.一个勇士只能杀一个恶龙.而且勇士只能杀直径不超过自己能力值的恶龙.每个勇士需要支付能力值一样的金币.问杀掉所有恶龙需要的最少金币. 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 20000 + 5; 4 int n, m; 5 int night[maxn], dragon[maxn

【一坨理论AC的题】Orz sxy大佬

1.UVA10891 Game of Sum 2.LA4254 Processor . 3.UVA10905 Children's Game 4.UVA11389 The Bus Driver Problem 5.LA4094 WonderTeam

UVA 11389

UVA 11389 Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description II U C   ONLINE   C ON TEST   2 008 The Bus Driver Problem Input: standard input Output: standard output In a city there are n bus drivers. Also there are n