#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
#define sz(x) (int)x.size()
/*
Date: 2022/08/08 11:13
Problem Link: link
Topic(s):
Time Spent:
Solution Notes:
There is a simply N^2 dp:
dp[i][j] = max reward in [0, i] after using j segments
dp[i][j] = ps[i] + max(-ps[i'] + dp[i'][j-1]), where ps[i] is the prefix sums of the first i elements OR dp[i-1][j]
Using Alien's trick to optimize
Consider the "value" of each segment, which is the max reward we can get if we place this segment optimally. As we add more segments, the answer increases,
but the answer increases by less as we have more segments. In other words, the value of a segment always decreases. We can think about the value of a segment
as a concave function. The kth segment will correpsond to some value x. Our goal is to find the sum of the values of segments before k (i.e. segments with value
greater or equal to x).
We can do this by binary searching on x, but how do we know if we have used more/less than k segments? First, think what does it mean for each segment to have value
at least x. That means the sum over all segments we takes >= k*x. Let's subtract our x every time we take a new segment, now we want to make sure we only
take segments that result in a positive value. Now, we can utilize our dp formulation. Instead of keeping track of how many segments we used as part of the state,
subtracting by x will automatically "deter" us from taking too many segments (if x is bigger, that means we will select less elements, and vice versa).
dp[i] = max reward in [0, i] (offseting by x everytime we take a new segment)
dp[i] = ps[i] + max(-ps[i'] + dp[i']) - x OR dp[i-1]
Track for each dp[i], how many x's have we subtracted by (how many segments used). In the end, we will adjust our binary search on x with it.
In the end, we will have to add back the k*x we offsetted by. There might be multiple segments all with value == x. However, we don't have to worry about this,
as we offset by x every time we take a segment (meaning in the dp, segments == x will contribute 0), and we ONLY add back k*x regardless of what lo (in the binary search) is.
*/
const int MAXN = 2e5+5, INF = 1e9;
int n, k, arr[MAXN], ps[MAXN];
pair<int, double> check(double x, bool ans = false){
vector<double> dp(n+1);
vector<int> seg(n+1);
double mx = 0;
int id = 0;
for (int i=1; i<=n; i++){
if (ps[i] + mx - x >= dp[i-1]){
dp[i] = ps[i] + mx - x;
seg[i] = seg[id]+1;
}else{
dp[i] = dp[i-1];
seg[i] = seg[i-1];
}
if (dp[i] - ps[i] > mx){
mx = dp[i] - ps[i];
id = i;
}else if (dp[i] - ps[i] == x && seg[i] < seg[id]){
id = i;
}
}
return {seg[n], dp[n]};
}
int main(){
cin.tie(0); ios_base::sync_with_stdio(0);
// freopen("file.in", "r", stdin);
// freopen("file.out", "w", stdout);
cin >> n >> k;
for (int i=0; i<n; i++){
cin >> arr[i];
}
for (int i=0; i<n; i++){
ps[i+1] = ps[i] + arr[i];
}
// binary search on x
double low = 0;
double high = 1e15;
for (int i=0; i<100; i++){
double mid = (low + high)/2;
int seg = check(mid).first;
if (seg > k){
low = mid;
}else{
high = mid;
}
}
auto ans = check(high, true);
cout << ans.second + min(ans.first, k)*high << "\n";
}
/**
* Debugging checklist:
* - Reset everything after each TC
* - Integer overflow, index overflow
* - Special cases?
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
20 ms |
4172 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
2008 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
29 ms |
4300 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
20 ms |
4172 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |