Processing and production scheduling problem of cattle off
topic
A factory has received orders for products of n, the n products were processed in two workshops A, B, and must be processed at the plant after the A to B the processing plant.
A product in the i A, B of two vehicles processing time was AiA_iAi, BiB_iBi. How to arrange the order of processing these n products in order to make the shortest total processing time. Processing time is mentioned here means: start from the first product processing to the final product are all in the A, B two finished processing workshops of time.
Enter a description
The first row only - data n, the number of products;
the next data is a time n the n A product processing plant to the respective;
last n data represents the n respective process plant product B the time required.
Output Description
A first data line, represents the minimum processing time
The second row is a minimum processing time of the processing sequence
Problem solution ideas
Reprinted from the idea of ho God blog
The main idea is this: A machine in a short processing time should be given priority task, and in the B machine short processing time task should be at the back.
Code
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
struct node {
int id,x,y;
}a[1001];
struct Node {
int id, num;
}b[1001];
int cmp(Node x, Node y) {
return x.num < y.num;
}
int ans[1001];
int main() {
ll n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].x;
a[i].id = i;
}
for (int i = 1; i <= n; i++) {
cin >> a[i].y;
b[i].num = min(a[i].x, a[i].y);//记录每个产品的最小工序时间
b[i].id = i;
}
sort(b + 1, b + 1 + n, cmp);//按照最小工序时间从小到大排序
int l=1, r=n;
for (int i = 1; i <= n; i++) {
//如果最小工序时间是第一道工序,就将其安排到前面
if (b[i].num == a[b[i].id].x)ans[l++] = b[i].id;
//如果最小工序时间是第二道工序,就将其安排到后面
else ans[r--] = b[i].id;
}
//这样,ans就保存下来加工产品的最优顺序编号
ll sum1=0, sum2=0;
//sum1记录加工完第i个产品的第一道工序后所用的时间
//sum2记录加工完第i个产品的第二道工序后所用的最优时间
for (ll i = 1; i <= n; i++) {
sum1 += a[ans[i]].x;
//还没有加工第i个的第二道程序之前总时间取决于sum1,和sum2的最大值。
sum2 = max(sum1, sum2);
//之后在执行第二道工序
sum2 += a[ans[i]].y;
}
cout << sum2 << endl;
for (ll i = 1; i < n; i++) {
cout << ans[i] << " ";
}
cout << ans[n] << endl;
return 0;
}