The main idea: a factory received n products orders, the N products are in A, b two workshop processing, and must first in a workshop before you can go to the B workshop processing. How to arrange the processing order of the N products, in order to make the total processing time shortest. The processing time referred to here refers to, from the beginning of processing the first product to the last of all products have been in a, b two workshops finished processing time.
Idea: Obviously, if the product starts production, then a production line will certainly not stop running, but the B production line may have to wait for a production line, so that we need B to run the least time. Further analysis, B line of waiting time sources are: when the first product processing time required, the last product on B on the shortest processing time. So we came up with a greedy strategy: remember an array of weights m[i]=min{timea[1],timeb[i]}, sort by the ascending rules of M, and then determine the processing order. Once backwards, if m[i]=a[i], the product will be placed in front of the back, otherwise from the rear. Finally determine the processing time.
Stricter proof in this: the correctness of the greedy strategy of the subject proves
The code is as follows:
#include <iostream>#include <cstdio>#include <algorithm>Using namespace Std;struct node{intTa,tb,w; Char ch; NodeintTa=0,inttb=0,intw=0, Char ch="'): Ta (TA), TB (TB), W (w), CH (ch) {} BOOL operator < (const node &a) Const {returnw<a.w; }};constintmaxn=1005;intNQ[MAXN]; nodem[Maxn];void init () {intI scanf"%d", &n); for(i=1; i<=n;++i) scanf ("%d",&m[I].ta]; for(i=1; i<=n;++i) {scanf ("%d",&m[I].TB];if(m[i].ta<m[I].TB) {m[i].w=m[I].ta;m[I].ch=' A '; }Else{m[i].w=m[I].TB;m[I].ch=' B '; } }Sort(m+1,m+n+1);} void work () {intH=1, T=n; for(intI=1; i<=n;++i) {if(m[i].ch==' A ') {Q[h]=i; h++; }Else{Q[t]=i; t--; } }intSa=0. S =0, i=1, j=0; sa=sb=m[Q[1]].ta; Do{i++; j + +;if(sa+m[Q[i]].ta>=sb+m[Q[j]].TB) {sa+=m[Q[i]].ta; Sb=sa; }Else{sa+=m[Q[i]].ta; sb+=m[Q[j]].TB; } } while(I!=n && j!=n-1);printf("%d", sb+m[Q[n]].TB);}intMain () {init (); Work ();return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Codevs3008 Processing Production Scheduling