【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 ...
随机推荐
- 【转】Odoo开发之:工作流 workflow
在OpenERP中,工作流是管理一组“所做的事情”(与一些数据模型的记录关联)的人为现象.工作流提供了高级别的方式来组织记录要上做的事情. 具体地说,工作流是一个定向的路径,这里节点称为活动并且弧线称 ...
- 天津Uber优步司机奖励政策(12月28日到12月29日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- springboot整合kafka应用
1.kafka在消息传递的使用非常普遍,相对于activemq来说kafka的分布式管理和使用更加灵活. 2.activemq的搭建和使用可以参考: activemq搭建和springmvc的整合:h ...
- spring-boot、spring-data-jpa、hibernate整合
一.Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置. ...
- springboot 读写excel
添加两个坐标: <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</a ...
- #define NULL ((void *)0)引起的风波
1. 看下宏定义的结构体 typedef struct { ]; //CMEI/IMEI ]; //server ]; //CMEI/IMEI } Options; 2. 定义的NULL #defin ...
- PHP调用wsdl接口实例化SoapClient抛出异常
异常:Message:SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://*****?wsdl' : failed to load externa ...
- ElasticSearch-Java-low-level-rest-client官方文档翻译
人肉翻译,非谷歌机翻,部分地方添加了个人的理解,并做了分割,如有错误请在评论指出.转载请指明原链接,尊重个人劳动成果. High-Level-Rest-Client基于Low-Level ...
- 微信小程序—day03
昨日问题 接着上一篇,昨天遇到的scroll-view组件不能滚动的问题. 今天经过调试,发现是由于:图片的实际宽高,大于给image设定的宽高导致的. 解决办法: 减小图片的实际宽高,使之小于ima ...
- 【jmeter进阶之逻辑控制器】
jmeter进阶之逻辑控制器 转载 https://www.cnblogs.com/malinalian/p/10491946.html 常用的逻辑控制器 1,循环控制器:可以设置该控制器内 ...