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. List 去处自定义重复对象方法

    list泛型集合去除重复项,对于单一的某个字段非常简单,但是对于一些自定义要求的还需自定义规则.例子代码: /************绑定乘客信息********/ List<RT.Model. ...

  2. VNC 黑屏

    参考资料: http://blog.sina.com.cn/s/blog_57edaf600100serf.html http://www.programgo.com/article/43581885 ...

  3. 去掉app右侧滑动时出现的滚动条

    把plus.webview.currentWebview().setStyle({scrollIndicator:'none'});放在mui.plusReady({});内

  4. 秒味课堂Angular js笔记------$scope.$watch和$scope.$apply

    $scope.$watch(watchFn , watchAction , deepWatch) 其中,watchFn是带有angular表达式或函数字符串: watchAction是一个函数或者表达 ...

  5. 各种乱码,编码问题设置方法整理(UTF-8)

    一.tomcat中文乱码问题 打开tomcat安装目录,在conf文件夹中找到server.xml文件 ,找到   <Connector port="8009" protoc ...

  6. php调试mysql信息。

    print_r(mysql_error());会返回执行myql的成功或者失败的信息.数据库的编码方式是UTF-8.获取手机号,返回的页面的编码是gb2312.需要转换

  7. 用CSS边框图像让你的网站更漂亮

    不久之前,添加一些装饰性元素,例如给网页中的图片添加花哨的边,以及耐心调整CSS文件才能使你的网页看起来不错.然而现在CSS已经做出了改变,用复杂的边框装饰你的网站只需几行代码.这篇文章将告诉你如何做 ...

  8. asp.net微信开发第八篇----永久素材管理

    除了3天就会失效的临时素材外,开发者有时需要永久保存一些素材,届时就可以通过本接口新增永久素材. 最近更新,永久图片素材新增后,将带有URL返回给开发者,开发者可以在腾讯系域名内使用(腾讯系域名外使用 ...

  9. JS 实现 Tab标签切换功能

    Tab标签切换 效果图: HTML部分: <div class="wrap">     <ul id="tag">       < ...

  10. CentOs上搭建git服务器

    CentOs上搭建git服务器 首先安装setuptools wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0 ...