Untitled

mail@pastecode.io avatar
unknown
plain_text
10 days ago
3.0 kB
3
Indexable
Never
#include <set>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int fee;
struct stone {
    int id;
    int cost;
    int content;
    int type;
    
    stone(int id, int type, int cost, int content) {
        this->id = id;
        this->cost = cost;
        this->content = content;
        this->type = type;
    }
    
    bool operator<(const stone& other) const {
        return cost < other.cost;
    }
};
stone del0 = stone(0, 0, 0, 0);
stone del1 = stone(0, 0, 0, 0);
stone del2 = stone(0, 0, 0, 0);

set<stone> table[3][3];

struct Result {
    int mCost;
    int mContent;
};

void init(int mShipFee) {
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            table[i][j].clear();
        }
    }
    fee = mShipFee;
}

int gather(int mMineId, int mType, int mCost, int mContent) {
    stone tmp = stone(mMineId, mType, mCost, mContent);
    table[mMineId][mType].insert(tmp);
    table[2][mType].insert(tmp);  // thêm vào hàng "tổng hợp"
    return 0;
}

Result mix(int mBudget) {
    int row = -1;
    Result res = {0, 0};
    int contentRes = -1;
    int costRes = 99999999;
    int contentCur, costCur;
    
    for (int i = 0; i < 3; i++) {
        auto stone0 = table[i][0].begin();
        auto stone1 = table[i][1].begin();
        auto stone2 = table[i][2].begin();
        
        while (stone0 != table[i][0].end() && stone1 != table[i][1].end() && stone2 != table[i][2].end()) {
            if (i != 2) {
                costCur = stone0->cost + stone1->cost + stone2->cost + fee;
            } else {
                costCur = stone0->cost + stone1->cost + stone2->cost + 2 * fee;
            }
            
            if (costCur <= mBudget) {
                contentCur = min({stone0->content, stone1->content, stone2->content});  // sửa min thành initializer list
                
                // Kiểm tra điều kiện tốt hơn
                if (contentCur > contentRes || (contentCur == contentRes && costCur < costRes)) {
                    contentRes = contentCur;
                    costRes = costCur;
                    del0 = *stone0;
                    del1 = *stone1;
                    del2 = *stone2;
                    row = i;
                }
            }
            
            // Cập nhật con trỏ dựa vào giá trị contentCur
            if (stone0->content == contentCur) stone0++;
            else if (stone1->content == contentCur) stone1++;
            else stone2++;
        }
    }

    if (row != -1) {
        table[row][0].erase(del0);
        table[row][1].erase(del1);
        table[row][2].erase(del2);
        table[2][0].erase(del0);  // Xóa cả trong bảng tổng hợp
        table[2][1].erase(del1);
        table[2][2].erase(del2);
        
        res.mCost = costRes;
        res.mContent = contentRes;
    } else {
        res.mCost = 0;
        res.mContent = 0;
    }

    return res;
}
Leave a Comment