Submission #89572

#TimeUsernameProblemLanguageResultExecution timeMemory
89572vanbang9710Split the sequence (APIO14_sequence)C++14
71 / 100
2070 ms83736 KiB
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<climits>
#include<cstring>
#include<iomanip>
#include<string>
#include<bitset>
#include<unordered_map>
#include<unordered_set>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<deque>
#include<algorithm>
#include<functional>
#include<chrono>
//#include<windows.h>
//#include<direct.h>
#include<random>
#include<sstream>
 
#define y0 asdahsdlkahsdad
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define taskname "sequence"
//#define GuiltiaSinJurai
 
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
 
using namespace std;
 
inline void Cin(int &x)
{
    register char c;
    for (c = getchar(); c < '0' || c > '9'; c = getchar());
    for (x = 0; c >= '0' && c <= '9'; c = getchar())
        x = x * 10 + c - '0';
}
inline void Cout(int x) { if (x > 9) Cout(x / 10); putchar(x % 10 + '0'); }
inline void Cout(ll x) { if (x > 9) Cout(x / 10); putchar(x % 10 + '0'); }
 
const int maxN = 1e5, maxK = 200;
 
int trace[maxN][maxK], s[maxN], N, K, a;
static ll f[maxN], t;
 
struct TLine
{
    int x;
    ll y;
} p[maxN];
typedef TLine* PLine;
PLine id[maxN];
 
inline bool Erase(const PLine a, const PLine b, const PLine c)
{
    return (a->y - c->y) * (b->x - c->x) <= (c->x - a->x) * (c->y - b->y);
}
inline ll Get(const PLine l) { return t * l->x + l->y; }
 
int main()
{
    #ifdef GuiltiaSinJurai
    auto start = chrono::steady_clock::now();
    #endif
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen(taskname".inp", "r", stdin);
    //freopen(taskname".out", "w", stdout);
 
    Cin(N); Cin(K);
    for (int i = 0, val = 0; i < N; ++i)
        Cin(a), s[i] = (val += a);
    for (int k = 0; k < K; ++k)
    {
        auto nId = id - 1, j = id;
        ll val = f[k], res, tmp;
        for (int i = k + 1; i < N; ++i)
        {
            --i;
            auto idi = p + i;
            *idi = {s[i], val - (ll)s[i] * s[i]};
            auto lim = max(id + 1, j);
            while (nId >= lim && Erase(*(nId - 1), *nId, idi)) --nId;
            *(++nId) = idi;
            val = f[++i];
            t = s[i];
            if (j >= nId) res = Get(*(j = nId));
            else for (res = Get(*j); j < nId; ++j)
                if (res < (tmp = Get(*(j + 1)))) res = tmp;
                else break;
            f[i] = res;
            trace[i][k] = *j - p;
        }
    }
    Cout(f[N - 1]); putchar('\n');
    int i = N - 1;
    while (--K >= 0)
        Cout((i = trace[i][K]) + 1), putchar(' ');
 
    #ifdef GuiltiaSinJurai
    auto end = chrono::steady_clock::now();
    cerr << "In milliseconds : "
         << chrono::duration_cast<chrono::milliseconds>(end - start).count();
    cerr << '\n' << "In seconds : "
         << chrono::duration_cast<chrono::seconds>(end - start).count() << '\n';
    #endif
    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...