HDU 3661 Assignments

weixin_34198881發表於2019-01-08

Assignments

http://acm.hdu.edu.cn/showproblem.php?pid=3661

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1205    Accepted Submission(s): 559

Problem Description
In a factory, there are N workers to finish two types of tasks (A and B). Each type has N tasks. Each task of type A needs xi time to finish, and each task of type B needs yj time to finish, now, you, as the boss of the factory, need to make an assignment, which makes sure that every worker could get two tasks, one in type A and one in type B, and, what's more, every worker should have task to work with and every task has to be assigned. However, you need to pay extra money to workers who work over the standard working hours, according to the company's rule. The calculation method is described as follow: if someone’ working hour t is more than the standard working hour T, you should pay t-T to him. As a thrifty boss, you want know the minimum total of overtime pay.
 
Input
There are multiple test cases, in each test case there are 3 lines. First line there are two positive Integers, N (N<=1000) and T (T<=1000), indicating N workers, N task-A and N task-B, standard working hour T. Each of the next two lines has N positive Integers; the first line indicates the needed time for task A1, A2…An (Ai<=1000), and the second line is for B1, B2…Bn (Bi<=1000).
 
Output
For each test case output the minimum Overtime wages by an integer in one line.
 
Sample Input
 
2 5
4 2
3 5
 
Sample Output
 
4
 
Source
 
 
簡單的貪心演算法

題意:(轉)有兩個長度為N(N<=1000)的序列A和B,把兩個序列中的共2N個數分為N組,使得每組中的兩個數分別來自A和B,每組的分數等於max(0,組內兩數之和-t),問所有組的分數之和的最小值。

解法:貪心。將A B排序,A中最大的和B中最小的一組,A中第二大的和B中第二小的一組,以此類推。給出一個簡單的證明:若有兩組(a0,b0), (a1,b1)滿足a0>=a1&&b0>=b1,這兩組的得分為max(a0+b0-t,0)+max(a1+b1-t,0) >= max(a0+b1-t,0)+max(a1+b0-t,0)即(a0,b1),(a1,b0)的得分,所以交換b0 b1之後可以使解更優。

 

#include<iostream>
#include<algorithm>
#include<cstdio>

using namespace std;

int n,t;
int a[1005],b[1005];

int cmp(int a,int b){
    return a>b;
}

int main(){
    while(scanf("%d%d",&n,&t)!=EOF){
        int i;
        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++)
            if(a[i]+b[i]>t)
                sum+=a[i]+b[i]-t;
        printf("%d\n",sum);
    }
    return 0;
}