• General Problem Solving Techniques [Intermediate-1]~G


    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
    ",sum*r);
        }
        return 0;
    }
  • 相关阅读:
    c# CLR无法从 COM 上下文 0x51cd20 转换为 COM 上下文 0x51ce90
    sql语法
    学籍管理系统
    【Android进阶】Android调用WebService的实现
    【Android进阶】自定义控件实现底部扇形展开菜单效果
    华为上机题汇总----java
    卡片游戏(栈和队列)
    18岁生日
    循环多少次?
    Flappy bird源代码(略吊)
  • 原文地址:https://www.cnblogs.com/chenchunhui/p/4865225.html
Copyright © 2020-2023  润新知