Submission #276020

# Submission time Handle Problem Language Result Execution time Memory
276020 2020-08-20T09:48:54 Z toonewbie Split the sequence (APIO14_sequence) C++17
33 / 100
268 ms 131076 KB
#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back
//#define endl '\n'
#define ll long long
#define pll pair<ll,ll>
#define pi pair<int,int>

using namespace std;

const int MOD = 1e9 + 7;
const ll INF = 1e17;
const int MAXN = 1e9+1;
const int N = 100005;

struct Line{
    ll m, b, id;
    ll eval(ll x) {
        return m*x + b;
    }
};
struct node{
    node *l, *r;
    Line ln;
    node(node* _l, node* _r, Line _ln):
        l(_l), r(_r), ln(_ln) {};
};
typedef node* pnode;

Line get(pnode rt) {
    return (!rt ? Line({0, -INF, -1}) : rt -> ln);
}
void add(pnode &rt, Line ln, int l=0, int r=MAXN, int v = 1) {
    if (!rt) rt = new node(NULL, NULL, Line({0, -INF, -1}));
    int m = (l + r) >> 1;
    bool left = ln.eval(l) > get(rt).eval(l);
    bool mid = ln.eval(m) > get(rt).eval(m);
    if (mid) swap(ln, rt -> ln);
    if (r - l == 1) return;
    if (left != mid) add(rt -> l, ln, l, m, v << 1);
    else add(rt -> r, ln, m, r, v << 1 | 1);
}

pll get(pnode &rt, ll x, int l=0, int r=MAXN, int v=1) {
    if (rt == NULL) rt = new node(NULL, NULL, Line({0, -INF, -1}));
    int m = (l + r) >> 1;
    pll now = {get(rt).eval(x), get(rt).id};
    if (r - l == 1) return now;
    if (x < m) return max(now, get(rt -> l, x, l, m, v << 1));
    else return max(now, get(rt -> r, x, m, r, v << 1 | 1));
}

ll dp[202][N];
int opt[202][N], ps[N];
pnode rt;
int main() {
    ios_base :: sync_with_stdio(0);
    cin.tie(0);
    int n, K;
    cin >> n >> K; K++;
    for (int i = 1, x; i <= n; i++) {
        cin >> x;
        ps[i] = ps[i - 1] + x;
        dp[0][i] = -INF;
    }
    for (int i = 1; i <= K; i++) {
        dp[i][0] = -INF;
    }
    dp[0][0] = 0;
    pll ans;
    for (int k = 1; k <= K; k++) {
        rt = NULL;
        if (k == 1) add(rt, Line({0, 0, 0}));
        for (int i = 1; i <= n; i++) {
            //cout << "getting ans for " << ps[i] << endl;
            ans = get(rt, ps[i]);
            dp[k][i] = ans.F;
            opt[k][i] = ans.S;
            //cout << k << " " << i << " " << opt[k][i] << endl;
            add(rt, Line({ps[i], dp[k - 1][i] - 1ll*ps[i]*ps[i], i}));
        }
    }
    cout << dp[K][n] << endl;
    vector<int> res;
    int id = n, ptr = K;
    while(res.size() < K - 1) {
        res.pb(opt[ptr][id]);
        id = opt[ptr--][id];
    }
    reverse(res.begin(), res.end());
    for (int i : res) cout << i << " ";
    return 0;
}

Compilation message

sequence.cpp: In function 'int main()':
sequence.cpp:88:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   88 |     while(res.size() < K - 1) {
      |           ~~~~~~~~~~~^~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB contestant found the optimal answer: 108 == 108
2 Correct 1 ms 384 KB contestant found the optimal answer: 999 == 999
3 Correct 1 ms 384 KB contestant found the optimal answer: 0 == 0
4 Correct 1 ms 512 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 1 ms 640 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 1 ms 384 KB contestant found the optimal answer: 1 == 1
7 Correct 1 ms 384 KB contestant found the optimal answer: 1 == 1
8 Correct 1 ms 384 KB contestant found the optimal answer: 1 == 1
9 Correct 1 ms 384 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 1 ms 384 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 1 ms 512 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 1 ms 512 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 1 ms 384 KB contestant found the optimal answer: 140072 == 140072
14 Correct 1 ms 384 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 1 ms 512 KB contestant found the optimal answer: 805 == 805
16 Correct 1 ms 384 KB contestant found the optimal answer: 900189994 == 900189994
17 Correct 1 ms 512 KB contestant found the optimal answer: 999919994 == 999919994
# Verdict Execution time Memory Grader output
1 Correct 1 ms 504 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 1 ms 512 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 5 ms 3712 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 1 ms 640 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 1 ms 768 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 1 ms 640 KB contestant found the optimal answer: 933702 == 933702
7 Correct 3 ms 2048 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 1 ms 896 KB contestant found the optimal answer: 687136 == 687136
9 Correct 1 ms 768 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 2 ms 1152 KB contestant found the optimal answer: 29000419931 == 29000419931
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 1 ms 640 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 64 ms 44216 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 2 ms 1152 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 21 ms 8864 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 26 ms 11256 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 61 ms 41300 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 12 ms 6144 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 8 ms 4736 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 18 ms 9088 KB contestant found the optimal answer: 490686959791 == 490686959791
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1152 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 3 ms 1152 KB contestant found the optimal answer: 140412195 == 140412195
3 Runtime error 209 ms 131072 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 25 ms 6904 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 27 ms 8192 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Runtime error 255 ms 131076 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 257 ms 66300 KB contestant found the optimal answer: 19795776960 == 19795776960
2 Correct 268 ms 67852 KB contestant found the optimal answer: 19874432173 == 19874432173
3 Runtime error 227 ms 131076 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -