#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;
}
已经开始刷普及组的题了,不过为啥上来就爆零? 题目