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. 配置VSFTP服务器

    一.Linux FTP服务器分类:  <1>wu-ftp  <2>proftp=profession ftp  <3>vsftp=very security ftp ...

  2. mount, findmnt,df命令

    ■    The  mount  command gives an overview of all mounted devices. To get this information, the /pro ...

  3. 使用多线程完成Socket

    public class Service { //服务器 public static void main(String[] args) { ServerSocket serverSocket=null ...

  4. hdu 2218

    题意: 切蛋糕问题 水题...... AC代码: #include <iostream> using namespace std; int main() { int a[6],i,n; c ...

  5. java transient关键字和transaction的区别

    transient:表示临时的,不会被持久化,保存进数据库 transaction:表示事务 <div style="background: #fff; color: #0ff;&qu ...

  6. BetWeen和模糊查询

    --区分大小写性能比较低select * from Students where Age >1 and Age <4select * from Students where Age bet ...

  7. Hadoop源码解析之 rpc通信 client到server通信

    rpc是Hadoop分布式底层通信的基础,无论是client和namenode,namenode和datanode,以及yarn新框架之间的通信模式等等都是采用的rpc方式. 下面我们来概要分析一下H ...

  8. 【基础教程】推荐10+必备的 WordPress 常用插件

    1.Akismet Akismet 是 WordPress 官方推荐的一款 WordPress 防垃圾评论插件,也是默认已安装的插件. 2.WP-Postviews 最好的最流行的WordPress浏 ...

  9. php empty和isset的区别

    通过一个例子来解释一下, $id = 0  empty($id) 的值是true isset($id) 也是true. empty 和isset 处理对象无外乎 未定义常量 .0 .空字符串 如果变量 ...

  10. 安全cookie登录状态设计方案

    我们知道web是基于HTTP协议传输的,明文传输是极其危险的,随便哪个抓包工具分析下数据包,就over啦,一个加密的传输过程应该包括两部分,一部分为身份认证,用户鉴别这个用户的真伪:另外一部分为数据加 ...