題解 P1248 【加工生產排程】
阿新 • • 發佈:2020-12-19
題目
某工廠收到了 n 個產品的訂單,這 n 個產品分別在 A、B 兩個車間加工,並且必須先在 A 車間加工後才可以到 B 車間加工。
某個產品 i 在 A、B 兩車間加工的時間分別為 Ai,Bi
怎樣安排這 n 個產品的加工順序,才能使總的加工時間最短。
這裡所說的加工時間是指:從開始加工第一個產品到最後所有的產品都已在 A、B 兩車間加工完畢的時間
貪心
mind
排序
考慮兩個產品, 在A中加工時間a1,a2,在B中加工的時間為b1,b2
假設先加工產品1的方案較優
先加工1, time: a1+max(b1,a2)+b2
先加工2, time: a2+max(b2,a1)+b1
得到: a1
移項,得到 max(b1,a2)-a2-b1 < max(b2,a1)-b2-a1
手摸一下闊以知道: -min(b1,a2) < -min(b2,a1)
也就是 min(a1,b2)<min(2,b1)
按照這個排序
dalao們都說該式子不具傳遞性
詳見……
怎麼求值??
還是考慮兩個產品
根據影象顯而易見了(from:_ztyqwq)
①
這種情況下 Aj <= Bi,time = Ai + Bi + Bj
②
這種情況下 Aj > Bi, time = Ai + Aj + Bj
綜上:總時間為:Ai
程式碼
/* work by:Ariel */ #include <iostream> #include <cstdio> #include <queue> #include <algorithm> #include <cstring> using namespace std; const int M = 1e4 + 4; typedef long long ll; int read(){ int x = 0,f = 1;char c = getchar(); while(c < '0'||c > '9'){if(c == '-')f = -1;c = getchar();} while(c >= '0'&&c <= '9'){x = x*10 + c - '0';c = getchar();} return f*x; } struct node{ int a,b,num; }work[M]; int ans; bool cmp(node x,node y){ return min(x.a , y.b) < min(y.a , x.b); } int tima,timb; int main() { int n = read(); for(int i = 1;i <= n; i++){ work[i].a = read(); work[i].num = i; } for(int i = 1;i <= n; i++){ work[i].b = read(); } sort(work + 1, work + n + 1, cmp); for(int i = 1;i <= n; i++){ tima += work[i].a; timb = max(tima, timb) + work[i].b; } cout << timb <<"\n"; for (int i = 1; i < n; i++) { cout << work[i].num << " "; } cout << work[n].num; return 0; }