Johnson proved law
Here the first two ought, by God, when I ran the same room efforts to study certification process, very
Why so angry person writing a book only put a few irresponsible not read the expression, but when we understand the pull
Nice book written
Ought to this end
Now prove the algorithm:
First, if you do not know what you can see Johnson rule "rise piece" on page 13,
There is also an article prove more difficult to understand (is my purpose in writing this article)
First, that a thing called "exchange argumentation"
"Swap argument" is what is it:
The main exchange of ideas argument is to assume that there is an optimal algorithm and our closest greedy algorithm,
Then exchange algorithm in a two step (or elements), to obtain a new optimal algorithms,
This algorithm is closer to our greedy algorithm for optimal algorithm than the previous one, resulting in conflicts, the original proposition is true.
The following is an example I cited
Suppose now that a and b are two things to do, do first a, b do the time T (a, b), then if in turn is T (b, a).
I <T (b, a) to give defined conditions (algorithm) such that T (a, b), and then run the program in accordance with the defined conditions, OK.
Above do not understand it does not matter
Johnson proved that the law on the use of exchange argument:
S = {their tasks; S} is actually a structure, a is the time to do the task A machine, b is the machine B;
Optimal scheduling now. If this schedule, arranged in front of two jobs are i and j.
Then get on the map
FIG. What do you mean: A machine starts machining tasks in S, the machine is still processing the other components B, B at time t after the machine starts machining tasks in S; tell you can think, A boot from the moment the machine is not stop work, B machine is turned on some time later than the a (to do first because a), end time machine a is constant (a number and all), while the B machine is not, because the B machine to wait a machined finish to do, B machine may appear different lengths of time to wait, so what is our purpose
Purpose is: so that the machine B "ride" shortest time (above paragraph may not understand Duokanjibian).
The first step now (column-wise):
The second step (simplified formula):
The third step (count):
The last formula is a mathematical expression Johnson algorithm
Of course, this question is solved
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int n,ans;
struct Node{
int a,b,c;
}x[1500];
bool cmp(Node x,Node y){
return min(y.b,x.a)<min(x.b,y.a);//这就是Johnson表达式
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&x[i].a);
for(int i=1;i<=n;i++) {scanf("%d",&x[i].b);x[i].c=i;};
sort(x+1,x+n+1,cmp);
int ta=0,tb=0;
for(int i=1;i<=n;i++){
ta+=x[i].a;
tb=max(ta,tb)+x[i].b;
}
printf("%d\n",tb);
for(int i=1;i<=n;i++) printf("%d ",x[i].c);
return 0;
}
Note: Most of the pictures in this article most of the characters are all in this room Gangster cainai produced.