Untitled

mail@pastecode.io avatar
unknown
plain_text
12 days ago
3.6 kB
2
Indexable
Never
#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;      
} orders[MAX_ORDER];


struct Cutter {
    int numShapes;       
    vector<int> shapeList;    
    bool canCutShape[1001];     
} cutters[101];


struct cmp {
    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;  
            return orders[a].startDay < orders[b].startDay;  
        }
        return orders[a].deadline < orders[b].deadline;  
    }
};


int curDay = 0;
int orderCnt = 0;
int cutterCnt = 0;
set<int, cmp> shapeOrders[1001];  
set<int, cmp> activeOrders;      


void init() {
    curDay = 0;
    orderCnt = 0;
    cutterCnt = 0;
    activeOrders.clear();
    for (int i = 0; i <= 1000; ++i) {
        shapeOrders[i].clear();
    }
    for (int i = 0; i <= 100; ++i) {
        cutters[i].shapeList.clear();
        for(int j=0; j<1000; j++) {
			cutters[i].canCutShape[j] = false;
		}
    }
}


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[cutterCnt].shapeList.push_back(shape);
        cutters[cutterCnt].canCutShape[shape] = true;  
    }
}


void orderCookie(int shape, int daysLeft) {
    orderCnt++;
	orders[orderCnt].orderID = orderCnt;
	orders[orderCnt].shape = shape;
	orders[orderCnt].startDay = curDay;
	orders[orderCnt].deadline = curDay + daysLeft;
	
   
    activeOrders.insert(orderCnt);           
    shapeOrders[shape].insert(orderCnt);   
}


int checkRemain(int shape) {
    return shapeOrders[shape].size();
}


void newDay() {
    curDay++;

    // Xử lý các đơn hàng đến hạn
    while (!activeOrders.empty()) {
        int topOrderID = *activeOrders.begin();
        if (orders[topOrderID].deadline != curDay)
            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 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) {
                if (!shapeOrders[shape].empty()) {
                    int orderIDDone = *shapeOrders[shape].begin();
                    activeOrders.erase(orderIDDone);
                    shapeOrders[shape].erase(orderIDDone);

                }
            }
        }
    }
}
Leave a Comment