Submission #1150297

#TimeUsernameProblemLanguageResultExecution timeMemory
1150297VovamatrixSplit the sequence (APIO14_sequence)C++20
100 / 100
892 ms86600 KiB
//https://oj.uz/problem/view/APIO14_sequence
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define PI acos(-1)
#define LSB(i) ((i) & -(i))
#define ll long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define sc second
#define th third
#define fo fourth
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ldb double
#define INF 1e15
#define MOD 1000000007
#define endl "\n"

#define all(data)       data.begin(),data.end()
#define TYPEMAX(type)   std::numeric_limits<type>::max()
#define TYPEMIN(type)   std::numeric_limits<type>::min()
#define MAXN 100007
#define MAXK 207
#define MAXA 1007
struct Line
{
    ll k,n,id;
    Line(){}
    Line(ll kk,ll nn,ll idx) {k=kk; n=nn; id=idx;}
    ll val(ll x) {return k*x+n;}
};
double Intersect(Line p1,Line p2) {return (double)(p2.n-p1.n)/(p1.k-p2.k);}
deque<Line> ch;
void Add(Line p)
{
    if(ch.empty()) {ch.pb(p); return;}
    if(ch.back().k==p.k)
    {
        if(p.n<ch.back().n) return;
        else ch.pop_back();
    }
    while(ch.size()>=2 && Intersect(p,ch.back())<=Intersect(ch.back(),ch[ch.size()-2])) ch.pop_back();
    ch.pb(p);
}
ll a[MAXN],psum[MAXN],psum2[MAXN],dp[MAXN][2];
int nxt[MAXN][MAXK];
int main()
{
    ios::sync_with_stdio(false); cin.tie(0);
    ll n,k; cin>>n>>k; k++;
    for(int i=1;i<=n;i++) {cin>>a[i]; psum[i]=psum[i-1]+a[i]; psum2[i]=psum2[i-1]+a[i]*a[i];}
    for(int i=1;i<=n;i++) dp[i][0]=0;
    vector<ll> v;
    for(int j=2;j<=k;j++)
    {
        ch.clear();
        for(int i=j;i<=n;i++)
        {
            Add(Line(psum[i-1],dp[i-1][0]-psum[i-1]*psum[i-1],i-1));
            while(ch.size()>=2 && Intersect(ch[0],ch[1])<=(double)psum[i]) ch.pop_front();
            dp[i][1]=ch[0].val(psum[i]);
            nxt[i][j]=ch[0].id;
        }
        for(int i=j;i<=n;i++) dp[i][0]=dp[i][1];
    }
    cout<<dp[n][1]<<endl;
    while(nxt[n][k])
    {
        v.pb(n=nxt[n][k]);
        k--;
    }
    reverse(all(v));
    for(auto w:v) cout<<w<<" ";
    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...