This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast,inline")
#pragma GCC optimize ("unroll-loops")
#include <iostream>
#include <cmath>
#include <unordered_map>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <string>
#include <iomanip>
#include <algorithm>
#include <chrono>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
typedef long long ll;
ll linf = 1e15+1;
inline void scoobydoobydoo(){
ios::sync_with_stdio(false);
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
}
const int MAXN = 3e5+3;
ll arr[MAXN];
pair<long double, ll> mem[MAXN][2];
ll n, k;
bool isVisited[MAXN][2];
long double lambda;
pair<long double, ll> dp(){
mem[0][0] = {0, 0};
mem[0][1] = {arr[0]-lambda, 1};
for (int i = 1; i < n; i++){
// 0
mem[i][0] = max(mem[i-1][0], mem[i-1][1]);
//buy from 0, continue 1
mem[i][1] = max(make_pair(mem[i-1][0].first+arr[i]-lambda, mem[i-1][0].second+1), make_pair(mem[i-1][1].first+arr[i], mem[i-1][1].second));
}
return max(mem[n-1][0], mem[n-1][1]);
}
bool check(long double la){
lambda = la;
for (int i = 0; i < n; i++){
isVisited[i][0] = false;
isVisited[i][1] = false;
}
//cout << la << " " << dp(0, 0).first << " " << dp(0, 0).second << endl;
return dp().second >= k;
}
int main(){
scoobydoobydoo();
cin >> n >> k;
for (int i = 0; i < n; i++)cin >> arr[i];
long double b = 0;
for (int i = 0; i < n; i++)b += abs(arr[i]);
long double a = 0;
chrono::time_point<chrono::system_clock> start, end;
start = chrono::system_clock::now();
while (a+(1e-3) < b){
long double mid = (a+b)/2.0;
if (check(mid)){
a = mid;
} else {
b = mid;
}
end = chrono::system_clock::now();
chrono::duration<double> elapsed_seconds = end - start;
if (elapsed_seconds.count() > 0.9)break;
}
check(a);
ll ans = round(a*k + max(mem[n-1][0], mem[n-1][1]).first);
cout << ans << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |