[USACO4.2]工序安排Job Processing
生活随笔
收集整理的這篇文章主要介紹了
[USACO4.2]工序安排Job Processing
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
https://www.luogu.org/problemnew/show/P2751?
題解:貪心?
從前往后貪心,用f[i]記錄第i個零件完成的最小時間
定義一個結(jié)構(gòu)體,記錄
v:每一臺機器完成一個零件的時間和
s:完成下一個零件到達的時間點(會不斷變化)
從前往后每次找完都要排序
最后輸出f[n]
f數(shù)組肯定是f[i]<=f[i+1],為了讓最長時間盡量少,我們就盡量
讓A,B連個步驟的總時間平均,,所以要?從后往前?找 查找的方
式和前面的幾乎一樣,但要記得+f[i]
/* *@Author: STZG *@Language: C++ */ #include <bits/stdc++.h> #include<iostream> #include<algorithm> #include<cstdlib> #include<cstring> #include<cstdio> #include<string> #include<vector> #include<bitset> #include<queue> #include<deque> #include<stack> #include<cmath> #include<list> #include<map> #include<set> //#define DEBUG #define RI register int #define endl "\n" using namespace std; typedef long long ll; //typedef __int128 lll; const int N=1000+10; const int M=100000+10; const int MOD=1e9+7; const double PI = acos(-1.0); const double EXP = 1E-8; const int INF = 0x3f3f3f3f; int s,t,n,m,k,p,l,r,u,v,w,a,b; int ans,cnt,flag,temp,sum; struct node{int s,v;//小根堆,在棧里面自動排序,非常有用,值得記一下 bool operator<(node k)const{if(s>k.s) return true;//這里把完成時間從小到大排一次序 return false;} }x; priority_queue<node> q;//棧,想學貪心必先學棧 int f[N];//記錄第i個零件完成的最小時間 int main() { #ifdef DEBUGfreopen("input.in", "r", stdin);//freopen("output.out", "w", stdout); #endif//ios::sync_with_stdio(false);//cin.tie(0);//cout.tie(0);//scanf("%d",&t);//int T=0;scanf("%d%d%d",&n,&a,&b);for(int i=1;i<=a;i++){scanf("%d",&x.v); x.s=x.v;//這里本來可以為0,但是為了讓排序方便很多//而且這樣可以避免很多不必要的情況發(fā)生 //比如說某一臺機器運轉(zhuǎn)時間比另一臺高出了很多 q.push(x);//放進棧 }for(int i=1;i<=n;i++){//從前往后 x=q.top();//取出最小值 q.pop();f[i]=x.s;//記錄 x.s=x.s+x.v;//為下一個做準備 q.push(x);//繼續(xù)放進棧里面排序 }while(!q.empty()) q.pop();//找完了A,再來找B,所以得把q全部彈出去 for(int i=1;i<=b;i++){scanf("%d",&x.v);//和上面幾乎一樣的操作 x.s=x.v;q.push(x);}int t=0;//t記錄用時最長的那個 for(int i=n;i>=1;i--){//從后往前 x=q.top();q.pop();t=max(t,x.s+f[i]);//記得+f[i],x不用自己加f[i] x.s=x.s+x.v;q.push(x);}printf("%d %d\n",f[n],t);//輸出 #ifdef DEBUGprintf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC); #endif//cout << "Hello world!" << endl;return 0; }?
總結(jié)
以上是生活随笔為你收集整理的[USACO4.2]工序安排Job Processing的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [USACO4.2]草地排水Draina
- 下一篇: 魔术球问题