Johnson算法.
设N1为a<b的作业集合, N2为a>=b的作业集合, 将N1的作业按a非减序排序, N2中的作业按照b非增序排序, 则N1作业接N2作业构成最优顺序.(证明略)
题目:
Description
Space shuttle Endeavour (Endeavour is of British style, and Endeavor is of American style. Why NASA uses the British style? You can find the answer by Baidu or Google) is in danger!
Endeavour meets the same problem as Columbus had met in 2003. When it was sent into space, some material on the surface of the space shuttle is damaged. When it reenters the atmosphere, hot air may destroy the shuttle and make it fall into pieces. NASA says that the only method to save Endeavour is to send another shuttle into space to help repair the Endeavour. But considering the condition of other shuttles available, such as Atlantis and Discovery, scientists think they are not capable to complete the demanding task. NASA has to build another new space shuttle.
There are N parts of this new shuttle need to be built. Because the shuttle will be assembled before all parts are ready. NASA wants to minimize the time to build all parts.
There are two workshops S1 and S2. A part must be processed by these two workshops in order, which means that a part must be processed first in S1 and then in S2. It is known that a workshop cannot process more than one part simultaneously. You job is to calculate the minimum time to build all parts.
Endeavour meets the same problem as Columbus had met in 2003. When it was sent into space, some material on the surface of the space shuttle is damaged. When it reenters the atmosphere, hot air may destroy the shuttle and make it fall into pieces. NASA says that the only method to save Endeavour is to send another shuttle into space to help repair the Endeavour. But considering the condition of other shuttles available, such as Atlantis and Discovery, scientists think they are not capable to complete the demanding task. NASA has to build another new space shuttle.
There are N parts of this new shuttle need to be built. Because the shuttle will be assembled before all parts are ready. NASA wants to minimize the time to build all parts.
There are two workshops S1 and S2. A part must be processed by these two workshops in order, which means that a part must be processed first in S1 and then in S2. It is known that a workshop cannot process more than one part simultaneously. You job is to calculate the minimum time to build all parts.
Input
For every test block in the input, the first line contains an integer N (1 <= N <= 10000), representing the number of parts. Next follow N lines. Each line contains two integers a and b (0 <= a, b <= 100), representing the time consumed in S1, S2 for the corresponding part.
There is a single 0 after the last test block, and you should not process it.
There is a single 0 after the last test block, and you should not process it.
Output
There is only one line for one test block, namely the earliest finishing time.
Sample Input
4 1 2 3 4 5 6 7 8 4 10 1 10 1 1 10 1 10 5 4 5 4 1 30 4 6 30 2 3 6 5 7 1 2 8 2 5 4 3 7 4 4 0
Sample Output
24 23 47 28
代码:
1 #include <iostream> 2 #include <string.h> 3 #include <stdio.h> 4 #include <algorithm> 5 using namespace std; 6 7 const int maxn=10005; 8 struct point 9 { 10 int a; 11 int b; 12 }poin[maxn],poin2[maxn]; 13 14 bool cmp1(point p1,point p2) 15 { 16 if(p1.a!=p2.a) 17 return p1.a<p2.a; 18 return p1.b<p2.b; 19 } 20 21 bool cmp2(point p1,point p2) 22 { 23 if(p1.b!=p2.b) 24 return p1.b>p2.b; 25 return p1.a>p2.a; 26 } 27 28 int main() 29 { 30 int n; 31 //freopen("aa.txt","r",stdin); 32 while(scanf("%d",&n)!=EOF) 33 { 34 if(n==0) 35 break; 36 int x,y; 37 int cnt1=0,cnt2=0; 38 for(int i=0;i<n;i++) 39 { 40 scanf("%d %d",&x,&y); 41 if(x<y) 42 { 43 poin[cnt1].a=x; 44 poin[cnt1].b=y; 45 cnt1++; 46 } 47 else 48 { 49 poin2[cnt2].a=x; 50 poin2[cnt2].b=y; 51 cnt2++; 52 } 53 } 54 sort(poin,poin+cnt1,cmp1); 55 sort(poin2,poin2+cnt2,cmp2); 56 /*cout<<endl; 57 for(int i=0;i<cnt1;i++) 58 cout<<poin[i].a<<" "<<poin[i].b<<endl; 59 for(int i=0;i<cnt2;i++) 60 cout<<poin2[i].a<<" "<<poin2[i].b<<endl; 61 */ 62 int len1=0; 63 int len2=0; 64 for(int i=0;i<cnt1;i++) 65 { 66 len1+=poin[i].a; 67 if(len2<len1) 68 len2=len1+poin[i].b; 69 else 70 len2+=poin[i].b; 71 } 72 for(int i=0;i<cnt2;i++) 73 { 74 len1+=poin2[i].a; 75 if(len2<len1) 76 len2=len1+poin2[i].b; 77 else 78 len2+=poin2[i].b; 79 } 80 printf("%d ",len2); 81 } 82 return 0; 83 }