Untitled

 avatar
unknown
plain_text
6 months ago
2.9 kB
3
Indexable
#include <set>
#include <unordered_map>
#include <queue>
#define MAX_LINE 501
#define MAX_E 501
#define MAX_PROD 20001
using namespace std;

int numLine, numeEq;
struct Product {
    int id;
    int line;
    int eqId;
    int endTime;
    int workTime;
    int status;
};
Product products[MAX_PROD];

struct cmp {
    bool operator() (int a, int b) {
        if (products[a].endTime == products[b].endTime)
            return products[a].id < products[b].id;
        return products[a].endTime < products[b].endTime;
    }
};

unordered_map<int, int> mpProd;
queue<int> listLine[MAX_LINE];
set<int, cmp> listProducing;
int eq[MAX_E], line[MAX_LINE];
int curTime;
int cntCurProd;

void init(int L, int M) {
    curTime = 0;
    numLine = L;
    numeEq = M;
    cntCurProd = 0;
    for (int i = 0; i < MAX_LINE; i++) {
        line[i] = 0;
    }
    for (int i = 0; i < MAX_E; i++) {
        eq[i] = 0;
    }
    mpProd.clear();
    return;
}

void update(int time, bool haveNew) {
    bool haveComplete = false;
    while (!listProducing.empty()) {
        auto it = listProducing.begin();
        if (products[*it].endTime > time) break;

        products[*it].status = 3; // hoàn thành
        eq[products[*it].eqId] = 0; // thả thiết bị
        line[products[*it].line] = 0; // thả line
        listProducing.erase(it);
        haveComplete = true;
    }
    
    if (haveComplete || haveNew) {
        for (int i = 0; i < numLine; i++) {
            if (!listLine[i].empty()) {
                int idx = listLine[i].front();
                if (line[products[idx].line] == 0 && eq[products[idx].eqId] == 0) {
                    listLine[i].pop();
                    products[idx].status = 2; // đang sản xuất
                    products[idx].endTime = time + products[idx].workTime;
                    eq[products[idx].eqId] = 1; // chiếm thiết bị
                    line[products[idx].line] = products[idx].id; // chiếm line
                    listProducing.insert(idx);
                }
            }
        }
    }
    curTime = time;
}

int request(int tStamp, int pId, int mLine, int eId, int mTime) {
    for (int i = curTime; i < tStamp; i++) {
        update(i, false);
    }

    cntCurProd++;
    products[cntCurProd].id = pId;
    products[cntCurProd].line = mLine;
    products[cntCurProd].eqId = eId;
    products[cntCurProd].workTime = mTime;
    products[cntCurProd].status = 1; // đang chờ
    products[cntCurProd].endTime = tStamp + mTime;
    listLine[mLine].push(cntCurProd);
    mpProd[pId] = cntCurProd;
    update(tStamp, true);
    
    return line[mLine];
}

int status(int tStamp, int pId) {
    for (int i = curTime; i < tStamp; i++) {
        update(i, false);
    }
    
    if (mpProd.find(pId) == mpProd.end()) return 0; // kiểm tra xem pId có trong mpProd không
    return products[mpProd[pId]].status;
}
Editor is loading...
Leave a Comment