5924: the production schedule (greedy)

description

 

 

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 Ai, Bi. 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.

 

 

 

Entry

 

 

The first row only - data n, the number of products;

N is a next data item in the n A workshop machining time of each desired;

Last n data indicates that this product n B in the processing plant to the respective time.

 

 

 

Export

 

 

Output minimum machining time.

 

 

Sample input

Sample Output

prompt

Problem-solving ideas: A small B just started to wait less time after the last small A to B B to wait for the completion of all completed

Takes the minimum value of each group A and B A set of small sort of group B placed in front of small discharge then later greedy

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int n;
 4 int ans[10005];
 5 struct Node{
 6     int x,y;
 7 }A[10005];
 8 
 9 struct Edg{
10     int time,B1,B2;
11     bool operator<(const Edg&X) const{
12         return time<X.time;
13     }
14 }B[10005];
15 
16 
17 int main()
18 {
19     ios::sync_with_stdio(false);
20     cin>>n;
21     for(int i=1;i<=n;i++) cin>>A[i].x;
22     for(int i=1;i<=n;i++) cin>>A[i].y;
23     for(int i=1;i<=n;i++){
24         int flag;
25         if(A[i].x<A[i].y) flag=0;
26         else flag=1;
27         B[i]={min(A[i].x,A[i].y),i,flag};
28     }
29     sort(B+1,B+1+n);
30     int left=0,right=n+1;
31     for(int i=1;i<=n;i++){
32         if(B[i].B2==1) ans[--right]=B[i].B1;
33         else{
34             ans[++left]=B[i].B1;
35         }
36     }
37     int max1=0,max2=0;
38     for(int i=1;i<=n;i++){
39         max1+=A[ans[i]].x;
40         max2=max(max2,max1)+A[ans[i]].y;
41     }
42     cout << max2 << endl;
43     return 0;
44 }
View Code

 

 

To 100% of the data, 0 <n <10000, all values ​​are all integers.

Guess you like

Origin www.cnblogs.com/qq-1585047819/p/11228333.html