Submission #107914

# Submission time Handle Problem Language Result Execution time Memory
107914 2019-04-26T09:01:09 Z SamAnd Split the sequence (APIO14_sequence) C++17
71 / 100
1053 ms 132096 KB
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const long long INF = 1000000009000000009;

int n, m;
int a[N];
long long p[N];

long long dp[N][202];
int pp[N][202];

vector<int> v[202];
vector<long double> x[202];

long double hat(long long b1, long long k1, long long b2, long long k2)
{
    return 1.0 * (b2 - b1 + 0.0) / (k1 - k2 + 0.0);
}

void ave(int k, int j)
{
    while (!v[k].empty() && p[v[k].back()] == p[j])
    {
        v[k].pop_back();
        if (!x[k].empty())
            x[k].pop_back();
    }
    while (v[k].size() >= 2)
    {
        long double x1 = hat(dp[v[k].back()][k] - p[v[k].back()] * p[v[k].back()], p[v[k].back()], dp[j][k] - p[j] * p[j], p[j]);
        if (x1 > x[k].back())
            break;
        v[k].pop_back();
        x[k].pop_back();
    }
    if (v[k].empty())
    {
        v[k].push_back(j);
        return;
    }
    long double x1 = hat(dp[v[k].back()][k] - p[v[k].back()] * p[v[k].back()], p[v[k].back()], dp[j][k] - p[j] * p[j], p[j]);
    x[k].push_back(x1);
    v[k].push_back(j);
}

int byn(int i, int k)
{
    int l = 0, r = x[k - 1].size() - 1;
    while ((r - l) > 3)
    {
        int m = (l + r) / 2;
        if (p[i] >= x[k - 1][m])
            l = m;
        else
            r = m;
    }
    for (int m = r; m >= l; --m)
    {
        if (p[i] >= x[k - 1][m])
            return v[k - 1][m + 1];
    }
    return v[k - 1][0];
}

int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; ++i)
        cin >> a[i];
    for (int i = 1; i <= n; ++i)
        p[i] = p[i - 1] + a[i];
    for (int i = 1; i <= n; ++i)
    {
        for (int k = 1; k <= min(m, i - 1); ++k)
        {
            int j = byn(i, k);
            dp[i][k] = dp[j][k - 1] - p[j] * p[j] + p[j] * p[i];
            pp[i][k] = j;
        }
        for (int k = 0; k <= min(m, i - 1); ++k)
        {
            ave(k, i);
        }
    }
    cout << dp[n][m] << endl;
    int i = n;
    for (int k = m; k > 0; --k)
    {
        cout << pp[i][k] << ' ';
        i = pp[i][k];
    }
    cout << endl;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB contestant found the optimal answer: 108 == 108
2 Correct 3 ms 384 KB contestant found the optimal answer: 999 == 999
3 Correct 2 ms 384 KB contestant found the optimal answer: 0 == 0
4 Correct 2 ms 384 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 2 ms 384 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 3 ms 384 KB contestant found the optimal answer: 1 == 1
7 Correct 2 ms 384 KB contestant found the optimal answer: 1 == 1
8 Correct 24 ms 384 KB contestant found the optimal answer: 1 == 1
9 Correct 2 ms 384 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 2 ms 384 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 2 ms 384 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 3 ms 384 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 3 ms 384 KB contestant found the optimal answer: 140072 == 140072
14 Correct 2 ms 384 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 2 ms 384 KB contestant found the optimal answer: 805 == 805
16 Correct 3 ms 384 KB contestant found the optimal answer: 900189994 == 900189994
17 Correct 3 ms 384 KB contestant found the optimal answer: 999919994 == 999919994
# Verdict Execution time Memory Grader output
1 Correct 4 ms 512 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 2 ms 512 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 3 ms 512 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 3 ms 512 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 3 ms 512 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 3 ms 512 KB contestant found the optimal answer: 933702 == 933702
7 Correct 3 ms 512 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 2 ms 512 KB contestant found the optimal answer: 687136 == 687136
9 Correct 3 ms 508 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 3 ms 512 KB contestant found the optimal answer: 29000419931 == 29000419931
# Verdict Execution time Memory Grader output
1 Correct 2 ms 896 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 3 ms 896 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 5 ms 1408 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 4 ms 896 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 5 ms 1280 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 5 ms 1280 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 5 ms 1408 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 3 ms 1024 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 3 ms 1024 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 3 ms 1152 KB contestant found the optimal answer: 490686959791 == 490686959791
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2816 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 4 ms 2880 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 49 ms 6912 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 5 ms 2816 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 66 ms 7032 KB contestant found the optimal answer: 679388326 == 679388326
6 Correct 35 ms 6264 KB contestant found the optimal answer: 4699030287 == 4699030287
7 Correct 64 ms 7076 KB contestant found the optimal answer: 12418819758185 == 12418819758185
8 Correct 51 ms 6904 KB contestant found the optimal answer: 31093317350 == 31093317350
9 Correct 10 ms 3584 KB contestant found the optimal answer: 12194625429236 == 12194625429236
10 Correct 18 ms 4480 KB contestant found the optimal answer: 12345131038664 == 12345131038664
# Verdict Execution time Memory Grader output
1 Correct 28 ms 24928 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 26 ms 24952 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Correct 1053 ms 70288 KB contestant found the optimal answer: 4973126687469639 == 4973126687469639
4 Correct 38 ms 25304 KB contestant found the optimal answer: 3748491676694116 == 3748491676694116
5 Correct 447 ms 45816 KB contestant found the optimal answer: 1085432199 == 1085432199
6 Correct 388 ms 45048 KB contestant found the optimal answer: 514790755404 == 514790755404
7 Correct 644 ms 59260 KB contestant found the optimal answer: 1256105310476641 == 1256105310476641
8 Correct 454 ms 52876 KB contestant found the optimal answer: 3099592898816 == 3099592898816
9 Correct 609 ms 55784 KB contestant found the optimal answer: 1241131419367412 == 1241131419367412
10 Correct 824 ms 64356 KB contestant found the optimal answer: 1243084101967798 == 1243084101967798
# Verdict Execution time Memory Grader output
1 Runtime error 176 ms 132096 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -