[Problem solution] Processing production scheduling

Title source: loj

Title description

A factory received an order for a product. This product was processed in the A and B workshops, and must be processed in the A workshop before being processed in the B workshop.

The processing time for a certain product in workshop A and workshop B are respectively. How to arrange the processing sequence of this product to minimize the total processing time.

The processing time mentioned here refers to the time from the beginning of processing the first product to the completion of all the products in the A and B workshops.

Input format

The first line is only one piece of data, indicating the quantity of the product;

The next data represents the time required for this product to be processed in workshop A;

The last piece of data represents the time required for this product to be processed in workshop B.

Output format

One data in the first line indicates the minimum processing time;

The second line is a processing sequence with minimum processing time.

Sample input

5
3 5 8 7 10
6 2 1 4 9

Sample output

34
1 5 4 2 3

Data range and tips

For 100% data, 0<n<1000, all values ​​are integers.

Ideas

Seeking a processing sequence to minimize the total processing time is to minimize the idle time of the machine. Once the A workshop starts processing, the A workshop will continue to work. The key is that the B workshop may have to wait for the A workshop during the processing. Obviously, when the first product is processed in workshop A, workshop B must wait, and when the last product is processed in workshop B, workshop A is also waiting for the completion of workshop B.

It can be boldly guessed that in order to minimize the idle time in the workshop, the products with the shortest processing time in the A workshop must be processed first, so that the B workshop can start processing in the shortest free time; the shortest processing time in the B workshop The products are placed in the final processing, so that workshop A can wait for the completion of workshop B in the shortest time.

Therefore, we should give priority to the products with short processing time on the A workshop, and the products with the short processing time on the B frame are ranked behind.

So what we need to do is:
(1) Give priority to the parts with the shortest processing time in workshop A, so that workshop B can start processing at the fastest speed
(2) put the products with the shortest processing time in workshop B Put it in the final processing, so as to minimize the idle time of the last workshop A

code

#include<bits/stdc++.h>
using namespace std;
const int N=1010;
int n,a[N],b[N],order[N];
struct node{
    
    
	int time,belong,id;
}c[N];
void init()
{
    
    
	scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%d",&a[i]);
	for (int i=1;i<=n;i++)
	{
    
    
		scanf("%d",&b[i]);
		c[i].id=i;
		if (a[i]<b[i]) //在A上加工的时间更短 
		{
    
    
			c[i].time=a[i];
			c[i].belong=0;
		}
		else //在B上加工的时间更短 
		{
    
    
			c[i].time=b[i];
			c[i].belong=1;
		}
	}
	return;
}

int cmp(node x,node y) {
    
     return x.time<y.time; }

void make_order()
{
    
    
	int cnt1=1,cnt2=n;
	sort(c+1,c+1+n,cmp);
	for (int i=1;i<=n;i++)
	{
    
    
		if (c[i].belong==0) //在A上加工的时间更短,从头开始排 
	    	order[cnt1++]=c[i].id;
	    else //在B上加工的时间更短,从尾开始排 
			order[cnt2--]=c[i].id;
	}
	return;
}

void work()
{
    
    
	int A=0,B=0;//A:A的加工时间 B:B的加工时间 
	for (int i=1;i<=n;i++)
	{
    
    
		A+=a[order[i]];
		if (B<A) B=A;
		B+=b[order[i]];
	}
	cout<<B<<endl;
	for (int i=1;i<=n;i++) printf("%d ",order[i]);
	return;
}
int main()
{
    
    
	init(); 
	make_order();
	work();
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45485187/article/details/102779656