Untitled
unknown
plain_text
a year ago
3.9 kB
7
Indexable
#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;
// Xử lý sản phẩm hoàn thành
while (!listProducing.empty()) {
auto it = listProducing.begin();
if (products[*it].end > timeSt) {
break;
}
products[*it].statusPro = 3; // Sản phẩm hoàn thành
equip[products[*it].idEquipInMap] = 0; // Giải phóng thiết bị
lineBusy[products[*it].line] = -1; // Giải phóng line
listProducing.erase(*it);
completed = true; // Đánh dấu là có sản phẩm hoàn thành
}
// Duyệt toàn bộ các line để xử lý sản phẩm mới
if (newr || completed) {
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; // Sản phẩm đang được xử lý
products[idxPro].end = timeSt + products[idxPro].work; // Cập nhật thời gian kết thúc
equip[products[idxPro].idEquipInMap] = 1; // Đánh dấu thiết bị đang sử dụng
lineBusy[products[idxPro].line] = products[idxPro].pID; // Đánh dấu line đang bận
listProducing.insert(idxPro); // Thêm sản phẩm vào danh sách đang sản xuất
}
}
}
timeLine = timeSt; // Cập nhật thời gian hiện tại
}
}
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; // Sản phẩm mới được yêu cầu
products[cntP].end = tStamp; // Thời gian bắt đầu
mapProduct[pId] = cntP; // Cập nhật bản đồ sản phẩm
if (mapEquip[eId] == 0) {
cntE++;
mapEquip[eId] = cntE; // Cập nhật bản đồ thiết bị
}
products[cntP].idEquipInMap = mapEquip[eId]; // Gán thiết bị vào sản phẩm
listLine[mLine].push(cntP); // Thêm sản phẩm vào hàng đợi line
duyetSP(tStamp, true); // Cập nhật tình trạng sản phẩm tại thời điểm tStamp
return lineBusy[mLine]; // Trả về trạng thái của line
}
int status(int tStamp, int pId) {
for (int i = timeLine; i <= tStamp; i++) {
duyetSP(i, false);
}
if (mapProduct[pId] == 0) {
return 0; // Sản phẩm không tồn tại
}
return products[mapProduct[pId]].statusPro; // Trả về trạng thái sản phẩm
}
Editor is loading...
Leave a Comment