Untitled
unknown
plain_text
a year ago
4.6 kB
9
Indexable
#include <iostream>
#include <queue>
#include <set>
#include <vector>
#include <unordered_map>
using namespace std;
#define MAX_N 10
#define MAX_ORDER 20000
// Prototype of the function makeCookies
void makeCookies(int cutterID);
// Struct để lưu thông tin về đơn đặt hàng
struct Order {
int orderID; // Số thứ tự của đơn hàng
int shape; // Hình dạng bánh quy
int startDay; // Ngày bắt đầu đặt hàng
int deadline; // Ngày cuối cùng để hoàn thành đơn hàng
} orders[MAX_ORDER + 1];
// Struct để lưu thông tin về máy cắt bánh quy
struct Cutter {
int numShapes; // Số lượng hình dạng mà máy cắt được
vector<int> shapeList; // Danh sách các hình dạng mà máy cắt được
bool canCutShape[1001]; // Mảng kiểm tra máy có thể cắt được hình dạng nào
} cutters[101];
// Hàm so sánh để sắp xếp đơn hàng theo ngày
struct OrderComparator {
bool operator()(int a, int b) {
if (orders[a].deadline == orders[b].deadline) {
if (orders[a].startDay == orders[b].startDay)
return orders[a].orderID < orders[b].orderID; // Ưu tiên đơn hàng có số thứ tự nhỏ hơn
return orders[a].startDay < orders[b].startDay; // Ưu tiên đơn hàng có ngày bắt đầu sớm hơn
}
return orders[a].deadline < orders[b].deadline; // Ưu tiên đơn hàng có deadline sớm hơn
}
};
// Các biến toàn cục
int currentDay = 0;
int orderCount = 0;
int cutterCount = 0;
set<int, OrderComparator> shapeOrders[1001]; // Các đơn hàng theo từng hình dạng
set<int, OrderComparator> activeOrders; // Danh sách các đơn hàng đang chờ xử lý
// Khởi tạo hệ thống
void init() {
currentDay = 0;
orderCount = 0;
cutterCount = 0;
activeOrders.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[]) {
cutterCount++;
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; // Đánh dấu máy có thể cắt hình dạng này
}
}
// Đặt đơn hàng bánh quy
void orderCookie(int shape, int daysLeft) {
orderCount++;
orders[orderCount] = {orderCount, shape, currentDay, currentDay + daysLeft};
activeOrders.insert(orderCount); // Thêm đơn hàng vào danh sách chung
shapeOrders[shape].insert(orderCount); // 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() {
currentDay++;
// Xử lý các đơn hàng đến hạn
while (!activeOrders.empty()) {
int topOrderID = *activeOrders.begin();
if (orders[topOrderID].deadline != currentDay)
break; // Nếu đơn hàng chưa đến hạn, dừng việc xử lý
int shape = orders[topOrderID].shape;
int bestCutter = -1;
int maxSharedShapes = -1;
// Tìm máy cắt có thể cắt được nhiều đơn hàng nhất
for (int i = 1; i <= cutterCount; ++i) {
if (cutters[i].canCutShape[shape]) {
int sharedShapeCount = 0;
for (int shape : cutters[i].shapeList) {
if (!shapeOrders[shape].empty()) {
sharedShapeCount++;
}
}
if (sharedShapeCount > maxSharedShapes) {
maxSharedShapes = sharedShapeCount;
bestCutter = i;
}
}
}
if (bestCutter != -1) {
makeCookies(bestCutter);
// Xóa đơn hàng đã xử lý
for (int shape : cutters[bestCutter].shapeList) {
if (!shapeOrders[shape].empty()) {
int processedOrderID = *shapeOrders[shape].begin();
activeOrders.erase(processedOrderID);
shapeOrders[shape].erase(processedOrderID);
}
}
}
}
}
Editor is loading...
Leave a Comment