Submission #566190

# Submission time Handle Problem Language Result Execution time Memory
566190 2022-05-22T04:47:16 Z quicksloth Split the sequence (APIO14_sequence) C++17
71 / 100
374 ms 82536 KB
#include <iostream>
using namespace std;
#define maxn 100000
#define maxk 200
#define ll long long

int best[maxn][maxk];
//arr is prefix sums

int main()
{
    ll dp[maxn], prv[maxn], arr[maxn];
    //prv is not quite necessary
    int n, k;
    cin >> n >> k;
    k++;
    //array endgth n, split into k+1 intervals, each time points = product of sums of two sides
    //if decided how intervals will be picked: then how to arrange which to select?
    //answer for a set of intervals = sum ab where a, b are distinct in it
    //true by induction on set's size
    //= ((sum a)^2 - sum a^2)/2 where first sum is known (sum of array) and second is split up
    //divide up the array optimally to minimize sum (segment sum)^2? dp with prefix sums
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
        if (i) arr[i] += arr[i-1];
        //1 interval
        dp[i] = arr[i]*arr[i];
        best[i][0] = -1;
        //cut at -1 means this is final interval
    }
    int stack[maxn];
    ll cut[maxn];
    cut[0] = 0;
    //stack[0] is best from cut[0] to cut[1]-1, ...
    int end, start;
    for (int t = 1; t < k; t++)
    {
        //t+1 intervals to make 0..i-1; if t = 0 then just interval sum ^ 2
        //else: dp[i] = min_x<i arr[i]^2 - 2*arr[x]*arr[i] + arr[x]^2 + prv[x]
        //with x >= t-1 (i >= t to make it well defined)
        //do all of dp[?] in O(n)? need to insert another x or query best x for given i
        //monotonic stack: record pair<int,ll>: best value of x for arr[i] <= that ll
        for (int i = 0; i < n; i++) prv[i] = dp[i];
        end = 0;
        start = 0;
        for (int i = t; i < n; i++)
        {
            //x = i-1: insert it into the monotonic stack
            ll x = 0;
            bool special = false;
            while (start != end)
            {
                //see when it beats the last function, then truncate
                //definitely should work at some value of arr[i]: prv is increasing not too fast
                //just assume it ought to work
                //solve for smallest int x:
//arr[stack[end-1]]*arr[stack[end-1]]+prv[stack[end-1]]-2*arr[stack[end-1]]*x >
//arr[i-1]*arr[i-1]+prv[i-1]-2*arr[i-1]*x
                //or tmp2x > tmp1 or x = ceil of (tmp1+1)/tmp2
                //just take floor and +1 if needed
                if (arr[i-1] == arr[stack[end-1]])
                {
                    //impossible to beat previous function, so done
                    special = true;
                    break;
                }
ll tmp1 = arr[i-1]*arr[i-1]+prv[i-1]-arr[stack[end-1]]*arr[stack[end-1]]-prv[stack[end-1]],
   tmp2 = 2*arr[i-1]-2*arr[stack[end-1]];
                x = (tmp1+1)/tmp2;
                if ((tmp1+1)%tmp2) x++;
                //if at some point it is optimal, it will continue to be optimal later
                if (x > cut[end-1])
                {
                    //previous one is sometimes optimal so cannot discard
                    break;
                }
                end--;
            }
            if (!special)
            {
                stack[end] = i-1;
                cut[end] = x;
                end++;
                cut[end] = 1e18;
            }
            //the last curve is always optimal, but previous one is up to x-1 only
            //now find which curve is best for arr[i]: pop from front of the stack
            while (start != end)
            {
                if (cut[start+1] > arr[i]) break;
                start++;
            }
            best[i][t] = stack[start];
            dp[i] = (arr[i]-arr[stack[start]])*(arr[i]-arr[stack[start]])+prv[stack[start]];
        }
    }
    //now dp[n-1] is the answer
    cout << (arr[n-1]*arr[n-1]-dp[n-1])/2 << '\n';
    int cur = n-1;
    while (k)
    {
        cur = best[cur][--k];
        if (cur != -1) cout << cur+1 << ' ';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3796 KB contestant found the optimal answer: 108 == 108
2 Correct 2 ms 3796 KB contestant found the optimal answer: 999 == 999
3 Correct 3 ms 3796 KB contestant found the optimal answer: 0 == 0
4 Correct 2 ms 3828 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 2 ms 3796 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 2 ms 3796 KB contestant found the optimal answer: 1 == 1
7 Correct 2 ms 3796 KB contestant found the optimal answer: 1 == 1
8 Correct 2 ms 3796 KB contestant found the optimal answer: 1 == 1
9 Correct 2 ms 3796 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 2 ms 3796 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 2 ms 3796 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 2 ms 3796 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 2 ms 3796 KB contestant found the optimal answer: 140072 == 140072
14 Correct 2 ms 3820 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 2 ms 3796 KB contestant found the optimal answer: 805 == 805
16 Correct 2 ms 3796 KB contestant found the optimal answer: 900189994 == 900189994
17 Correct 2 ms 3796 KB contestant found the optimal answer: 999919994 == 999919994
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3796 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 2 ms 3796 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 2 ms 3796 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 2 ms 3824 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 2 ms 3796 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 2 ms 3796 KB contestant found the optimal answer: 933702 == 933702
7 Correct 2 ms 3796 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 2 ms 3796 KB contestant found the optimal answer: 687136 == 687136
9 Correct 2 ms 3828 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 2 ms 3824 KB contestant found the optimal answer: 29000419931 == 29000419931
# Verdict Execution time Memory Grader output
1 Correct 3 ms 3924 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 2 ms 3924 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 2 ms 3948 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 2 ms 3924 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 3 ms 3924 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 3 ms 3924 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 2 ms 3924 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 2 ms 3956 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 2 ms 3924 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 2 ms 3924 KB contestant found the optimal answer: 490686959791 == 490686959791
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4564 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 2 ms 4596 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 4 ms 4596 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 3 ms 4564 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 5 ms 4564 KB contestant found the optimal answer: 679388326 == 679388326
6 Correct 5 ms 4564 KB contestant found the optimal answer: 4699030287 == 4699030287
7 Correct 6 ms 4600 KB contestant found the optimal answer: 12418819758185 == 12418819758185
8 Correct 5 ms 4564 KB contestant found the optimal answer: 31093317350 == 31093317350
9 Correct 3 ms 4596 KB contestant found the optimal answer: 12194625429236 == 12194625429236
10 Correct 3 ms 4564 KB contestant found the optimal answer: 12345131038664 == 12345131038664
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11672 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 6 ms 11604 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Correct 27 ms 11648 KB contestant found the optimal answer: 4973126687469639 == 4973126687469639
4 Correct 8 ms 11732 KB contestant found the optimal answer: 3748491676694116 == 3748491676694116
5 Correct 25 ms 11572 KB contestant found the optimal answer: 1085432199 == 1085432199
6 Correct 38 ms 11604 KB contestant found the optimal answer: 514790755404 == 514790755404
7 Correct 29 ms 11604 KB contestant found the optimal answer: 1256105310476641 == 1256105310476641
8 Correct 20 ms 11696 KB contestant found the optimal answer: 3099592898816 == 3099592898816
9 Correct 23 ms 11612 KB contestant found the optimal answer: 1241131419367412 == 1241131419367412
10 Correct 26 ms 11648 KB contestant found the optimal answer: 1243084101967798 == 1243084101967798
# Verdict Execution time Memory Grader output
1 Correct 48 ms 82208 KB contestant found the optimal answer: 19795776960 == 19795776960
2 Correct 49 ms 82224 KB contestant found the optimal answer: 19874432173 == 19874432173
3 Incorrect 374 ms 82536 KB Extra information in the output file
4 Halted 0 ms 0 KB -