Untitled
unknown
plain_text
a year ago
3.8 kB
17
Indexable
#include <iostream>
#include <queue>
#include <set>
#include <vector>
#include <unordered_map>
using namespace std;
#define MAX_N 10
#define MAX_ORDER 20001
#define MAX_SHAPES 1001
void makeCookies(int cutterID);
struct Order {
int orderID;
int shape;
int startDay;
int deadline;
Order() {}
Order(int id, int shp, int sd, int dl) : orderID(id), shape(shp), startDay(sd), deadline(dl) {}
};
struct Cutter {
int numShapes;
vector<int> shapeList;
bool canCutShape[1001];
} cutters[101];
struct cmp {
bool operator()(const Order& a, const Order& b) const {
if (a.deadline == b.deadline) {
if (a.startDay == b.startDay)
return a.orderID < b.orderID;
return a.startDay < b.startDay;
}
return a.deadline < b.deadline;
}
};
int curDay = 0;
int orderCnt = 0;
int cutterCnt = 0;
set<Order, cmp> shapeOrders[1001]; // Thay đổi kiểu dữ liệu sang Order
set<Order, cmp> listOrder; // Thay đổi activeOrders thành listOrder với kiểu Order
// Khởi tạo hệ thống
void init() {
curDay = 0;
orderCnt = 0;
cutterCnt = 0;
listOrder.clear();
for (int i = 0; i <= 1000; ++i) {
shapeOrders[i].clear();
}
for (int i = 0; i <= 100; ++i) {
cutters[i].shapeList.clear();
fill(begin(cutters[i].canCutShape), end(cutters[i].canCutShape), false);
}
}
// Thêm máy cắt bánh quy mới
void addCookieCutter(int cutterID, int numShapes, int shapeList[]) {
cutterCnt++;
cutters[cutterID].numShapes = numShapes;
for (int i = 0; i < numShapes; ++i) {
int shape = shapeList[i];
cutters[cutterID].shapeList.push_back(shape);
cutters[cutterID].canCutShape[shape] = true;
}
}
// Đặt đơn hàng bánh quy
void orderCookie(int shape, int daysLeft) {
orderCnt++;
Order newOrder(orderCnt, shape, curDay, curDay + daysLeft);
listOrder.insert(newOrder); // Thêm đơn hàng vào danh sách chung
shapeOrders[shape].insert(newOrder); // Thêm vào danh sách theo hình dạng
}
// Kiểm tra số lượng đơn hàng chưa hoàn thành cho một hình dạng
int checkRemain(int shape) {
return shapeOrders[shape].size();
}
// Xử lý khi đến ngày mới
void newDay() {
curDay++;
// Xử lý các đơn hàng đến hạn
while (!listOrder.empty()) {
auto orderTop = listOrder.begin(); // Lấy đơn hàng có hạn sớm nhất
if (orderTop->deadline != curDay)
break; // Nếu đơn hàng chưa đến hạn, dừng việc xử lý
int shape = orderTop->shape;
int bestCutter = -1;
int maxNumShapes = -1;
// Tìm máy cắt có thể cắt được nhiều đơn hàng nhất
for (int i = 1; i <= cutterCnt; ++i) {
if (cutters[i].canCutShape[shape]) {
int numShapes = 0;
for (int shape : cutters[i].shapeList) {
if (!shapeOrders[shape].empty()) {
numShapes++;
}
}
if (numShapes > maxNumShapes) {
maxNumShapes = numShapes;
bestCutter = i;
}
}
}
if (bestCutter != -1) {
makeCookies(bestCutter);
// Xóa đơn hàng đã xử lý
for (int shape : cutters[bestCutter].shapeList) {
while (!shapeOrders[shape].empty()) {
auto orderDone = shapeOrders[shape].begin();
listOrder.erase(*orderDone);
shapeOrders[shape].erase(orderDone);
}
}
}
}
}
Editor is loading...
Leave a Comment