Submission #655295

#TimeUsernameProblemLanguageResultExecution timeMemory
655295benjaminkleynSplit the sequence (APIO14_sequence)C++17
33 / 100
42 ms81932 KiB
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
 
int N, K, a[100001];
ll pref[100001] = {0};
ll dp[100001][2] = {0};
int last_split[100001][201];

bool check(int i, int j)
{
    return (dp[i][0] - pref[i] * pref[i] >= dp[j][0] - pref[j] * pref[j]);
}

ll eval(int j, int i)
{
    return dp[j][0] + pref[j] * (pref[i] - pref[j]);
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> N >> K;
    for (int i = 1; i <= N; i++)
    {
         cin >> a[i];
         pref[i] = pref[i-1] + a[i];
    }
 
    for (int k = 1; k <= K; k++)
    {
        deque<int> cht;
        for (int i = k + 1; i <= N; i++)
        {
            dp[i][1] = 0;
            cht.push_back(i-1);

            while (cht.size() > 2 && eval(cht[0], i) <= eval(cht[1], i) && check(cht[0], cht[1]))
                cht.pop_front();

            eval(cht[0], i);
            dp[i][1] = eval(cht.front(), i);
            last_split[i][k] = cht.front();

            if (dp[i][1] < eval(cht.back(), i))
            {
                dp[i][1] = eval(cht.back(), i);
                last_split[i][k] = cht.back();
            }
        }
        for (int i = 1; i <= N; i++)
            dp[i][0] = dp[i][1];
    }
 
    cout << dp[N][0] << '\n';
 
    int i = N;
    do
    {
        i = last_split[i][K];
        cout << i << ' ';
    } while (--K);
    cout << '\n';
 
    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...