Submission #855477

# Submission time Handle Problem Language Result Execution time Memory
855477 2023-10-01T09:52:37 Z Pragmatism Split the sequence (APIO14_sequence) C++17
50 / 100
811 ms 131072 KB
//Bismillahir-Rahmanir-Rahim

#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")

#define pb push_back
#define pii pair <int, int>
#define pll pair <long long, long long>
#define pld pair <long double, long double>
#define ll long long
#define ld long double
#define x first
#define y second
#define all(v) v.begin(),v.end()
#define sz(s) (int)s.size()
#define skip continue
#define bpop(x) (ll)__builtin_popcountll(x)

using namespace std;

const int N = 1e5 + 7;
const int M = 1e2 + 7;
const int MAXA = 5e4 + 7;
const int inf = 1e9 + 7;
const ll INF = 1e18 + 7;
const ll MOD = 1e9 + 7;

pii dir[] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

#define int long long

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n, k, a[N], pre[N], pr[N][207], dp[N][207];
struct chtDynamic {
    struct line {
        int m, b, val, pos;
        double x;
        bool query;
        line(int _m = 0, int _b = 0, int i = 0): m(_m), b(_b), val(0), pos(i), x(0), query(false) {}
        int eval(int x) {
            return m * x + b;
        }
        bool parallel(const line &other) const {
            return m == other.m;
        }
        ld intersect(const line &other) const {
          return (parallel(other) ? inf : 1.0 * (other.b - b) / (m - other.m));
        }
        bool operator < (const line &other) const {
          if(other.query) return x < other.val;
          else return m < other.m;
        }
    };
    set<line> hull;
    bool cPrev(auto it) { return it != hull.begin(); }
    bool cNext(auto it) { return it != hull.end() && next(it) != hull.end(); }
    bool bad(const line &l1, const line &l2, const line &l3) {
        return l1.intersect(l3) <= l1.intersect(l2);
    }
    bool bad(auto it) {
        return cPrev(it) && cNext(it) && bad(*prev(it), *it, *next(it));
    }
    auto update(auto it) {
        if(!cPrev(it)) return it;
        ld x = it -> intersect(*prev(it));
        line tmp(*it); tmp.x = x;
        it = hull.erase(it);
        return hull.insert(it, tmp);
    }
    void addLine(int m, int b, int pos) {
        line l(m, b, pos);
        auto it = hull.lower_bound(l);
        if(it != hull.end() && l.parallel(*it)) {
            if(it -> b < b) it = hull.erase(it);
            else return;
        }
        it = hull.insert(it, l);
        if(bad(it)) return (void) hull.erase(it);
        while(cPrev(it) && bad(prev(it))) hull.erase(prev(it));
        while(cNext(it) && bad(next(it))) hull.erase(next(it));
        it = update(it);
        if(cPrev(it)) update(prev(it));
        if(cNext(it)) update(next(it));
    }
    pii get(int x) const {
        if(hull.empty())return {0, 1};
        line q;
        q.val = x, q.query = 1;
        auto it = --hull.lower_bound(q);
        line opt = *it;
        return {opt.eval(x), opt.pos};
    }
}convex[207];
void solve() {
    cin >> n >> k;
    for (int i = 1;i <= n;i++)cin >> a[i], pre[i] = pre[i - 1] + a[i];
    convex[1].addLine(pre[1], -pre[1] * pre[1], 1);
    for (int i = 2;i <= n;i++) {
        for (int j = min(i, k + 1);j >= 2;j--) {
            pii p = convex[j - 1].get(pre[i]);
            if (p.x >= dp[i][j])dp[i][j] = p.x, pr[i][j] = p.y;
            convex[j].addLine(pre[i], dp[i][j] - pre[i] * pre[i], i);
        }
        convex[1].addLine(pre[i], -pre[i] * pre[i], i);
    }
    int ans = dp[n][k + 1];
    cout << ans << '\n';
    int pos = n, cur = k + 1;
    vector <int> path;
    while (cur > 0) {
        pos = pr[pos][cur], cur--;
        if (pos)path.pb(pos);
    }
    reverse(all(path));
    for (auto to : path)cout << to << ' ';
}
signed main() {
    //srand(time(NULL));
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen("milkorder.in", "r", stdin);
    //freopen("milkorder.out", "w", stdout);
    int test = 1;
    //cin >> test;
    for (int i = 1;i <= test;i++) {
        //cout << "Case " << i << ": ";
        solve();
    }
    return 0;
}

Compilation message

sequence.cpp:4: warning: ignoring '#pragma comment ' [-Wunknown-pragmas]
    4 | #pragma comment(linker, "/stack:200000000")
      | 
sequence.cpp:58:16: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   58 |     bool cPrev(auto it) { return it != hull.begin(); }
      |                ^~~~
sequence.cpp:59:16: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   59 |     bool cNext(auto it) { return it != hull.end() && next(it) != hull.end(); }
      |                ^~~~
sequence.cpp:63:14: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   63 |     bool bad(auto it) {
      |              ^~~~
sequence.cpp:66:17: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   66 |     auto update(auto it) {
      |                 ^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB contestant found the optimal answer: 108 == 108
2 Correct 1 ms 2396 KB contestant found the optimal answer: 999 == 999
3 Correct 1 ms 2396 KB contestant found the optimal answer: 0 == 0
4 Correct 1 ms 2396 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 1 ms 2392 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 0 ms 2396 KB contestant found the optimal answer: 1 == 1
7 Correct 1 ms 2540 KB contestant found the optimal answer: 1 == 1
8 Correct 0 ms 2396 KB contestant found the optimal answer: 1 == 1
9 Correct 0 ms 2520 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 1 ms 2396 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 1 ms 2396 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 1 ms 2396 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 1 ms 2396 KB contestant found the optimal answer: 140072 == 140072
14 Correct 1 ms 2396 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 0 ms 2396 KB contestant found the optimal answer: 805 == 805
16 Correct 1 ms 2396 KB contestant found the optimal answer: 900189994 == 900189994
17 Correct 1 ms 2536 KB contestant found the optimal answer: 999919994 == 999919994
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 1 ms 2396 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 1 ms 2652 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 1 ms 2392 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 1 ms 2652 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 1 ms 2396 KB contestant found the optimal answer: 933702 == 933702
7 Correct 1 ms 2652 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 1 ms 2652 KB contestant found the optimal answer: 687136 == 687136
9 Correct 1 ms 2396 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 1 ms 2648 KB contestant found the optimal answer: 29000419931 == 29000419931
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4956 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 1 ms 4956 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 7 ms 6748 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 1 ms 4956 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 5 ms 6232 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 6 ms 6236 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 7 ms 6460 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 4 ms 5468 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 2 ms 5208 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 4 ms 5720 KB contestant found the optimal answer: 490686959791 == 490686959791
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6492 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 3 ms 6492 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 90 ms 22940 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 3 ms 6492 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 67 ms 18836 KB contestant found the optimal answer: 679388326 == 679388326
6 Correct 60 ms 15956 KB contestant found the optimal answer: 4699030287 == 4699030287
7 Correct 82 ms 22096 KB contestant found the optimal answer: 12418819758185 == 12418819758185
8 Correct 90 ms 22956 KB contestant found the optimal answer: 31093317350 == 31093317350
9 Correct 16 ms 9564 KB contestant found the optimal answer: 12194625429236 == 12194625429236
10 Correct 29 ms 12520 KB contestant found the optimal answer: 12345131038664 == 12345131038664
# Verdict Execution time Memory Grader output
1 Correct 23 ms 38236 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 23 ms 37456 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Runtime error 811 ms 131072 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 96 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -