Submission #949113

# Submission time Handle Problem Language Result Execution time Memory
949113 2024-03-19T01:12:16 Z hengliao Split the sequence (APIO14_sequence) C++17
71 / 100
60 ms 70204 KB
#include<bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define pb push_back
#define vll vector<ll>
#define pll pair<ll, ll>
typedef long long ll;

struct line{
    ll m, n, id;
    ll getval(ll x){
        return m*x+n;
    }
    ll xinter(const line &tar){
        return (tar.n-n) / (m-tar.m);
    }
};

const ll mxN=1e4+5;

ll dp[mxN][201];
ll par[mxN][201];

void solve(){
    memset(dp, 0, sizeof(dp));
    memset(par, 0, sizeof(par));
    ll n, k;
    cin>>n>>k;
    vll tep(n);
    for(ll i=0;i<n;i++){
        cin>>tep[i];
    }
    vll a;
    vll idx;
    for(ll i=0;i<n;i++){
        if(tep[i]!=0){
            a.pb(tep[i]);
            idx.pb(i);
        }
    }
    ll siz=a.size();
    if(siz<=k){
        ll ans=0;
        ll pre=0;
        for(ll i=0;i<siz;i++){
            ans+=a[i]*pre;
            pre+=a[i];
        }
        cout<<ans<<'\n';
        ll cnt=0;
        for(auto it:idx){
            if(it>=n-1) break;
            cout<<it+1<<' ';
            cnt++;
        }
        for(ll i=0;i<n && cnt<k;i++){
            if(tep[i]==0){
                cout<<i+1<<' ';
                cnt++;
            }
        }
        cout<<'\n';
        return;
    }
    vll pre(siz+1);
    for(ll i=0;i<siz;i++){
        pre[i+1]=pre[i]+a[i];
    }
    //for()
    for(ll tar=2;tar<=k;tar++){
        deque<line> dq;
        dq.pb({0, 0, 0});
        for(ll i=1;i<=siz;i++){
            while(dq.size()>=2 && dq[0].getval(pre[i])<dq[1].getval(pre[i])){
                dq.pop_front();
            }
            dp[i][tar]=dq[0].getval(pre[i]);
            par[i][tar]=dq[0].id;
            line nline={pre[i], dp[i][tar-1]-pre[i]*pre[i], i};
            while(dq.size()>=2 && dq[(int) dq.size()-1].xinter(nline)<
                dq[(int) dq.size()-2].xinter(nline)){
                dq.pop_back();
            } 
            dq.pb(nline);
        }
    }
    for(ll i=1;i<=siz;i++){
        dp[i][k]=dp[i][k]+(pre[siz]-pre[i])*pre[i];
    }
    ll ans=0;
    ll cur=1;
    for(ll i=1;i<=siz-1;i++){
        if(dp[i][k]>ans){
            ans=dp[i][k];
            cur=i;
        }
    }
    cout<<ans<<'\n';
    vll ans1;
    for(ll i=k;i>=1;i--){
        //assert(cur-1<-100);
        /*if(cur==0){
            cout<<"wrong\n";
            break;
        }*/
        ans1.pb(idx[cur-1]+1);
        //cout<<cur<<' ';
        cur=par[cur][i];
    }
    reverse(ans1.begin(), ans1.end());
    for(auto it:ans1){
        cout<<it<<' ';
    }
    cout<<'\n';
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    solve();
    return 0;
}

/*

dp[k][i]=max j<i dp[k-1][j]+(pre[i]-pre[j])*pre[j]
                =pre[j]*pre[i]+(dp[k-1][j]-pre[j]*pre[j])

*/
# Verdict Execution time Memory Grader output
1 Correct 4 ms 31832 KB contestant found the optimal answer: 108 == 108
2 Correct 4 ms 31908 KB contestant found the optimal answer: 999 == 999
3 Correct 4 ms 31700 KB contestant found the optimal answer: 0 == 0
4 Correct 5 ms 31836 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 4 ms 31832 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 4 ms 31836 KB contestant found the optimal answer: 1 == 1
7 Correct 5 ms 31836 KB contestant found the optimal answer: 1 == 1
8 Correct 5 ms 31836 KB contestant found the optimal answer: 1 == 1
9 Correct 5 ms 31836 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 4 ms 31836 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 5 ms 31836 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 5 ms 31836 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 4 ms 31940 KB contestant found the optimal answer: 140072 == 140072
14 Correct 4 ms 31832 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 5 ms 31832 KB contestant found the optimal answer: 805 == 805
16 Correct 5 ms 31836 KB contestant found the optimal answer: 900189994 == 900189994
17 Correct 4 ms 31932 KB contestant found the optimal answer: 999919994 == 999919994
# Verdict Execution time Memory Grader output
1 Correct 4 ms 31836 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 4 ms 31836 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 5 ms 31836 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 4 ms 31836 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 4 ms 31836 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 5 ms 31836 KB contestant found the optimal answer: 933702 == 933702
7 Correct 4 ms 31704 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 5 ms 31836 KB contestant found the optimal answer: 687136 == 687136
9 Correct 6 ms 31836 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 5 ms 31832 KB contestant found the optimal answer: 29000419931 == 29000419931
# Verdict Execution time Memory Grader output
1 Correct 5 ms 31836 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 5 ms 31836 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 5 ms 31836 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 4 ms 31932 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 5 ms 31836 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 4 ms 31832 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 5 ms 31832 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 5 ms 32032 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 4 ms 31832 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 5 ms 31924 KB contestant found the optimal answer: 490686959791 == 490686959791
# Verdict Execution time Memory Grader output
1 Correct 4 ms 31832 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 5 ms 31836 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 9 ms 31832 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 4 ms 31832 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 8 ms 31836 KB contestant found the optimal answer: 679388326 == 679388326
6 Correct 7 ms 31836 KB contestant found the optimal answer: 4699030287 == 4699030287
7 Correct 9 ms 31836 KB contestant found the optimal answer: 12418819758185 == 12418819758185
8 Correct 9 ms 31748 KB contestant found the optimal answer: 31093317350 == 31093317350
9 Correct 5 ms 31832 KB contestant found the optimal answer: 12194625429236 == 12194625429236
10 Correct 7 ms 31936 KB contestant found the optimal answer: 12345131038664 == 12345131038664
# Verdict Execution time Memory Grader output
1 Correct 6 ms 32088 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 6 ms 32220 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Correct 59 ms 32348 KB contestant found the optimal answer: 4973126687469639 == 4973126687469639
4 Correct 6 ms 32348 KB contestant found the optimal answer: 3748491676694116 == 3748491676694116
5 Correct 33 ms 32376 KB contestant found the optimal answer: 1085432199 == 1085432199
6 Correct 39 ms 32092 KB contestant found the optimal answer: 514790755404 == 514790755404
7 Correct 54 ms 32504 KB contestant found the optimal answer: 1256105310476641 == 1256105310476641
8 Correct 40 ms 32600 KB contestant found the optimal answer: 3099592898816 == 3099592898816
9 Correct 43 ms 32388 KB contestant found the optimal answer: 1241131419367412 == 1241131419367412
10 Correct 60 ms 32348 KB contestant found the optimal answer: 1243084101967798 == 1243084101967798
# Verdict Execution time Memory Grader output
1 Runtime error 38 ms 70204 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -