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. Html5选择图片并及时预览图片

    以往想要实现图片预览基本都是先传至服务器后等返回链接地址才能进行预览,使用Html5选择图片并及时预览图片的代码如下,使用起来更爽了. <!DOCTYPE html> <html l ...

  2. Json序列反序列类型处理帮助类

    Json序列反序列类型处理帮助类. JSON反序列化 JSON序列化 将Json序列化的时间由/Date(1294499956278+0800)转为字符串 将时间字符串转为Json时间 using S ...

  3. 读取文件内容返回List<String>类型

    文件内容格式: string1 string2 String 3 …… 很简单,两句话 String content = new String(Files.readAllBytes(Paths.get ...

  4. GraphViz web版

    http://graphviz-dev.appspot.com/ 用来把dot语言的图画出来,很多地方用dot语言来画图,比如doxygen的类关系,gperftools的分析结果等.

  5. VideoView 视频播放 示例

    介绍 实现的功能: 可播放本地视频或网络视频,可控制播放或暂停 最小化时保存播放位置及播放状态,resume时恢复所有状态: 横竖屏切换时保持切换前的位置及状态 在屏幕上竖直滑动可调节屏幕亮度和音量 ...

  6. python面对对象编程------3:写集合类的三种方法

    写一个集合类的三种方法:wrap,extend,invent 一:包装一个集合类 class Deck: def __init__( self ): self._cards = [card6(r+1, ...

  7. ASP.NET 2.0服务器控件开发的基本概念(转载)

    利用ASP.NET 2.0技术,创建Web自定义服务器控件并不是一件轻松的事情.因为,这需要开发人员了解并能够灵活应用多种Web开发技术,例如,CSS样式表.客户端 脚本语言..NET开发语言.服务器 ...

  8. Navicat:cant create OCI environment.

    一直在使用 Navicat ,这是一个数据库客户端软件,能连接多种不同类型的数据库,给我们的日常的工作带来了不少的便捷. 最近,我在电脑上安装了oracle的客户端ODTwihtODAC121012, ...

  9. (转)ASP.NET里面简单的记住用户名和密码

    using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using S ...

  10. DNN模块开发之利器篇:七种武器

    我们在进行DNN模块开发时经常需要调用Dotnetnuke.dll中的方法函数,模块开发用到DNN的方法函数会让你的开发更加得心应手,下面我们就来介绍一下.   1) PortalModuleBase ...