Submission #868359

# Submission time Handle Problem Language Result Execution time Memory
868359 2023-10-31T07:59:14 Z velislavgarkov Split the sequence (APIO14_sequence) C++14
71 / 100
90 ms 131072 KB
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
const long long INF=2e18;
const int MAXN=1e5+10;
const int MAXK=2e2+5;
struct Prava {
    long long a;
    long long b;
    long long x;
    int ind;
};
vector <Prava> env[MAXK];
stack <int> st;
int br[MAXK];
long long a[MAXN], pref[MAXN], dp[MAXN][MAXK];
int ind[MAXN][MAXK];
bool check (long long a1, long long b1, long long a2, long long b2, long long a3, long long b3) {
    return (a1-a2)*(b3-b1)<=(a1-a3)*(b2-b1);
}
long long find_intersection(long long a1, long long b1, long long a2, long long b2) {
    long long da, db;
    da=a1-a2; db=b2-b1;
    if (da==0) return INF;
    return (db+da-1)/da;
}
void add(int k, long long a, long long b, int ind) {
    if (env[k].empty()) {
        env[k].push_back({a,b,-INF,ind});
        return;
    }
    long long a1, b1, a2, b2, x;
    int i;
    while (env[k].size()>1) {
        i=env[k].size();
        a1=env[k][i-2].a; b1=env[k][i-2].b;
        a2=env[k][i-1].a; b2=env[k][i-1].b;
        if (!check(a,b,a1,b1,a2,b2)) break;
        if (i-1==br[k]) br[k]--;
        env[k].pop_back();
    }
    i=env[k].size();
    a1=env[k][i-1].a; b1=env[k][i-1].b;
    x=find_intersection(a1,b1,a,b);
    env[k].push_back({a,b,x,ind});
}
int main () {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, k;
    cin >> n >> k;
    for (int i=1;i<=n;i++) {
        cin >> a[i];
        pref[i]=pref[i-1]+a[i];
    }
    /*long long s;
    for (int i=1;i<=n;i++) {
        for (int j=min(i,k+1);j>1;j--) {
            dp[i][j]=-1;
            for (int p=i-1;p>=j-1;p--) {
                s=dp[p][j-1]+(pref[i]-pref[p])*pref[p];
                if (s>dp[i][j]) {
                    dp[i][j]=s;
                    ind[i][j]=p;
                }
            }
        }
        dp[i][1]=0;
        ind[i][1]=-1;
    }
    dp[n][k+1]=-dp[n][k+1];*/
    ind[0][1]=-1;
    for (int i=1;i<=n;i++) {
        /*if (a[i]==0) {
            for (int j=min(i,k+1);j>=1;j--) {
                if (j==i) {
                    dp[i][j]=dp[i-1][j-1];
                    ind[i][j]=i-1;
                    add(j,-pref[i],dp[i][j]+pref[i]*pref[i],i);
                    continue;
                }
                dp[i][j]=dp[i-1][j];
                ind[i][j]=ind[i-1][j];
            }
            continue;
        }*/
        for (int j=min(i,k+1);j>1;j--) {
            while (br[j-1]<env[j-1].size() && env[j-1][br[j-1]].x<=pref[i]) br[j-1]++;
            br[j-1]--;
            ind[i][j]=env[j-1][br[j-1]].ind;
            dp[i][j]=env[j-1][br[j-1]].a*pref[i]+env[j-1][br[j-1]].b;
            add(j,-pref[i],dp[i][j]+pref[i]*pref[i],i);
            ///cout << i << ' ' << j << ' ' << dp[i][j] << endl;
        }
        add(1,-pref[i],pref[i]*pref[i],i);
        ind[i][1]=-1;
        dp[i][0]=0;
    }
    cout << -dp[n][k+1] << endl;
    int i=ind[n][k+1], curk=k;
    while (curk>=1) {
        st.push(i);
        i=ind[i][curk];
        curk--;
    }
    while (!st.empty()) {
        cout << st.top();
        st.pop();
        if (!st.empty()) cout << ' ';
    }
    cout << endl;
    return 0;
}

Compilation message

sequence.cpp: In function 'int main()':
sequence.cpp:90:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Prava>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |             while (br[j-1]<env[j-1].size() && env[j-1][br[j-1]].x<=pref[i]) br[j-1]++;
      |                    ~~~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB contestant found the optimal answer: 108 == 108
2 Correct 1 ms 4444 KB contestant found the optimal answer: 999 == 999
3 Correct 1 ms 4444 KB contestant found the optimal answer: 0 == 0
4 Correct 1 ms 4444 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 1 ms 4444 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 1 ms 4440 KB contestant found the optimal answer: 1 == 1
7 Correct 1 ms 4552 KB contestant found the optimal answer: 1 == 1
8 Correct 1 ms 4444 KB contestant found the optimal answer: 1 == 1
9 Correct 1 ms 4444 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 0 ms 4444 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 1 ms 4444 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 1 ms 4456 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 1 ms 4444 KB contestant found the optimal answer: 140072 == 140072
14 Correct 1 ms 4548 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 1 ms 4444 KB contestant found the optimal answer: 805 == 805
16 Correct 1 ms 4444 KB contestant found the optimal answer: 900189994 == 900189994
17 Correct 1 ms 4444 KB contestant found the optimal answer: 999919994 == 999919994
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 1 ms 4444 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 1 ms 4444 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 1 ms 4444 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 1 ms 4444 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 1 ms 4444 KB contestant found the optimal answer: 933702 == 933702
7 Correct 1 ms 4444 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 1 ms 4444 KB contestant found the optimal answer: 687136 == 687136
9 Correct 1 ms 4444 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 1 ms 4444 KB contestant found the optimal answer: 29000419931 == 29000419931
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 1 ms 4700 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 2 ms 5468 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 1 ms 4700 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 1 ms 5212 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 1 ms 5212 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 1 ms 5468 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 1 ms 4956 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 1 ms 4956 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 1 ms 4956 KB contestant found the optimal answer: 490686959791 == 490686959791
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6748 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 1 ms 6748 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 7 ms 13148 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 1 ms 6744 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 8 ms 13044 KB contestant found the optimal answer: 679388326 == 679388326
6 Correct 6 ms 11792 KB contestant found the optimal answer: 4699030287 == 4699030287
7 Correct 7 ms 13148 KB contestant found the optimal answer: 12418819758185 == 12418819758185
8 Correct 7 ms 13148 KB contestant found the optimal answer: 31093317350 == 31093317350
9 Correct 3 ms 7944 KB contestant found the optimal answer: 12194625429236 == 12194625429236
10 Correct 4 ms 9308 KB contestant found the optimal answer: 12345131038664 == 12345131038664
# Verdict Execution time Memory Grader output
1 Correct 5 ms 28368 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 5 ms 28116 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Correct 90 ms 95600 KB contestant found the optimal answer: 4973126687469639 == 4973126687469639
4 Correct 5 ms 28608 KB contestant found the optimal answer: 3748491676694116 == 3748491676694116
5 Correct 46 ms 57984 KB contestant found the optimal answer: 1085432199 == 1085432199
6 Correct 57 ms 54576 KB contestant found the optimal answer: 514790755404 == 514790755404
7 Correct 71 ms 77180 KB contestant found the optimal answer: 1256105310476641 == 1256105310476641
8 Correct 58 ms 67352 KB contestant found the optimal answer: 3099592898816 == 3099592898816
9 Correct 70 ms 71752 KB contestant found the optimal answer: 1241131419367412 == 1241131419367412
10 Correct 83 ms 83708 KB contestant found the optimal answer: 1243084101967798 == 1243084101967798
# Verdict Execution time Memory Grader output
1 Runtime error 34 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -