UVA 11389

Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

IC   ONLINE   C OTEST   008

The Bus Driver Problem

Input: standard input

Output: standard output

In a city there are n bus drivers. Also there are n morning bus routes & afternoon bus routes with various lengths. Each driver is assigned one morning route & one evening route. For any driver, if his total route length for a day exceeds d, he has to be paid overtime for every hour after the first hours at a flat taka / hour. Your task is to assign one morning route & one evening route to each bus driver so that the total overtime amount that the authority has to pay is minimized.

Input

The first line of each test case has three integers nand r, as described above. In the second line, there are space separated integers which are the lengths of the morning routes given in meters. Similarly the third line has space separated integers denoting the evening route lengths. The lengths are positive integers less than or equal to 10000. The end of input is denoted by a case with three 0 s.

Output

For each test case, print the minimum possible overtime amount that the authority must pay.

Constraints

-           1 ≤ n ≤ 100

-           1 ≤ d ≤ 10000

-           1 ≤ r ≤ 5

Sample Input

Output for Sample Input

2 20 5

10 15

10 15

2 20 5

10 10

10 10

0 0 0

50

0

 

 

 

题解:本题是解决实际问题,有n个上午的任务和下午的任务,分配给司机,如果工作总时间超过d,超过的部分要给加班费;

现在让你安排任务,问最小的加班分花费。

分析:贪心问题。将两个任务分别按递增和递减序排序,每个对应边号的一组即可。

设序列中的两组配对的元素为m1,a1,m2,a2 且 m1≤m2,a1≤a2;

则配对方式<m1,a2>,<m2,a1>优于<m1,a1>,<m2,a2>

代码如下:

  1. #include<iostream>
  2. #include<algorithm>
  3. using namespace std;
  4.  
  5. int a[],b[];
  6.  
  7. int main()
  8. {
  9.  
  10. int i,x,y,z;
  11. while(cin>>x>>y>>z&&x&&y&&z)
  12. {
  13. int s=;
  14. for(i=; i<=x; i++)
  15. {
  16. cin>>a[i];
  17. }
  18. for(int i=; i<=x; i++)
  19. {
  20. cin>>b[i];
  21. }
  22. sort(a,a+x);
  23. sort(b,b+x);
  24. for(i=; i<=x; i++)
  25. {
  26. if(a[i]+b[x+-i]>y)
  27. {
  28. s=(a[i]+b[x-i+]-y)*z;
  29. s+=s;
  30. }
  31. }
  32. cout<<s<<endl;
  33. }
  34. return ;
  35. }

咳咳,其实上面是错的。。虽然可以输出正确结果,但是提交不会AC。

以下是正确的代码,请注意改动的地方!!

  1. #include<iostream>
  2. #include<algorithm>
  3. using namespace std;
  4. int a[],b[];
  5. int main()
  6. {
  7.  
  8. int i,j,k,x,y,z;
  9. while(cin>>x>>y>>z&&x&&y&&z)
  10. {
  11. int s=;
  12. for(i=; i<=x; i++)
  13. {
  14. cin>>a[i];
  15. }
  16. for(int k=; k<=x; k++)
  17. {
  18. cin>>b[k];
  19. }
  20. sort(a,a+x);
  21. sort(b,b+x);
  22. for(int j=; j<=x; j++)
  23. {
  24. if(a[j]+b[x+-j]>y)
  25. {
  26. s=(a[j]+b[x-j+]-y)*z;
  27. s+=s;
  28. }
  29. }
  30. cout<<s<<endl;
  31. }
  32. return ;
  33. }

可以仔细想一想为什么会出现这样的情况

UVA 11389(贪心问题)的更多相关文章

  1. UVa 11389 (贪心) The Bus Driver Problem

    题意: 有司机,下午路线,晚上路线各n个.给每个司机恰好分配一个下午路线和晚上路线. 给出行驶每条路线的时间,如果司机开车时间超过d,则要付加班费d×r. 问如何分配路线才能使加班费最少. 分析: 感 ...

  2. UVA 11389 The Bus Driver Problem 贪心水题

    题目链接:UVA - 11389 题意描述:有n个司机,n个早班路线和n个晚班路线,给每个司机安排一个早班路线和一个晚班路线,使得每个早班路线和晚班路线只属于一个司机.如果一个司机早班和晚班总的驾驶时 ...

  3. 01_传说中的车(Fabled Rooks UVa 11134 贪心问题)

    问题来源:刘汝佳<算法竞赛入门经典--训练指南> P81: 问题描述:你的任务是在n*n(1<=n<=5000)的棋盘上放n辆车,使得任意两辆车不相互攻击,且第i辆车在一个给定 ...

  4. uva 10154 贪心+dp

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  5. UVa 1467 (贪心+暴力) Installations

    题意: 一共有n项服务,每项服务有安装的时间s和截止时间d.对于每项任务,如果有一项超出截止时间,惩罚值为所超出时间的长度.问如何安装才能使惩罚值最大的两个任务的惩罚值之和最小. 分析: 如果是求总惩 ...

  6. UVa 11389 - The Bus Driver Problem 难度:0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  7. Party Games UVA - 1610 贪心

    题目:题目链接 思路:排序后处理到第一个不同的字符,贪心一下就可以了 AC代码: #include <iostream> #include <cstdio> #include ...

  8. UVa 1149 (贪心) Bin Packing

    首先对物品按重量从小到大排序排序. 因为每个背包最多装两个物品,所以直觉上是最轻的和最重的放一起最节省空间. 考虑最轻的物品i和最重的物品j,如果ij可以放在一个包里那就放在一起. 否则的话,j只能自 ...

  9. 【策略】UVa 11389 - The Bus Driver Problem

    题意: 有司机,下午路线,晚上路线各n个.给每个司机恰好分配一个下午路线和晚上路线.给出行驶每条路线的时间,如果司机开车时间超过d,则要付加班费d×r.问如何分配路线才能使加班费最少. 虽然代码看起来 ...

随机推荐

  1. HDU 4800/zoj 3735 Josephina and RPG 2013 长沙现场赛J题

    第一年参加现场赛,比赛的时候就A了这一道,基本全场都A的签到题竟然A不出来,结果题目重现的时候1A,好受打击 ORZ..... 题目链接:http://acm.hdu.edu.cn/showprobl ...

  2. 《JavaScript语言精髓与编程实践》读书笔记一

    受到狗哥书单的影响,看到了豆瓣上的评论,买了这本书,然后囫囵吞枣似地用一个月的时间看完了.回头想想自己做的js项目,感觉都羞愧-什么东西都是拿来尝试了一下就用了,其实有很多写得超级丑的地方,看完这个让 ...

  3. Jenkins 五: 构建Ant项目

    1. 点击“新建”,在“Item名称”栏输入要构建的项目名,比如“Ant_project”,选择“构建一个自由风格的软件项目”,点击“OK”按钮. 2. 找到“源码管理”-> “Subversi ...

  4. hdu 3698 Let the light guide us(线段树优化&简单DP)

    Let the light guide us Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 62768/32768 K (Java/O ...

  5. SVN 基本操作

    SVN基础 一 简介 tortoiseSVN是windows下其中一个非常优秀的SVN客户端工具.通过使用它,我们可以可视化的管理我们的版本库.不过由于它只是一个客户端,所以它不能对版本库进行权限管理 ...

  6. python面向对象【初级篇】

    概述 python支持多种编程范式:面向过程.面向对象.面向切面(装饰器部分)等. 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对 ...

  7. Java 判断一段网络资源是否存在

    package cn.ycmedia.common.utils; import java.io.InputStream; import java.net.URL; import java.net.UR ...

  8. media screen 响应式布局(知识点)

    一.什么是响应式布局? 响应式布局是Ethan Marcotte在2010年5月份提出的一个概念,简而言之,就是一个网站能够兼容多个终端--而不是为每个终端做一个特定的版本.这个概念是为解决移动互联网 ...

  9. ExtJS学习-----------Ext.String,ExtJS对javascript中的String的扩展

    关于ExtJS对javascript中的String的扩展,能够參考其帮助文档,文档下载地址:http://download.csdn.net/detail/z1137730824/7748893 以 ...

  10. IOS Dictionary和Model相互转换

    // // HYBJSONModel.h // Json2ModelDemo // // Created by huangyibiao on 14-9-15. // Copyright (c) 201 ...