Submission #1365235

#TimeUsernameProblemLanguageResultExecution timeMemory
1365235eyadoozSplit the sequence (APIO14_sequence)C++20
50 / 100
418 ms154756 KiB
#include<bits/stdc++.h>
using namespace std;

#define int long long
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()
#define endl '\n'

struct line
{
    int m, b, ind;
    pii calc(int x)
    {
        return {m*x+b, ind};
    }
};
 
struct node
{
    node *l=nullptr, *r=nullptr;
    line *ln=nullptr;
};
void update(node* cur, line* lin, int l, int r)
{
    int m=(l+r)/2;
    if(cur->ln==nullptr) {cur->ln=lin;return;}
    if(lin->calc(m)>cur->ln->calc(m)) swap(lin, cur->ln);
    if(l==r) return;
    if(cur->ln->calc(l)<lin->calc(l))
    {
        if(cur->l==nullptr) cur->l = new node();
        update(cur->l, lin, l, m);
    }
    if(cur->ln->calc(r)<lin->calc(r))
    {
        if(cur->r==nullptr) cur->r = new node();
        update(cur->r, lin, m+1, r);
    }
}
pii query(int x, node* cur, int l, int r)
{
    if(!cur) return {-LLONG_MAX, 0};
    pii res = (cur->ln ? cur->ln->calc(x) : make_pair(-LLONG_MAX, 0ll));
    if(l==r) return res;
    int m = (l + r) / 2;
    if(x<=m&&cur->l) return max(res, query(x, cur->l, l, m));
    if(x>m&&cur->r) return max(res, query(x, cur->r, m+1, r));
    return res;
}
node* root[205];

main()
{
    cin.tie(0) -> sync_with_stdio(0);

    for(int i = 0;i <= 200;i++) root[i]=new node();
    int n, k;
    cin >> n >> k;
    int a[n+1], pref[n+5]={}, sum=0;
    int dp[n+5][k+5]={};
    int last[n+5][k+1]={};
    dp[0][0]=a[0]=0;
    for(int i = 1;i <= n;i++) {cin >> a[i];pref[i]=pref[i-1]+a[i];sum+=a[i];}
    for(int i = 0;i <= n;i++) 
    {
        for(int j=0;j<=k;j++) dp[i][j]=-LONG_LONG_MAX;
    }
    int mx=0;
    for(int i = 0;i <= n;i++) last[i][0]=0;
    update(root[0], new line{0, 0, 0}, 0, 1e9);
    for(int i = 1;i <= n;i++) 
    {
        sum-=a[i];
        for(int x=1;x<=k;x++) 
        {
            if(x>i) continue;
            auto[val, ind]=query(sum, root[x-1], 0, 1e9);
            dp[i][x]=val+(sum*pref[i]);
            last[i][x]=ind;
        }
        for(int x=1;x<=k;x++) 
        {
            if(x>i) continue;
            update(root[x], new line{-pref[i], dp[i][x], i}, 0, 1e9);
        }
        mx=max(mx, dp[i][k]);
    }
    for(int i = 1;i<=n;i++) 
    {
        if(mx==dp[i][k]) 
        {
            cout << mx << endl;
            int cur=k, j=i;
            cout << j << " ";
            while(cur!=1) 
            {
                cout << last[j][cur] << " ";
                j=last[j][cur];
                cur--;
            }
            return 0;
        }
    }
}

Compilation message (stderr)

sequence.cpp:57:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   57 | main()
      | ^~~~
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...