Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
The stamina value looks scary because it is real-valued, but ask yourself: if your current stamina were doubled, what happens to every possible future score?
The tasks are fixed in order, so think from the back. Let mean: maximum score obtainable from tasks if your current stamina is exactly .
Because all future gains are multiplied by the current stamina, if your current stamina is , the best future value from suffix is just . That kills the continuous-state monster. RIP.
For task , skipping gives . Completing gives , where . Take the better one.
The whole solution is one backward variable: start , then for down to do Print with enough precision.
The annoying part is stamina being a real number. But here is the trick: the problem is perfectly linear in stamina.
Suppose before some suffix of tasks your stamina is . Every score you can earn in that suffix is multiplied by , and every later stamina value is also multiplied by . So if the best answer for that suffix starting with stamina is , then starting with stamina gives exactly:
No need to store possible stamina values. That would be overkill, and overkill is where wrong solutions go to die.
Let be the maximum score obtainable from tasks assuming current stamina is right before task .
For task , define:
There are two choices:
Therefore:
The base case is:
We only need the next value, so one variable is enough.
The recurrence checks both legal actions at task . If we skip, the state of the suffix is unchanged. If we complete, the immediate gain is because current stamina is , and by linearity the optimal suffix value becomes .
Since every optimal strategy must either skip or complete task , the maximum of these two values is exactly optimal. Processing from right to left computes all suffix answers in valid dependency order.
Each task is processed once.
per test case, and
extra memory besides the input arrays. With , this is comfortably fast.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
cout << fixed << setprecision(10);
while (t--) {
int n;
cin >> n;
vector<int> c(n), p(n);
for (int i = 0; i < n; i++) {
cin >> c[i] >> p[i];
}
long double ans = 0.0L;
for (int i = n - 1; i >= 0; i--) {
long double keep = ans;
long double finish = c[i] + (1.0L - p[i] / 100.0L) * ans;
ans = max(keep, finish);
}
cout << (double)ans << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
cout << fixed << setprecision(10);
while (t--) {
int n;
cin >> n;
vector<int> c(n), p(n);
for (int i = 0; i < n; i++) {
cin >> c[i] >> p[i];
}
long double ans = 0.0L;
for (int i = n - 1; i >= 0; i--) {
long double keep = ans;
long double finish = c[i] + (1.0L - p[i] / 100.0L) * ans;
ans = max(keep, finish);
}
cout << (double)ans << '\n';
}
return 0;
}