Untitled

mail@pastecode.io avatar
unknown
plain_text
17 days ago
2.9 kB
5
Indexable
Never
#define MAX_LINE 503
#define MAX_EQUIP 503
#define MAX_PRODUCT 30005
#include<unordered_map>
#include<queue>
#include<set>
using namespace std;
struct Product{
    int pID;
    int line;
    int end;
    int work;
    int idEquipInMap;
    int statusPro;
};
Product products[MAX_PRODUCT];
int cntP,cntE;
struct cmp{
    bool operator()(int a, int b){
        if(products[a].end<products[b].end||(products[a].end==products[b].end&&products[a].pID<products[b].pID)){
            return true;
        }
        return false;
    }
};
queue<int>listLine[MAX_LINE];
unordered_map<int,int> mapProduct;
unordered_map<int,int> mapEquip;
set<int,cmp> listProducing;
int equip[MAX_EQUIP];
int lineBusy[MAX_LINE];
int numLine;
int numEquip;
int timeLine;
void init(int L, int M) {
    numLine=L;
    numEquip=M;
    cntP=0;
    cntE=0;
    timeLine=0;
    mapProduct.clear();
    mapEquip.clear();
    listProducing.clear();
    for(int i=0;i<MAX_LINE;i++){
        listLine[i]=queue<int>();
        lineBusy[i]=-1;
        equip[i]=0;
    }
    return;
}
void duyetSP(int timeSt, bool newr){
    bool completed=false;
    while(!listProducing.empty()){
        auto it=listProducing.begin();
        if(products[*it].end>timeSt){
            break;
        }
        products[*it].statusPro=3;
        equip[products[*it].idEquipInMap]=0;
        lineBusy[products[*it].line]=-1;
        listProducing.erase(*it);
        completed=true;
    }
    if(newr==true||completed==true){
    for(int i=0;i<numLine;i++){
        if(!listLine[i].empty()){
            int idxPro=listLine[i].front();
            if(lineBusy[products[idxPro].line]==-1&&equip[products[idxPro].idEquipInMap]==0){
                listLine[i].pop();
                products[idxPro].statusPro=2;
                products[idxPro].end=timeSt+products[idxPro].work;
                equip[products[idxPro].idEquipInMap]=1;
                lineBusy[products[idxPro].line]=products[idxPro].pID;
                listProducing.insert(idxPro);
            }
        }
    }
    timeLine=timeSt;
    }
}
int request(int tStamp, int pId, int mLine, int eId, int mTime) {
    for(int i=timeLine;i<tStamp;i++){
        duyetSP(i,false);
    }
    cntP++;
    products[cntP].pID=pId;
    products[cntP].work=mTime;
    products[cntP].line=mLine;
    products[cntP].statusPro=1;
    products[cntP].end=tStamp;
    mapProduct[pId]=cntP;
    if(mapEquip[eId]==0){
        cntE++;
        mapEquip[eId]=cntE;
    }
    products[cntP].idEquipInMap=mapEquip[eId];
    listLine[mLine].push(cntP);
    duyetSP(tStamp,true);
    return lineBusy[mLine];
}
  
int status(int tStamp, int pId) {
    for(int i=timeLine;i<=tStamp;i++){
        duyetSP(i,false);
    }
    if(mapProduct[pId]==0){
        return 0;
    }
    return products[mapProduct[pId]].statusPro;
}
Leave a Comment