55 lines
1.5 KiB
C++
55 lines
1.5 KiB
C++
#include <iostream>
|
|
#include <vector>
|
|
#include <algorithm>
|
|
#include <fstream>
|
|
|
|
using namespace std;
|
|
|
|
// Function to find the largest index j such that locations[j] <= locations[i] - 5
|
|
int p(int i, vector<int>& locations) {
|
|
for (int j = i - 1; j >= 0; j--) {
|
|
if (locations[j] <= locations[i] - 5) {
|
|
return j;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
// Function to compute the maximum total bonus and record the choices
|
|
pair<vector<int>, vector<int>> computeMaxBonusAndChoices(vector<int>& locations, vector<int>& bonuses) {
|
|
int n = locations.size();
|
|
vector<int> T(n + 1), R(n + 1);
|
|
T[0] = 0;
|
|
|
|
for (int i = 1; i <= n; i++) {
|
|
int pi = p(i - 1, locations);
|
|
int bonus = (pi != -1) ? bonuses[i - 1] + T[pi + 1] : bonuses[i - 1];
|
|
if (bonus > T[i - 1]) {
|
|
T[i] = bonus;
|
|
R[i] = i;
|
|
} else {
|
|
T[i] = T[i - 1];
|
|
R[i] = R[i - 1];
|
|
}
|
|
}
|
|
|
|
return {T, R};
|
|
}
|
|
|
|
int main() {
|
|
vector<int> locations = {8, 10, 15, 22, 26};
|
|
vector<int> bonuses = {15, 5, 25, 15, 5};
|
|
|
|
pair<vector<int>, vector<int>> result = computeMaxBonusAndChoices(locations, bonuses);
|
|
vector<int> T = result.first;
|
|
vector<int> R = result.second;
|
|
|
|
ofstream outfile("bonus_pc_out.txt");
|
|
for (int i = 0; i < T.size(); i++) {
|
|
outfile << "T[" << i << "] = " << T[i] << ", R[" << i << "] = " << R[i] << endl;
|
|
}
|
|
outfile << "Maximum total bonus = " << T.back() << endl;
|
|
outfile.close();
|
|
|
|
return 0;
|
|
} |