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

思路:
最优化的问题,由于最近一直在看DP,一开始就好不犹豫的选择了DP,但并没有找到什么合适的解
那么剩下的选择就是贪心了,策略很简单,就是一个数组从小到大排,另一个从大到小排,然后逐项相加就OK
关键是策略的证明————
(1)(凭什么让我和你匹配:与其他可能性的比较)a中的最小和b中的最大匹配,如果二者的和没有超过T那自然最好;而如果超过T了,那么a中其他的项与其匹配自然也会超过T。
(2)(反证法,如果不和你匹配)若有两组(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 <cstdio>
#include <algorithm>
using namespace std; int N,T;
int A[],B[];
int g[];
int tmp;
int ans; int cmp(int a,int b)
{
return a > b;
} int main()
{
while(~scanf("%d%d",&N,&T))
{
ans = ;
for(int i = ;i <= N;i++)
scanf("%d",&A[i]);
for(int i = ;i <= N;i++)
scanf("%d",&B[i]);
sort(A+,A+N+);
sort(B+,B+N+,cmp);
for(int i = ;i <= N;i++) {
tmp = A[i]+B[i];
g[i] = max(,tmp-T);
ans += g[i];
}
printf("%d\n",ans);
}
return ;
}
 

HDU-3661(贪心)的更多相关文章

  1. hdu 3661 Assignments (贪心)

    Assignments Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. HDU 3661 Assignments (水题,贪心)

    题意:n个工人,有n件工作a,n件工作b,每个工人干一件a和一件b,a[i] ,b[i]代表工作时间,如果a[i]+b[j]>t,则老板要额外付钱a[i]+b[j]-t;现在要求老板付钱最少: ...

  3. Hdu 5289-Assignment 贪心,ST表

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Assignment Time Limit: 4000/2000 MS (Java/Others) ...

  4. hdu 4803 贪心/思维题

    http://acm.hdu.edu.cn/showproblem.php?pid=4803 话说C++还卡精度么?  G++  AC  C++ WA 我自己的贪心策略错了 -- 就是尽量下键,然后上 ...

  5. hdu 1735(贪心) 统计字数

    戳我穿越:http://acm.hdu.edu.cn/showproblem.php?pid=1735 对于贪心,二分,枚举等基础一定要掌握的很牢,要一步一个脚印走踏实 这是道贪心的题目,要有贪心的意 ...

  6. hdu 4974 贪心

    http://acm.hdu.edu.cn/showproblem.php?pid=4974 n个人进行选秀,有一个人做裁判,每次有两人进行对决,裁判可以选择为两人打分,可以同时加上1分,或者单独为一 ...

  7. hdu 4982 贪心构造序列

    http://acm.hdu.edu.cn/showproblem.php?pid=4982 给定n和k,求一个包含k个不相同正整数的集合,要求元素之和为n,并且其中k-1的元素的和为完全平方数 枚举 ...

  8. HDU 2307 贪心之活动安排问题

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2037 今年暑假不AC Time Limit: 2000/1000 MS (Java/Others)  ...

  9. HDU 1052 贪心+dp

    http://acm.hdu.edu.cn/showproblem.php?pid=1052 Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS ...

  10. HDU 2111 Saving HDU【贪心】

    解题思路:排序后贪心,和fatmouse's  trade 类似 Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: ...

随机推荐

  1. [Angular 2] Handling Clicks and Intervals Together with Merge

    Observable.merge allows you take two different source streams and use either one of them to make cha ...

  2. C++之static

    一.静态全局变量和非静态全局变量 1. 隐藏作用 比较非静态全局变量和静态(static)全局变量: 对于多个文件的代码,非静态全局变量和函数都是全局可见的.举例如下: a.c中: #include& ...

  3. sae storage 使用uploadify插件进行文件批量上传

    uploadify插件在文件上传方面还是很不错的,这不我需要往sae 的storage上上传文件,就用了它.下面我就分享一下如何实现的吧.我们先到官网下载最新的uploadify最新的插件包.在页面中 ...

  4. thinkphp3.2.3 成功对接支付宝接口

    一.首先下载支付宝官方接口,下载地址: https://b.alipay.com/order/productDetail.htm?productId=2012111200373124&tabI ...

  5. 动态图片 gif

    简介 android不推荐使用gif图片,一般都是png的,对于gif的图片解析比较消耗资源,但是对于一些动态gif图片的播放,如果比较小的话还是可以的,要是大的话,建议还是把gif图片转换成一帧一帧 ...

  6. javascript !!的作用是把一个其他类型的变量转成的bool类型

    !!的作用是把一个其他类型的变量转成的bool类型

  7. Avi视频生成缩略图时,提示“尝试读取或写入受保护的内存。这通常指示其他内存已损坏”

    需求:录制Avi格式视频成功后,使用DirectShow生成缩略图,由于视频录制时,宽高分辨率可调节,所以有些情况下,生成缩略图会抛出异常“尝试读取或写入受保护的内存.这通常指示其他内存已损坏”. 异 ...

  8. 3.RxJava详解

    一.RxJava 到底是什么 异步(取代AsyncTask/Handler/XXX/...?)   二.RxJava 好在哪   简洁(逻辑的简洁,.一步一走)   举例: 题目:将文件夹中的图片都取 ...

  9. c - 统计字符串"字母,空格,数字,其他字符"的个数和行数.

    #include <stdio.h> #include <ctype.h> using namespace std; /* 题目:输入一行字符,分别统计出其中英文字母.空格.数 ...

  10. angularjs使用directive实现分页组件

    闲来没事,分享下项目中自己写的分页组件.来不及了,直接上车. 效果: 输入框可任意输入,并会自动提交到该页 html: <ul class="page clearfix"&g ...