【bzoj1727】[Usaco2006 Open]The Milk Queue 挤奶队列 贪心
题目描述
Every morning, Farmer John's N (1 <= N <= 25,000) cows all line up for milking. In an effort to streamline the milking process, FJ has designed a two-stage milking process where the line of cows progresses through two barns in sequence, with milking taking part sequentially in both barns. Farmer John milks cows one by one as they go through the first barn, and his trusty sidekick Farmer Rob milks the cows (in the same order) as they are released from the first barn and enter the second barn. Unfortunately, Farmer John's insistence that the cows walk through both barns according to a single ordering leads to some inefficiencies. For example, if Farmer John takes too long to milk a particular cow, Farmer Rob might end up sitting idle with nothing to do for some time. On the other hand, if Farmer John works too fast then we might end up with a long queue of cows waiting to enter the second barn. Please help Farmer John decide on the best possible ordering of cows to use for the milking, so that the last cow finishes milking as early as possible. For each cow i we know the time A(i) required for milking in the first barn and the time B(i) required for milking in the second barn. Both A(i) and B(i) are in the range 1...20,000.
输入
* Line 1: A single integer, N.
* Lines 2..1+N: Line i+1 contains two space-separated integers A(i) and B(i) for cow i.
输出
* Line 1: The minimum possible time it takes to milk all the cows, if we order them optimally.
样例输入
3
2 2
7 4
3 5
样例输出
16
提示
把奶牛们按照3,1,2的顺序排队,这样挤奶总共花费16个单位时间.
题解
神贪心
贪心法则:如果min(a1,b2)<min(a2,b1),那么就先选择1再选择2,其余同理。
严格证明什么的就算了(我也不会……)就说说简单想法吧。
如果有1和2,那么先1后2的时间为a1+b2+max(b1,a2),x先2后1的时间为a2+a1+max(b2,a1)。
要想使先1后2比先2后1合适,则应满足a1+b2+max(b1,a2)<a2+b1+max(b2,a1)。
整理得a1+b2-max(a1,b2)<b1+a2-max(b1,a2)。
两个数的和就是这两个数的最大值与最小值之和(这不是废话吗)。
于是a+b=max(a,b)+min(a,b),即a+b-max(a,b)=min(a,b)。
由此化简上式得min(a1,b2)<min(a2,b1)。
这个条件也应该具有单调性(虽然我证不出来……)。
然后按照这个法则排序求解就行了。
#include <cstdio>
#include <algorithm>
using namespace std;
struct data
{
int a , b;
}c[25001];
bool cmp(data x , data y)
{
return min(x.a , y.b) < min(x.b , y.a);
}
int main()
{
int n , i , w = 0 , ans = 0;
scanf("%d" , &n);
for(i = 1 ; i <= n ; i ++ )
scanf("%d%d" , &c[i].a , &c[i].b);
sort(c + 1 , c + n + 1 , cmp);
for(i = 1 ; i <= n ; i ++ )
{
ans += c[i].a;
w = max(c[i].b , w - c[i].a + c[i].b);
}
printf("%d\n" , ans + w);
return 0;
}
【bzoj1727】[Usaco2006 Open]The Milk Queue 挤奶队列 贪心的更多相关文章
- BZOJ1727 [Usaco2006 Open]The Milk Queue 挤奶队列
贪心...我怎么不会QAQ[捂脸熊] 对于1.2两头牛,如果1号牛要排在2号牛前面才能时间更少,则 $$max(A_1 + B_1 + B_2, \ A_1 + A_2 + B_2) \le max( ...
- BZOJ1727:[Usaco2006 Open]The Milk Queue挤奶队列
我对\(Jhonson\)算法的理解:https://www.cnblogs.com/AKMer/p/9863620.html 题目传送门:https://www.lydsy.com/JudgeOnl ...
- Queue 先进先出队列的操作
1.Queue定义 System.Collections.Queue类表示对象的先进先出集合,存储在 Queue(队列) 中的对象在一端插入,从另一端移除. 2.优点 1.能对集合进行顺序处理(先进先 ...
- C++数据结构之Queue(队列)
Queue,队列,和我们日常生活中的队列是同样的规则,"先进先出",从尾入,从首出. Queue,主要有三种基本操作,append(添加元素至队尾):serve(队首元素出列):r ...
- python-Day3-set 集合-counter计数器-默认字典(defaultdict) -可命名元组(namedtuple)-有序字典(orderedDict)-双向队列(deque)--Queue单项队列--深浅拷贝---函数参数
上节内容回顾:C语言为什么比起他语言块,因为C 会把代码变异成机器码Pyhton 的 .pyc文件是什么python 把.py文件编译成的.pyc文件是Python的字节码, 字符串本质是 字符数组, ...
- pyhton中的Queue(队列)
什么是队列? 队列就像是水管子,先进先出,与之相对应的是栈,后进先出. 队列是线程安全的,队列自身有机制可以实现:在同一时刻只有一个线程在对队列进行操作. 存数据,取数据 import Queue q ...
- STL --> queue单向队列
queue单向队列 queue 模板类的定义在<queue>头文件中.与stack 模板类很相似,queue 模板类也需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器 ...
- STL - queue(队列)
Queue简介 queue是队列容器,是一种"先进先出"的容器. queue是简单地装饰deque容器而成为另外的一种容器. #include <queue> queu ...
- Queue<T>队列与Stack<T>堆栈
一.概述: Queue<T>队列,对象的先进先出集合("FIFO").Stack<T>栈,对象的后进先出集合("LIFO"). Queu ...
随机推荐
- 20154327 Exp1 PC平台逆向破解
一.实践目标 1.运行原本不可访问的代码片段 2.强行修改程序执行流 3.以及注入运行任意代码 二.基础知识 1.直接修改程序机器指令,改变程序执行流程 2.通过构造输入参数,造成BOF攻击,改变程序 ...
- BZOJ1303_中位数图_KEY
题目传送门 较水,开两个桶即可. 题目可以理解为,将大于B的数看为1,小于B的数看为-1,将以B这个数为中位数的序列左右分为两半,加起来为0. code: #include <cstdio> ...
- 北京Uber优步司机奖励政策(4月1日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 北京Uber优步司机奖励政策(11月2日~11月8日)
用户组:优步北京人民优步A组(适用于11月2日-11月8日) 滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不 ...
- 成都Uber优步司机奖励政策(1月10日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 1 多任务fork Unix/Linux/Mac
# 注意,fork函数,只在Unix/Linux/Mac上运行,windows不可以 1.如下程序,来模拟“唱歌跳舞”这件事情 #-*- coding:utf-8 -*- import time de ...
- oracle分区表按时间自动创建
表分区是一种思想,分区表示一种技术实现.当表的大小过G的时候可以考虑进行表分区,提高查询效率,均衡IO.oracle分区表是oracle数据库提供的一种表分区的实现形式.表进行分区后,逻辑上仍然是一张 ...
- 打造移动应用与游戏安全防线,腾讯WeTest安全服务全线升级
当移动互联网渗透到千家万户,与工业控制.智慧交通.实时社交.休闲娱乐紧密结合时,应用安全就变得尤为重要. 尤其在网络强相关的APP流行年代,当APP应用客户端上传与获取信息,大多通过接口在服务器双向通 ...
- 通过批处理命令for提取数据
前两天有这么个小需求: 在cmd中运行某测试工具后,会返回一个json结果,其中有一个参数的值每次都变且经常要用,正常情况复制粘贴就好了,但这个值非常长,配上cmd的标记+粘贴的行为,就很酸爽了.然后 ...
- Unity 特殊目录
其他目录 Application.persistentDataPath:webGL平台只能使用这个