寫一個程序來模擬操做系統的進程調度。假設該系統只有一個CPU,每個進程的到達時間,執行時間和運行優先級都是已知的。其中運行優先級用天然數表示,數字越大,則優先級越高。node
若是一個進程到達的時候CPU是空閒的,則它會一直佔用CPU直到該進程結束。除非在這個過程當中,有一個比它優先級高的進程要運行。在這種狀況下,這個新的(優先級更高的)進程會佔用CPU,而老的只有等待。c++
若是一個進程到達時,CPU正在處理一個比它優先級高或優先級相同的進程,則這個(新到達的)進程必須等待。ide
一旦CPU空閒,若是此時有進程在等待,則選擇優先級最高的先運行。若是有多個優先級最高的進程,則選擇到達時間最先的。spa
輸入格式:操作系統
輸入包含若干行,每一行有四個天然數(均不超過10^8),分別是進程號,到達時間,執行時間和優先級。不一樣進程有不一樣的編號,不會有兩個相同優先級的進程同時到達。輸入數據已經按到達時間從小到大排序。輸入數據保證在任什麼時候候,等待隊列中的進程不超過15000個。debug
輸出格式:rest
按照進程結束的時間輸出每一個進程的進程號和結束時間。排序
1 1 5 3 2 10 5 1 3 12 7 2 4 20 2 3 5 21 9 4 6 22 2 4 7 23 5 2 8 24 2 4
1 6 3 19 5 30 6 32 8 34 4 35 7 40 2 42
好惡心一模擬題 一開始好不容易作完 發現每一個任務能夠分時間完成
debug無能
#include<bits/stdc++.h> using namespace std; //input by bxd #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define repp(i,a,b) for(int i=(a);i>=(b);--i) #define RI(n) scanf("%d",&(n)) #define RII(n,m) scanf("%d%d",&n,&m) #define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k) #define RS(s) scanf("%s",s); #define ll long long #define pb push_back #define REP(i,N) for(int i=0;i<(N);i++) #define CLR(A,v) memset(A,v,sizeof A) ////////////////////////////////// #define inf 0x3f3f3f3f const int N=200000+5; struct node { int id,s,d,rank1; bool operator< (const node & b)const { return rank1<b.rank1||rank1==b.rank1&&s>b.s; } }s[N]; int main() { priority_queue<node>q; node temp; int cnt=0; while(scanf("%d%d%d%d",&temp.id,&temp.s,&temp.d,&temp.rank1)==4) { s[++cnt]=temp; } int cur=1; int ti=s[1].s; while(cur<=cnt) { while(q.empty()) q.push(s[cur]); ti=max(ti,q.top().s); while(max(ti,q.top().s)+q.top().d>s[cur+1].s&&cur+1<=cnt) { q.push(s[++cur]); } ti=max(ti,q.top().s); ti+=q.top().d; printf("%d %d\n",q.top().id,ti); q.pop(); if(q.empty())cur++; } while(!q.empty()) { ti=max(ti,q.top().s); ti+=q.top().d; printf("%d %d\n",q.top().id,ti); q.pop(); } return 0; }
參考了大佬 學不來隊列
#include<bits/stdc++.h> using namespace std; //input by bxd #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define repp(i,a,b) for(int i=(a);i>=(b);--i) #define RI(n) scanf("%d",&(n)) #define RII(n,m) scanf("%d%d",&n,&m) #define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k) #define RS(s) scanf("%s",s); #define ll long long #define pb push_back #define REP(i,N) for(int i=0;i<(N);i++) #define CLR(A,v) memset(A,v,sizeof A) ////////////////////////////////// #define inf 0x3f3f3f3f const int N=2000000+5; struct node { int id,s,d,rank1; bool operator< (const node & b)const { return rank1<b.rank1||rank1==b.rank1&&s>b.s; } }s[N]; int main() { priority_queue<node>q; node temp; int cnt=0; while(scanf("%d%d%d%d",&temp.id,&temp.s,&temp.d,&temp.rank1)==4) { s[++cnt]=temp; } int rest=cnt; s[++cnt].s=inf; int cur=1; int ti; while(rest!=0) { if(q.empty())q.push(s[cur]),ti=s[cur].s,cur++; node temp=q.top();q.pop(); int lasttime=ti; ti=min( s[cur].s,lasttime+temp.d ); if(ti==lasttime+temp.d) printf("%d %d\n",temp.id,ti),rest--; else temp.d-=ti-lasttime,q.push(temp); if(ti==s[cur].s)q.push(s[cur]),cur++; } return 0; }