Submission #923760

#TimeUsernameProblemLanguageResultExecution timeMemory
923760KarootFeast (NOI19_feast)C++17
41 / 100
1062 ms77768 KiB
#include <iostream>
#include <cmath>
#include <unordered_map>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <string>
#include <iomanip>
#include <algorithm>

#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;
    if (inInterval){
        //fortsätt -> ingen penalty
        pair<long double, ll> cont = {dp(index+1, 1).first+arr[index], dp(index+1, 1).second};

        pair<long double, ll> stop = {dp(index+1, 0).first, dp(index+1, 0).second};

        //stanna -> penalty
        return mem[index][inInterval] = max(cont, stop);
    } else {
        //köp inget
        pair<long double, ll> cont = {dp(index+1, 0).first, dp(index+1, 0).second};

        //starta
        pair<long double, ll> start = {dp(index+1, 1).first + arr[index] - lambda, dp(index+1, 1).second+1};
        return mem[index][inInterval] = max(cont, start);
    }
}

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 a = 0, b = 1e20;

    while (a+(1e-5) < b){
        long double mid = (a+b)/2.0;
        if (check(mid)){
            a = mid;
        } else {
            b = mid;
        }
    }

    check(a);
    //cout << fixed << setprecision(5);
    //cout << a << endl;

    //cout << check(6.99) << endl;
    //cout << check(7.99) << endl;

    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...