【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 ...
随机推荐
- springboot shiro没有注解解决方案
颓废的悠然 springboot shiro开启注释 shiroconfiguration中增加 1 2 3 4 5 6 7 @Bean public AuthorizationAttri ...
- getSteam
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- 基于Kafka的服务端用户行为日志采集
本文来自网易云社区 作者:李勇 背景 随着互联网的不断发展,用户所产生的行为数据被越来越多的网站重视,那么什么是用户行为呢?所谓的用户行为主要由五种元素组成:时间.地点.人物.行为.行为对应的内容.为 ...
- Mybatis JPA 插件简介
前段时间了解到Spring JPA,感觉挺好用,但其依赖于Hibernate,本人看到Hibernate就头大(不是说Hibernate不好哈,而是进阶太难),于是做了一个迷你版的Mybatis JP ...
- 结合BeautifulSoup和hackhttp的爬虫实例
网页页数的改变 headers头不添加
- Qt-第一个QML程序-2-关键代码分析,TEXT,Image,Mouseare
qml语言开始写的时候有点不习惯,后面用的多了感觉很好,很顺手,用于快速搭建项目界面,真的很好. 目前用到的还是比较简单的 隐藏标题栏,而依附任务栏 flags: Qt.Window | Qt.Fra ...
- 各种对list,string操作函数的总结
#encoding=utf-8#reverse,用来反转lista=['aa','bb','cc']a.reverse()print a#['cc', 'bb', 'aa']#不能直接print a. ...
- Unity自带标准资源包中的特效
- post接口_ajax上传
Action() { web_reg_save_param("find_msg", "LB=message\":\"", "RB= ...
- Apache——访问控制
Order 指定执行允许访问规则和拒绝访问规则 Deny 定义拒绝访问列表 Allow 定义允许访问列表 Order allow,deny 先执行允许,再执行拒绝 Order deny,allow ...