CF - William and Robot
Author: Alex Du
Explanation
First note that the only requirement for a valid solution is that for any even number , William must have selected at most integers out of the first integers. It is impossible to select more than integers in the first integers, since the robot will have taken all other integers by the time you can choose your next integer. Note that it is also possible to "save" your choices, since you can choose less than integers for , allowing you to choose more for a larger .
For example, if we had array of size , we could select either of the first two elements and either of the last two elements, selecting exactly integers for both and . Alternatively, we could select both of the last two elements, selecting 0 integers for and integers for . Note that we cannot take both the first and second element, since the Robot will take the one we don't pick.
To solve this problem, we can use a greedy algorithm.
For any even , if we know the integers that make the highest sum, we can use this to find the integers for .
The base case is , where the highest sum is simply the maximum of the first two integers.
Similarly, to gain the highest sum for our current , we will always select the maximum of the and elements, since we now have another turn and this will build directly off of our previous solution.
However, it is sometimes optimal to take both integers, rather than just the maximum of the two. After , we can choose to remove any previous integer we selected and replace it with the minimum of the two integers we are currently considering. Since we can remove any integer, we will remove the minimum. Therefore, this is only worth it if the minimum of the two integers is greater than the minimum of all the integers we already selected. To quickly access and remove the smallest integer, we can use either a priority queue or a multiset.
Implementation
Time Complexity:
C++
#include <bits/stdc++.h>using namespace std;int main() {int n;cin >> n;vector<int> a(n);for (int &i : a) { cin >> i; }
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!