Submission #891939

# Submission time Handle Problem Language Result Execution time Memory
891939 2023-12-24T13:33:32 Z abysmal Split the sequence (APIO14_sequence) C++14
0 / 100
2000 ms 90688 KB
#include<iostream>
#include<stdio.h>
#include<stdint.h>
#include<vector>
#include<algorithm>
#include<utility>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<iomanip>
#include<string>
#include<math.h>
#include<assert.h>

using namespace std;

const double PI = (double) atan(1.0) * 4;
const int64_t INF = (int64_t) 1e18 + 777;
const int64_t mINF = (int64_t) 2e9 + 777;
const int64_t offset = (int64_t) 1e6 + 777;
const int64_t MOD = 1e9 + 7;
const int nbit = 11;
const int ndig = 10;
const int nchar = 26;
const int p1 = 31;
const int p2 = 53;
const int D = 4;
int dr[D] = {0, 1, 0, -1};
int dc[D] = {1, 0, -1, 0};
// 0 right // 1 down // 2 left // 3 up
bool Q = false;

int modadd(int t1, int t2)
{
    int res = t1 + t2;
    if(res >= MOD) res -= MOD;

    return res;
}

int modmul(int t1, int t2)
{
    int64_t res = 1LL * t1 * t2;
    return (res % MOD);
}

struct Line
{
    int64_t k; int64_t m;
    int id;
    mutable int64_t p;

    bool operator < (const Line& o) const
    {
        if(Q) return p < o.p;
        return k < o.k;
    }
};

struct LineContainer : multiset<Line, less<> >
{
    int64_t div(int64_t a, int64_t b)
    {
        return a / b - ((a ^ b) < 0 && (a % b));
    }

    bool isect(iterator x, iterator y)
    {
        if(y == end())
        {
            x->p = INF;
            return false;
        }

        if(x->k == y->k)
        {
            if(x->m > y->m) x->p = INF;
            else x->p = -INF;
        }
        else x->p = div(y->m - x->m, x->k - y->k);

        return x->p >= y->p;
    }

    void add(int64_t k, int64_t m, int i)
    {
        auto z = insert({k, m, i, 0});
        auto y = z++;
        auto x = y;

        while(isect(y, z)) z = erase(z);
        if(x != begin() && isect(--x, y)) erase(x, y = erase(y));

        while((y = x) != begin() && (--x)->p >= y->p) isect(x, erase(y));
    }

    pair<int64_t, int> query(int64_t x)
    {
        assert(!empty());

        Q = true;
        auto l = *lower_bound({0, 0, 0, x});
        Q = false;

        return {l.k * x + l.m, l.id};
    }
};

struct Solution
{
    int n; int k;
    vector<int> a;
    Solution() {}

    void solve()
    {
        cin >> n >> k;
        a.resize(n);
        for(int i = 0; i < n; i++)
        {
            cin >> a[i];
        }

        vector<int64_t> prefix(n + 1, 0);
        vector<int64_t> suffix(n + 2, 0);
        for(int i = 1; i <= n; i++)
        {
            prefix[i] = prefix[i - 1] + a[i - 1];
        }
        for(int i = n; i >= 1; i--)
        {
            suffix[i] = suffix[i + 1] + a[i - 1];
        }

        vector<int64_t> dp(n + 1, -INF);
        vector<int64_t> f(n + 1, -INF);
        vector<vector<int> > trace(k, vector<int>(n + 1, -1));
        dp[0] = 0;
        int e = -1;
        int64_t ans = -INF;
        for(int t = 0; t < k; t++)
        {
            LineContainer lc;
            for(int i = 0; i <= n; i++)
            {
                f[i] = dp[i];
                int64_t p = prefix[i];
                int64_t suf = suffix[i + 1];
                if(!lc.empty())
                {
                    pair<int64_t, int> pr = lc.query(suf);
                    int64_t val = pr.first;
                    int id = pr.second;

                    f[i] = max(f[i], val + p * suf);
                    trace[t][i] = id;
                }

                if(ans < f[i])
                {
                    ans = f[i];
                    e = i;
                }
                lc.add(-p, dp[i], i);
            }

            swap(f, dp);
        }

        cout << ans << "\n";
        vector<int> path;
        for(int i = k - 1; i >= 0; i--)
        {
            path.push_back(e);
            e = trace[i][e];
        }

        reverse(path.begin(), path.end());
        for(int i = 0; i < k; i++)
        {
            cout << path[i] << " ";
        }
    }

    int modsub(int t1, int t2)
    {
        int res = t1 - t2;
        if(res < 0) res += MOD;

        return res;
    }

    bool BIT(int& mask, int& j)
    {
        return (mask & MASK(j));
    }

    int64_t Abs(int64_t t1)
    {
        if(t1 < 0) return -t1;
        return t1;
    }

    int MASK(int j)
    {
        return (1 << j);
    }
};

void __startup()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
}

int main()
{
    __startup();
    int t = 1;
//    cin >> t;

    for(int i = 1; i <= t; i++)
    {
        Solution().solve();
    }

    return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB contestant found the optimal answer: 108 == 108
2 Correct 0 ms 348 KB contestant found the optimal answer: 999 == 999
3 Incorrect 0 ms 348 KB Integer 0 violates the range [1, 1]
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 1 ms 344 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 1 ms 348 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 1 ms 348 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 1 ms 348 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 0 ms 348 KB contestant found the optimal answer: 933702 == 933702
7 Correct 1 ms 348 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Incorrect 0 ms 348 KB contestant didn't find the optimal answer: 681371 < 687136
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 1 ms 348 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 4 ms 604 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 1 ms 344 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 3 ms 348 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 4 ms 604 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 3 ms 456 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Incorrect 1 ms 348 KB contestant didn't find the optimal answer: 193456081 < 193556962
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 1 ms 348 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 19 ms 1116 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 1 ms 348 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 22 ms 1116 KB contestant found the optimal answer: 679388326 == 679388326
6 Correct 18 ms 1116 KB contestant found the optimal answer: 4699030287 == 4699030287
7 Correct 20 ms 1116 KB contestant found the optimal answer: 12418819758185 == 12418819758185
8 Incorrect 19 ms 1112 KB contestant didn't find the optimal answer: 31092578772 < 31093317350
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1368 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 4 ms 1372 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Correct 295 ms 9452 KB contestant found the optimal answer: 4973126687469639 == 4973126687469639
4 Correct 6 ms 1760 KB contestant found the optimal answer: 3748491676694116 == 3748491676694116
5 Correct 164 ms 6128 KB contestant found the optimal answer: 1085432199 == 1085432199
6 Correct 198 ms 6944 KB contestant found the optimal answer: 514790755404 == 514790755404
7 Correct 197 ms 7488 KB contestant found the optimal answer: 1256105310476641 == 1256105310476641
8 Incorrect 174 ms 6384 KB contestant didn't find the optimal answer: 3099384768442 < 3099592898816
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 51 ms 11212 KB contestant found the optimal answer: 19795776960 == 19795776960
2 Correct 50 ms 11468 KB contestant found the optimal answer: 19874432173 == 19874432173
3 Execution timed out 2066 ms 90688 KB Time limit exceeded
4 Halted 0 ms 0 KB -