zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    git push 报错:missing Change-Id in commit message footer
    script命令录屏
    dubbo.xsd
    常规项目用到的jar包之maven的pom.xml
    WebSocket Demo
    对程序员有帮助的站点集锦
    java之finally的用法
    Java 中的四种引用
    字符串类型的对象与引用及字符串常量池详解
    如何掌握一项新的技能?
  • 原文地址:https://www.cnblogs.com/chenchunhui/p/4865225.html
Copyright © 2011-2022 走看看