P2278 [HNOI2003]操作系統

1522713-20190823195028383-1060138796.png

模擬

思路挺好想的,就是模擬

當然要按照開始時間和優先度排序,然後在進行模擬

模擬方法就是每次讀入一個數,如果當前結束時間小於下一個的開始時間,那就加入堆

代碼

#include <bits/stdc++.h>
#define maxn 21000 
using namespace std ;
struct dy{
    int num , start , cost , pow ;
    int operator < (const dy &a) const {
        if(pow == a.pow) return start > a.start ;
        else return pow < a.pow ; 
    }
}a ;
int ti , t ;
priority_queue<dy>q ;
int main () {
//  while(cin >> a[++t].num >> a[t].start >> a[t].cost >> a[t].pow) ;
//  sort(a+1,a+1+t,cmp) ;
//  int now = 1 ;
    while(cin >> a.num >> a.start >> a.cost >> a.pow) {
        while(!q.empty() && ti + q.top().cost <= a.start) {
            dy b = q.top() ;
            q.pop() ;
            cout << b.num << " " << ti+b.cost << endl ;
            ti += b.cost ;
        }
        if(!q.empty() ) {
            dy d = q.top() ;
            q.pop() ;
            d.cost = d.cost - a.start + ti ;
            q.push(d) ;
        }
        q.push(a) ;
        ti = a.start ;
    }
    while(!q.empty()) {
        dy d = q.top() ;
        q.pop() ;
        ti += d.cost ;
        cout << d.num << " " << ti << endl ;
    }
    return 0 ;
}

溜了溜了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章