Untitled

mail@pastecode.io avatar
unknown
plain_text
21 days ago
2.3 kB
2
Indexable
Never
#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<501;i++) {
		line[i] = 0;
		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;
		eq[products[*it].eqId] = 1;
		line[products[*it].line] = 1;
		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;
					products[idx].endTime = time + products[idx].workTime;
					eq[products[idx].eqId] = 1;
					line[products[idx].line] = products[idx].id;	
					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;
	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[pId] == 0) return 0;
	return products[mpProd[pId]].status;

}
Leave a Comment