BZOJ1727: [Usaco2006 Open]The Milk Queue 挤奶队列

1727: [Usaco2006 Open]The Milk Queue 挤奶队列

Time Limit: 5 Sec   Memory Limit: 64 MB
Submit: 152   Solved: 98
[ Submit][ Status][ Discuss]

Description

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.

每天早晨,约翰的N(1≤N≤25000)头奶牛都排成一列,逐一挤奶.为了提高挤奶的速率,
约翰把整个挤奶过程划分成两道工序,每头牛都得连续地完成这些挤奶工序.奶牛们一个接一个地进入挤奶的牛棚,约翰负责实行第一道工序,第二道工序则让他的好友萝卜帮助完成.并且,如果某头奶牛先于另一头奶牛开始进行第一道工序,那么她开始第二道工序的时间也一定在那一头奶牛之前.    约翰发现,如果奶牛们按某种顺序排队进行挤奶,那么可能会在排队等待上多花很多的时间.比方说,如果约翰要花很长时间才能完成某头奶牛挤奶时的第一道工序,那么萝卜可能会有一段时间没有事做.当煞,如果约翰的工作完成得太快,萝}、面前就会有很多奶牛排起长队.    请你帮助约翰计算一下,如果按照最优的排队方式,最少需要多少时间才能把所有奶牛都挤过奶.对于每头奶牛,我们都知道在她身上完成第一道工序所需的时间Ai,以及完成第二道工序的时间Bi.   1≤Ai,Bi≤20000.

Input

* 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.

第1行一个整数N.接下来N行,每行两个整数表示第i头牛的Ai,Bi值.

Output

* Line 1: The minimum possible time it takes to milk all the cows, if we order them optimally.

    输出按照最优方案排队后,最少需要多少时间才能完成对所有奶牛的挤奶.

Sample Input

3
2 2
7 4
3 5

Sample Output

16

HINT

   把奶牛们按照312的顺序排队,这样挤奶总共花费16个单位时间.

题解:

对于两头牛a,b;
满足下面条件时a应该在b之前:
max(A1+B1+B2, A1+A2+B2)<=max(A2+B2+B1, A2+A1+B1)


化简:
max(A2, B1)+A1+B2<=max(A1, B2)+A2+B1
max(A2, B1)-A2-B1<=max(A1, B2)-A1-B2
-min(A2, B1)<=-min(A1, B2)
min(A1, B2)<=min(A2, B1)


用上面这个式子来对牛进行排序再模拟就可以了
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=25005;
struct cow
{int a,b;}s[N];
int n;
bool cmp(cow u,cow v)
{return min(u.a,v.b)<min(u.b,v.a);}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d%d",&s[i].a,&s[i].b);
	sort(s+1,s+n+1,cmp);
	//for(int i=1;i<=n;i++) cout<<s[i].a<<' '<<s[i].b<<endl; 
	for(int i=1;i<=n;i++) s[i].a+=s[i-1].a;
	int t=s[1].a;
	for(int i=1;i<=n;i++)
	{
	    if(s[i].a>t) t=s[i].a;
		t+=s[i].b;
	}
	printf("%d",t);
}



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值