Explanation
We can binary search on real values to find the minimum average we can achieve.
Now, if we find the prefix sum array of the machine milk production array , consider the condition we need to achieve an average by removing the subarray :
where is the current sum of the array. After multiplying both sides by the denominator and isolating all terms involving , we get:
For a fixed , we should try to minimize the left side expression. So we can iterate from to and keep a running minimum of the left side of the inequality. If the condition is satisfied, we can achieve an average .
Implementation
Time Complexity: , where is the fixed error margin.
C++
#include <bits/stdc++.h>using namespace std;int main() {freopen("sabotage.in", "r", stdin);freopen("sabotage.out", "w", stdout);int n;cin >> n;vector<int> m(n);
Python
with open("sabotage.in", "r") as read:n = int(read.readline())m = [int(read.readline()) for i in range(n)]pref_sum = [0]for i in range(n):pref_sum.append(pref_sum[-1] + m[i])arr_sum = sum(m)
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!