#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Function to calculate total points based on customer data and adTimes
int calculatePoints(vector<int>& adTimes, const vector<pair<int, int>>& ads, const vector<pair<int, int>>& customers) {
int totalPoints = 0;
for (auto customer : customers) {
int arrival = customer.first;
int duration = customer.second;
int maxPoints = 0;
for (int i = 0; i < adTimes.size(); i++) {
int adStart = adTimes[i];
int adDuration = ads[i].first;
int adPoints = ads[i].second;
// Check if the customer can fully watch the ad
if (adStart >= arrival && adStart + adDuration <= arrival + duration) {
maxPoints = max(maxPoints, adPoints);
}
}
totalPoints += maxPoints;
}
return totalPoints;
}
// Backtracking function to assign ads
void assignAds(int adIndex, vector<int>& adTimes, vector<bool>& timeline, int maxTime, const vector<pair<int, int>>& ads, const vector<pair<int, int>>& customers, int& maxPoints) {
if (adIndex == ads.size()) { // All ads assigned
maxPoints = max(maxPoints, calculatePoints(adTimes, ads, customers));
return;
}
int adDuration = ads[adIndex].first;
for (int startTime = 1; startTime <= maxTime; ++startTime) {
bool canPlace = true;
// Check if the ad can be placed without overlapping
for (int t = startTime; t < startTime + adDuration; ++t) {
if (t > maxTime || timeline[t]) {
canPlace = false;
break;
}
}
if (canPlace) {
// Place the ad
adTimes[adIndex] = startTime;
for (int t = startTime; t < startTime + adDuration; ++t) {
timeline[t] = true;
}
// Recurse for the next ad
assignAds(adIndex + 1, adTimes, timeline, maxTime, ads, customers, maxPoints);
// Backtrack
for (int t = startTime; t < startTime + adDuration; ++t) {
timeline[t] = false;
}
adTimes[adIndex] = -1;
}
}
}
int main() {
// Ads: {duration, points}
vector<pair<int, int>> ads = {{3, 6}, {2, 4}, {1, 3}};
// Customers: {arrival time, duration}
vector<pair<int, int>> customers = {{1, 5}, {1, 3}, {2, 4}, {2, 2}};
// Maximum time (from customers' durations)
int maxTime = 10;
// Initialize variables
vector<int> adTimes(ads.size(), -1); // Start times for each ad
vector<bool> timeline(maxTime + 1, false); // Track occupied time slots
int maxPoints = 0;
// Start backtracking
assignAds(0, adTimes, timeline, maxTime, ads, customers, maxPoints);
// Output the result
cout << "Maximum Points: " << maxPoints << endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8YWxnb3JpdGhtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKLy8gRnVuY3Rpb24gdG8gY2FsY3VsYXRlIHRvdGFsIHBvaW50cyBiYXNlZCBvbiBjdXN0b21lciBkYXRhIGFuZCBhZFRpbWVzCmludCBjYWxjdWxhdGVQb2ludHModmVjdG9yPGludD4mIGFkVGltZXMsIGNvbnN0IHZlY3RvcjxwYWlyPGludCwgaW50Pj4mIGFkcywgY29uc3QgdmVjdG9yPHBhaXI8aW50LCBpbnQ+PiYgY3VzdG9tZXJzKSB7CiAgICBpbnQgdG90YWxQb2ludHMgPSAwOwoKICAgIGZvciAoYXV0byBjdXN0b21lciA6IGN1c3RvbWVycykgewogICAgICAgIGludCBhcnJpdmFsID0gY3VzdG9tZXIuZmlyc3Q7CiAgICAgICAgaW50IGR1cmF0aW9uID0gY3VzdG9tZXIuc2Vjb25kOwoKICAgICAgICBpbnQgbWF4UG9pbnRzID0gMDsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IGFkVGltZXMuc2l6ZSgpOyBpKyspIHsKICAgICAgICAgICAgaW50IGFkU3RhcnQgPSBhZFRpbWVzW2ldOwogICAgICAgICAgICBpbnQgYWREdXJhdGlvbiA9IGFkc1tpXS5maXJzdDsKICAgICAgICAgICAgaW50IGFkUG9pbnRzID0gYWRzW2ldLnNlY29uZDsKCiAgICAgICAgICAgIC8vIENoZWNrIGlmIHRoZSBjdXN0b21lciBjYW4gZnVsbHkgd2F0Y2ggdGhlIGFkCiAgICAgICAgICAgIGlmIChhZFN0YXJ0ID49IGFycml2YWwgJiYgYWRTdGFydCArIGFkRHVyYXRpb24gPD0gYXJyaXZhbCArIGR1cmF0aW9uKSB7CiAgICAgICAgICAgICAgICBtYXhQb2ludHMg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UGxhY2UgPSBmYWxzZTsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICBpZiAoY2FuUGxhY2UpIHsKICAgICAgICAgICAgLy8gUGxhY2UgdGhlIGFkCiAgICAgICAgICAgIGFkVGltZXNbYWRJbmRleF0gPSBzdGFydFRpbWU7CiAgICAgICAgICAgIGZvciAoaW50IHQgPSBzdGFydFRpbWU7IHQgPCBzdGFydFRpbWUgKyBhZER1cmF0aW9uOyArK3QpIHsKICAgICAgICAgICAgICAgIHRpbWVsaW5lW3RdID0gdHJ1ZTsKICAgICAgICAgICAgfQoKICAgICAgICAgICAgLy8gUmVjdXJzZSBmb3IgdGhlIG5leHQgYWQKICAgICAgICAgICAgYXNzaWduQWRzKGFkSW5kZXggKyAxLCBhZFRpbWVzLCB0aW1lbGluZSwgbWF4VGltZSwgYWRzLCBjdXN0b21lcnMsIG1heFBvaW50cyk7CgogICAgICAgICAgICAvLyBCYWNrdHJhY2sKICAgICAgICAgICAgZm9yIChpbnQgdCA9IHN0YXJ0VGltZTsgdCA8IHN0YXJ0VGltZSArIGFkRHVyYXRpb247ICsrdCkgewogICAgICAgICAgICAgICAgdGltZWxpbmVbdF0gPSBmYWxzZTsKICAgICAgICAgICAgfQogICAgICAgICAgICBhZFRpbWVzW2FkSW5kZXhdID0gLTE7CiAgICAgICAgfQogICAgfQp9CgppbnQgbWFpbigpIHsKICAgIC8vIEFkczoge2R1cmF0aW9uLCBwb2ludHN9CiAgICB2ZWN0b3I8cGFpcjxpbnQsIGludD4+IGFkcyA9IHt7MywgNn0sIHsyLCA0fSwgezEsIDN9fTsKCiAgICAvLyBDdXN0b21lcnM6IHthcnJpdmFsIHRpbWUsIGR1cmF0aW9ufQogICAgdmVjdG9yPHBhaXI8aW50LCBpbnQ+PiBjdXN0b21lcnMgPSB7ezEsIDV9LCB7MSwgM30sIHsyLCA0fSwgezIsIDJ9fTsKCiAgICAvLyBNYXhpbXVtIHRpbWU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