1 条题解

  • 0
    @ 2025-5-12 8:17:56
    #include <iostream>
    #include <vector>
    #include <string>
    #include <algorithm>
    using namespace std;
    
    struct Ingredient {
        string name;
        int amount;
        int price;
    };
    
    struct Recipe {
        string name;
        vector<string> ingredientNames;
        vector<int> ingredientAmounts;
        int price;
        int timesMixed;
        int recipeIndex;
    };
    
    bool compareRecipes(const Recipe& a, const Recipe& b) {
        if (a.timesMixed != b.timesMixed)
            return a.timesMixed > b.timesMixed;
        return a.recipeIndex < b.recipeIndex;
    }
    
    int main() {
        int scenarios;
        cin >> scenarios;
    
        for (int scenario = 1; scenario <= scenarios; scenario++) {
            // 读取食谱
            int numRecipes;
            cin >> numRecipes;
            vector<Recipe> recipes(numRecipes);
            for (int i = 0; i < numRecipes; i++) {
                Recipe r;
                cin >> r.name;
                int numIngredients;
                cin >> numIngredients;
                r.ingredientNames.resize(numIngredients);
                r.ingredientAmounts.resize(numIngredients);
                for (int j = 0; j < numIngredients; j++) {
                    cin >> r.ingredientNames[j] >> r.ingredientAmounts[j];
                }
                r.timesMixed = 0;
                r.recipeIndex = i;
                recipes[i] = r;
            }
    
            // 读取配料
            int numIngredients;
            cin >> numIngredients;
            vector<Ingredient> ingredients(numIngredients);
            for (int i = 0; i < numIngredients; i++) {
                Ingredient ing;
                cin >> ing.name >> ing.amount >> ing.price;
                ingredients[i] = ing;
            }
    
            // 计算每个鸡尾酒的价格
            for (int i = 0; i < numRecipes; i++) {
                Recipe& r = recipes[i];
                int totalPrice = 0;
                for (int j = 0; j < r.ingredientNames.size(); j++) {
                    string ingName = r.ingredientNames[j];
                    int ingAmount = r.ingredientAmounts[j];
                    for (int k = 0; k < ingredients.size(); k++) {
                        if (ingredients[k].name == ingName) {
                            totalPrice += ingAmount * ingredients[k].price;
                            break;
                        }
                    }
                }
                r.price = totalPrice * 3 + 10000;
            }
    
            // 处理订单
            int lastMixed = -1; // 初始没有混合的鸡尾酒
            int numOrders;
            cin >> numOrders;
            for (int i = 0; i < numOrders; i++) {
                string orderName;
                cin >> orderName;
    
                // 查找订单的鸡尾酒索引
                int startIndex = -1;
                for (int j = 0; j < numRecipes; j++) {
                    if (recipes[j].name == orderName) {
                        startIndex = j;
                        break;
                    }
                }
    
                if (startIndex == -1) continue; // 找不到鸡尾酒,忽略订单
    
                // 尝试混合鸡尾酒
                int current = startIndex;
                bool mixed = false;
                do {
                    // 检查是否是上一个混合的鸡尾酒
                    if (current == lastMixed) {
                        current = (current + 1) % numRecipes;
                        continue;
                    }
    
                    // 检查配料是否足够
                    bool canMix = true;
                    Recipe& r = recipes[current];
                    for (int j = 0; j < r.ingredientNames.size(); j++) {
                        string ingName = r.ingredientNames[j];
                        int ingAmount = r.ingredientAmounts[j];
                        bool found = false;
                        for (int k = 0; k < ingredients.size(); k++) {
                            if (ingredients[k].name == ingName) {
                                if (ingredients[k].amount < ingAmount) {
                                    canMix = false;
                                }
                                found = true;
                                break;
                            }
                        }
                        if (!found || !canMix) {
                            canMix = false;
                            break;
                        }
                    }
    
                    if (canMix) {
                        // 混合鸡尾酒
                        r.timesMixed++;
                        lastMixed = current;
                        // 减少配料库存
                        for (int j = 0; j < r.ingredientNames.size(); j++) {
                            string ingName = r.ingredientNames[j];
                            int ingAmount = r.ingredientAmounts[j];
                            for (int k = 0; k < ingredients.size(); k++) {
                                if (ingredients[k].name == ingName) {
                                    ingredients[k].amount -= ingAmount;
                                    break;
                                }
                            }
                        }
                        mixed = true;
                        break;
                    } else {
                        current = (current + 1) % numRecipes;
                    }
                } while (current != startIndex);
            }
    
            // 排序并输出结果
            sort(recipes.begin(), recipes.end(), compareRecipes);
    
            cout << "Scenario " << scenario << " top cocktails:" << endl;
            int count = 0;
            for (int i = 0; i < recipes.size() && count < 10; i++) {
                if (recipes[i].timesMixed > 0) {
                    cout << (count + 1) << " " << recipes[i].name << " " 
                         << recipes[i].timesMixed << " " << recipes[i].price << endl;
                    count++;
                }
            }
        }
    
        return 0;
    }    
    
    • 1

    信息

    ID
    914
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    5
    已通过
    1
    上传者