Submission #923767

#TimeUsernameProblemLanguageResultExecution timeMemory
923767KarootFeast (NOI19_feast)C++17
71 / 100
982 ms134844 KiB
#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(ll index, int inInterval){
    if (index == n) return {0, 0};
    if (isVisited[index][inInterval])return mem[index][inInterval];
    isVisited[index][inInterval] = true;

    return mem[index][inInterval] = (inInterval ? max(make_pair(dp(index+1, 1).first+arr[index], dp(index+1, 1).second), make_pair(dp(index+1, 0).first, dp(index+1, 0).second)) : max(make_pair(dp(index+1, 0).first, dp(index+1, 0).second), make_pair(dp(index+1, 1).first + arr[index] - lambda, dp(index+1, 1).second+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(0,0).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 + mem[0][0].first);
    
    cout << ans << endl;


    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...