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>

代码如下:

#include<iostream>
#include<algorithm>
using namespace std; int a[],b[]; int main()
{ int i,x,y,z;
while(cin>>x>>y>>z&&x&&y&&z)
{
int s=;
for(i=; i<=x; i++)
{
cin>>a[i];
}
for(int i=; i<=x; i++)
{
cin>>b[i];
}
sort(a,a+x);
sort(b,b+x);
for(i=; i<=x; i++)
{
if(a[i]+b[x+-i]>y)
{
s=(a[i]+b[x-i+]-y)*z;
s+=s;
}
}
cout<<s<<endl;
}
return ;
}

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

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

#include<iostream>
#include<algorithm>
using namespace std;
int a[],b[];
int main()
{ int i,j,k,x,y,z;
while(cin>>x>>y>>z&&x&&y&&z)
{
int s=;
for(i=; i<=x; i++)
{
cin>>a[i];
}
for(int k=; k<=x; k++)
{
cin>>b[k];
}
sort(a,a+x);
sort(b,b+x);
for(int j=; j<=x; j++)
{
if(a[j]+b[x+-j]>y)
{
s=(a[j]+b[x-j+]-y)*z;
s+=s;
}
}
cout<<s<<endl;
}
return ;
}

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

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. poj 3084 最小割

    题目链接:http://poj.org/problem?id=3084 本题主要在构图上,我采用的是把要保护的房间与源点相连,有intruder的与汇点相连,相对麻烦. #include <cs ...

  2. linux内网机器访问外网代理设置squid

    公司一般出于安全考虑, 在同一局域网中只有一台机器可以访问外网,运维进行了整体的限制, 但是在后面的工作中,需要在机器上安装一些软件,及命令,所以其他的机器需要访问外网来简化工作, 但又不能打乱原有运 ...

  3. java中的代理

    package cn.itcast.day3; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationHan ...

  4. 414. Third Maximum Number

    这个题有点坑啊..主要是自己蠢,以为 Integer.MIN_VALUE -1 == -2147483649 public class Solution { public int thirdMax(i ...

  5. ArrayList、LinkedList、HashMap底层实现

    ArrayList 底层的实现就是一个数组(固定大小),当数组长度不够用的时候就会重新开辟一个新的数组,然后将原来的数据拷贝到新的数组内. LinkedList 底层是一个链表,是由java实现的一个 ...

  6. linux网络编程学习笔记之五 -----并发机制与线程�

    进程线程分配方式 简述下常见的进程和线程分配方式:(好吧,我仅仅是举几个样例作为笔记...并发的水太深了,不敢妄谈...) 1.进程线程预分配 简言之,当I/O开销大于计算开销且并发量较大时,为了节省 ...

  7. Android NDK 环境搭建 + 测试例程(转)

    懒得废话一大堆概念,关于ADT.NDK的概念要是你不懂,怎么会搜到这里来?所以你只需要根据下面的步骤来,就可以完成NDK环境搭建了. 步骤:(假设你未安装任何相关开发工具,如果已经安装了,就可以跳过) ...

  8. Android真正意义上的无限轮播Banner

    在android开发的时候,经常会使用到轮播图,对于这种效果,一般情况下,我们都会使用一种叫做ViewPager的来实现. 传统的实现逻辑是自定义一个View继承ViewPager,在适配器中 将co ...

  9. ASP.NET-FineUI开发实践-11

    我用实例项目写了个子父页面传值,算是比较灵活的写法,可以把js提取出来写成包,然后调用,我先一步一步写,为有困难的朋友打个样. 先画个页面: 上面是个查询用的表单,底下是表格,内存分页,用到了VBox ...

  10. 使用<input>标签做了两个按钮, 按钮之间间距如何去掉

    遇到的问题: 使用<input>标签做了两个按钮, 按钮之间有个间距不知道怎么去掉. 如下图: 问题解决: <input>是内联块状元素(inline-block); 内联元素 ...