萌新求助
  • 板块学术版
  • 楼主_Amy
  • 当前回复0
  • 已保存回复0
  • 发布时间2023/11/6 12:57
  • 上次更新2023/11/6 16:50:09
查看原帖
萌新求助
1012285
_Amy楼主2023/11/6 12:57
#include <iostream>
#include <vector>
#include <cmath>

int main() {
    int expectedPrice;
    std::cin >> expectedPrice;

    int cost, baseQuantity;
    std::cin >> cost >> baseQuantity;

    std::vector<std::pair<int, int>> priceQuantity;
    int price, quantity;
    while (std::cin >> price >> quantity && (price != -1 || quantity != -1)) {
        priceQuantity.push_back({price, quantity});
    }

    int decrement;
    std::cin >> decrement;

    int maxProfit = 0;
    int minTaxOrSubsidy = 0;

    for (const auto& pq : priceQuantity) {
        int unitProfit = pq.first - cost;
        int quantity = pq.second;

        int remainingQuantity = baseQuantity - decrement * (pq.first - expectedPrice);

        if (remainingQuantity >= quantity) {
            int profit = unitProfit * quantity;
            if (profit > maxProfit) {
                maxProfit = profit;
                minTaxOrSubsidy = 0;
            }
        } else if (remainingQuantity > 0) {
            int profit = unitProfit * remainingQuantity;
            if (profit > maxProfit) {
                maxProfit = profit;
                minTaxOrSubsidy = -(quantity - remainingQuantity) * (expectedPrice - cost);
            } else if (profit == maxProfit) {
                int taxOrSubsidy = -(quantity - remainingQuantity) * (expectedPrice - cost);
                if (std::abs(taxOrSubsidy) < std::abs(minTaxOrSubsidy)) {
                    minTaxOrSubsidy = taxOrSubsidy;
                }
            }
        }
    }

    if (maxProfit == 0) {
        std::cout << "NO SOLUTION" << std::endl;
    } else {
        std::cout << minTaxOrSubsidy << std::endl;
    }

    return 0;
}

已经开始刷普及组的题了,不过为啥上来就爆零? 题目

2023/11/6 12:57
加载中...