Submission #138163

# Submission time Handle Problem Language Result Execution time Memory
138163 2019-07-29T13:33:46 Z muradeyn Split the sequence (APIO14_sequence) C++14
71 / 100
299 ms 131072 KB
/* Murad Eynizade */

#include <bits/stdc++.h>
#define int long long
#define FAST_READ ios_base::sync_with_stdio(0);cin.tie(0);
#define F first
#define S second
//#define endl '\n'

using namespace std;

const int maxx = 100005;

int n , k , idx;

int dp[maxx][201] , pre[maxx] , arr[maxx] , sz[201] , in[201];

vector< pair< int , pair<int,int> > >v[201];

int inter(pair<int,int>a,pair<int,int>b) {
    return (a.S - b.S) / (b.F - a.F);
}

pair<int,int> sec(int i) {
    pair< int , pair<int,int> >t , ret;
    t = v[i].back(); v[i].pop_back();
    ret = v[i].back(); v[i].push_back(t);
    return ret.S;
}

main() {
    FAST_READ
    cin>>n>>k;
    for (int i = 1;i<=n;i++)cin>>arr[i] , pre[i] = pre[i - 1] + arr[i];
    for (int j = 1;j<=n;j++) {
        dp[j][1] = pre[j] * (pre[n] - pre[j]);
        int i = 1;
        pair<int,int>line = {pre[j] , -pre[j] * pre[n] + dp[j][1]};
        while (sz[i] >= 2) {
            pair<int,int>tp = v[i].back().S;
            pair<int,int>tp2 = sec(i);
            if (tp.F == line.F) {
                v[i].pop_back();
                sz[i]--;
                continue;
            }
            int inter1 = inter(tp , tp2);
            int inter2 = inter(tp2 , line);
            if (inter2 < inter1)v[i].pop_back() , sz[i]--;
            else {
                break;
            }
        }
        if (sz[i]) {
            pair< int , pair<int,int> >cur = v[i].back();
            v[i].pop_back();
            sz[i]--;
            if (cur.S.F != line.F) {
                cur.F = inter(line , cur.S);
                v[i].push_back(cur);
                sz[i]++;
            }
        }
        v[i].push_back({1000000001 , line});
        sz[i]++;
    }
    for (int j = 2;j<=k;j++) {
        for (int i = j;i<=n;i++) {
            while (in[j - 1] < sz[j - 1] && v[j - 1][in[j - 1]].F < pre[i])in[j - 1]++;
            pair< int , pair<int,int> >cur = v[j - 1][in[j - 1]];
            dp[i][j] = cur.S.F * pre[i] + cur.S.S + pre[i] * pre[n] - pre[i] * pre[i];
            int ii = j;
            pair<int,int>line = {pre[i] , -pre[i] * pre[n] + dp[i][j]};
            while (sz[ii] >= 2) {
                pair<int,int>tp = v[ii].back().S;
                pair<int,int>tp2 = sec(ii);
                if (tp.F == line.F) {
                    v[ii].pop_back();
                    sz[ii]--;
                    continue;
                }
                int inter1 = inter(tp , tp2);
                int inter2 = inter(tp2 , line);
                if (inter2 < inter1)v[ii].pop_back() , sz[ii]--;
                else {
                    break;
                }
            }
            if (sz[ii]) {
                pair< int , pair<int,int> >cur = v[ii].back();
                v[ii].pop_back();
                sz[ii]--;
                if (cur.S.F != line.F) {
                    cur.F = inter(line , cur.S);
                    v[ii].push_back(cur);
                    sz[ii]++;
                }
            }
            v[ii].push_back({1000000001 , line});
            sz[ii]++;
        }
    }
    idx = -1;
    for (int i = 1;i<=n;i++) {
        if (idx == -1)idx = i;
        if (dp[i][k] > dp[idx][k])idx = i;
    }
    cout<<dp[idx][k]<<endl;
    vector<int>res;
    res.push_back(idx);
    k--;
    while (k) {
        for (int ii = idx;ii;ii--) {
            if (dp[ii - 1][k] + (pre[idx] - pre[ii - 1]) * (pre[n] - pre[idx]) == dp[idx][k + 1]) {
                idx = ii - 1;
                break;
            }
        }
        res.push_back(idx);
        k--;
    }
    reverse(res.begin(),res.end());
    for (int i : res)cout<<i<<" ";
    cout<<endl;
}

Compilation message

sequence.cpp:31:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main() {
      ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 468 KB contestant found the optimal answer: 108 == 108
2 Correct 2 ms 376 KB contestant found the optimal answer: 999 == 999
3 Correct 2 ms 376 KB contestant found the optimal answer: 0 == 0
4 Correct 2 ms 376 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 2 ms 376 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 2 ms 376 KB contestant found the optimal answer: 1 == 1
7 Correct 3 ms 420 KB contestant found the optimal answer: 1 == 1
8 Correct 2 ms 376 KB contestant found the optimal answer: 1 == 1
9 Correct 2 ms 376 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 2 ms 504 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 2 ms 376 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 2 ms 376 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 2 ms 376 KB contestant found the optimal answer: 140072 == 140072
14 Correct 2 ms 380 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 2 ms 380 KB contestant found the optimal answer: 805 == 805
16 Correct 3 ms 376 KB contestant found the optimal answer: 900189994 == 900189994
17 Correct 2 ms 376 KB contestant found the optimal answer: 999919994 == 999919994
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 1 ms 476 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 2 ms 508 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 2 ms 376 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 2 ms 504 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 2 ms 376 KB contestant found the optimal answer: 933702 == 933702
7 Correct 2 ms 504 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 2 ms 476 KB contestant found the optimal answer: 687136 == 687136
9 Correct 2 ms 376 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 2 ms 504 KB contestant found the optimal answer: 29000419931 == 29000419931
# Verdict Execution time Memory Grader output
1 Correct 2 ms 632 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 2 ms 632 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 5 ms 1396 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 2 ms 680 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 4 ms 1148 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 4 ms 1144 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 5 ms 1528 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 3 ms 1016 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 3 ms 860 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 3 ms 1016 KB contestant found the optimal answer: 490686959791 == 490686959791
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2040 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 4 ms 2040 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 23 ms 6776 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 4 ms 2040 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 23 ms 6264 KB contestant found the optimal answer: 679388326 == 679388326
6 Correct 21 ms 5368 KB contestant found the optimal answer: 4699030287 == 4699030287
7 Correct 23 ms 6392 KB contestant found the optimal answer: 12418819758185 == 12418819758185
8 Correct 23 ms 6776 KB contestant found the optimal answer: 31093317350 == 31093317350
9 Correct 7 ms 2936 KB contestant found the optimal answer: 12194625429236 == 12194625429236
10 Correct 12 ms 3832 KB contestant found the optimal answer: 12345131038664 == 12345131038664
# Verdict Execution time Memory Grader output
1 Correct 19 ms 16952 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 19 ms 16952 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Correct 285 ms 63824 KB contestant found the optimal answer: 4973126687469639 == 4973126687469639
4 Correct 20 ms 17396 KB contestant found the optimal answer: 3748491676694116 == 3748491676694116
5 Correct 164 ms 38520 KB contestant found the optimal answer: 1085432199 == 1085432199
6 Correct 197 ms 33820 KB contestant found the optimal answer: 514790755404 == 514790755404
7 Correct 299 ms 50132 KB contestant found the optimal answer: 1256105310476641 == 1256105310476641
8 Correct 183 ms 43764 KB contestant found the optimal answer: 3099592898816 == 3099592898816
9 Correct 203 ms 46348 KB contestant found the optimal answer: 1241131419367412 == 1241131419367412
10 Correct 259 ms 54572 KB contestant found the optimal answer: 1243084101967798 == 1243084101967798
# Verdict Execution time Memory Grader output
1 Runtime error 122 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -