HDU 3661-assignments (Greedy)

Source: Internet
Author: User
Assignments Time Limit: 4000/2000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1554 accepted submission (s): 716


Problem descriptionin 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 cocould get two tasks, one in Type A and one in type B, and, what's more, every worker shoshould 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 shocould pay t-t to him. as a thrity Boss, you want know the minimum total of overtime pay.
 
Inputthere 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 ).
 
Outputfor each test case output the minimum overtime wages by an integer in one line.
 
Sample Input
2 54 23 5
 
Sample output
4. N workers, n work items A and N work items B. A and B, a [I], and B [I] represent the working hours, if a [I] + B [J]> T, the boss will pay an extra fee. A [I] + B [J]-T; now the boss is required to pay at least; I thought it was DP at first, but it was not written after a while... Later, let's look at the greedy problem. Let's talk about my own understanding: A [] is in ascending order, B [] is in descending order, and then corresponding to addition; why is it in ascending order and descending order, I think this is the case: for the absence of a [I], we should combine a [I] as big as possible with B [J] as small as possible, in this way, the boss's payment can be minimized (proof cannot be given .. Sad)
#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>#include <cctype>#include <cmath>#include <cstdlib>#include <vector>#include <queue>#include <set>#include <map>#include <list>#define ll long longusing namespace std;const int INF=1<<27;const int maxn=1010;int a[maxn],b[maxn];int main(){int n,t;while(scanf("%d%d",&n,&t)!=EOF){for(int i=0;i<n;i++)scanf("%d",a+i);sort(a,a+n);for(int i=0;i<n;i++)scanf("%d",b+i);sort(b,b+n);int sum=0;for(int i=0;i<n;i++)sum+=(a[i]+b[n-1-i]>t?a[i]+b[n-1-i]-t:0);printf("%d\n",sum);}    return 0;}

HDU 3661-assignments (Greedy)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.