BZOJ1727: [Usaco2006 open]the Milk Queue milking queues

Source: Internet
Author: User
1727: [Usaco2006 open]the Milk queue milking queuesTime 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. Effort to streamline the milking process, FJ have designed a two-stage milking process where the line of cows progres Ses through, 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 he trusty sidekick Farmer Rob milks the cows (in The same order) as they is 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 and we might end up with a long queue of cows waiting to enter the Secon D Barn. Farmer John decide on the best possible ordering of cowsING, so and 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) is in the range 1...20,000. Every morning, John's N (1≤n≤25000) cows are lined up and milked one after the other. In order to increase the rate of milking, John divides the milking process into two processes, each of which has to complete the milking process in succession. The cows went into the milking barn one by one, John was in charge of the first procedure, and the second was for his friend, the carrot, to help finish. Also, if a cow starts the first procedure before the other cow, she must start the second process before the cow.      John found that if cows were queued in a certain order, they might spend a lot more time waiting in line. For example, if John takes a long time to complete the first process of milking a cow, the radish may not be able to do it for some time. If John's work is done too quickly, there will be a lot of cows in front of them.      ask you to help John calculate how long it will take to squeeze all the cows out of the milk if the best way to queue is followed. For each cow, we know the time Ai required to complete the first procedure on her, and the time bi.   1≤ai,bi≤20000 to complete the second procedure.Input

* Line 1: A single integer, N.

* Lines 2..1+n:line i+1 contains II space-separated integers a (i) and B (i) for cow I. Line 1th an integer N. The next n lines, two integers per line, represent the Ai,bi value of the first cow. Output

* Line 1:the minimum possible time it takes to milk all the cows, if we order them optimally. After the output is queued to the optimal scheme, the minimum amount of time is required to complete the milking of all cows. Sample Input 3
2 2
7 4
3 5 Sample Output 16
HINT

Line up the cows in the 3,1,2 order, which takes 16 units of time to milk in total.

The following: for two cows A, a, b;
A should precede B if the following conditions are met:
Max (A1+B1+B2, A1+A2+B2) <=max (A2+B2+B1, A2+A1+B1)


Simplification:
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)

You can use the above formula to sort the cows and then simulate them.

#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);
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.