Untitled
unknown
plain_text
2 years ago
1.8 kB
7
Indexable
#include <iostream> #include <vector> #include <algorithm> struct Singer { int budget; int followers; Singer(int b, int f) : budget(b), followers(f) {} }; bool compareSingers(const Singer& s1, const Singer& s2) { // Sắp xếp theo độ giảm dần của lượng người theo dõi return s1.followers > s2.followers; } void optimizeAdvertising(std::vector<Singer>& singers, int totalBudget) { // Sắp xếp danh sách ca sĩ theo độ giảm dần của lượng người theo dõi std::sort(singers.begin(), singers.end(), compareSingers); int maxFollowers = 0; int totalSpent = 0; for (const Singer& singer : singers) { // Nếu chi phí dự kiến với ca sĩ hiện tại không vượt quá tổng ngân sách if (totalSpent + singer.budget <= totalBudget) { totalSpent += singer.budget; maxFollowers += singer.followers; } else { // Nếu vượt quá ngân sách, dừng lại vì không thể thêm ca sĩ khác nữa break; } } std::cout << "Tổng người theo dõi tối đa: " << maxFollowers << std::endl; std::cout << "Tổng chi phí tối thiểu: " << totalSpent << std::endl; } int main() { std::vector<Singer> singers; singers.emplace_back(1000, 9000); // Ca sĩ 1, có budget 1000 và 50000 người theo dõi singers.emplace_back(2000, 8500); // Ca sĩ 2, có budget 2000 và 75000 người theo dõi singers.emplace_back(2000, 10000); // Ca sĩ 3, có budget 1500 và 60000 người theo dõi int totalBudget = 4000; // Tổng ngân sách optimizeAdvertising(singers, totalBudget); return 0; }
Editor is loading...