Submission #499622

# Submission time Handle Problem Language Result Execution time Memory
499622 2021-12-29T05:26:18 Z todaybrian Split the sequence (APIO14_sequence) C++14
100 / 100
941 ms 81604 KB
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define nl '\n'
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define flood(x) memset(x, 0x3f3f3f3f, sizeof x)
#define drain(x) memset(x, 0, sizeof x)
#define dcit cin.sync_with_stdio(0);cin.tie(0)
#define frp(x, y) freopen(x, "r", stdin);freopen(y, "w", stdout);
#define endl '\n'
typedef pair<int, int> pii;
typedef long long ll;

#define gc getchar_unlocked()
#define pc(x) putchar_unlocked(x)

template<typename T> void scani(T& x){x = 0; register bool _ = 0;register T c = gc;while((c<48||c>57)&&c!=45) c=gc;_=c== 45;c=_ ?gc:c;while (c<48||c>57)c=gc;for (;c<48||c>57;c=gc);for (;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printi(T n) {register bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);pc(10);}
string nextstring() { string r; register char c=gc;while(c==32||c==10) c = gc;while(!(c==32||c==10)) { r += c;c = gc; }return r; }
char nextgetchar(){ register char c = gc; while(c==32 || c==10) c = gc; return c;}

void prints(string k){ for (int i = 0; i < k.length(); i++)pc(k[i]); }
void scani() {}

template<typename First, typename ... Ints>
void scani(First &arg, Ints&... rest) { scani(arg);scani(rest...); }

const int MAXN = 100002;
int N, K, pos[202][MAXN], cur, pre, now = 1;
ll dp[2][MAXN], psa[MAXN], ans = -1;
deque<int> q;

inline double slope(int p, int q) {
    if (psa[p] == psa[q])return -1.0e8;
    return (double) (dp[pre][p] - psa[p] * psa[N] - dp[pre][q] + psa[q] * psa[N]) / (double) (psa[q] - psa[p]);
}

int main() {
    scani(N, K);
    for (int i = 1; i <= N; i++) {
        scani(psa[i]);
        psa[i] += psa[i - 1];
    }
    for (int k = 1; k <= K; k++) {
        memset(dp[now], -1, sizeof(dp[now]));
        q.clear();
        q.push_back(0);
        for (int i = 1; i <= N; i++) {
            while (q.size()>1 && slope(q[0], q[1]) <= psa[i])
                q.pop_front();

            int j = q[0];
            dp[now][i] = dp[pre][j] + (psa[i] - psa[j]) * (psa[N] - psa[i]);
            pos[k][i] = j;

            while (q.size() > 1 && slope(q[q.size()-2], q[q.size()-1]) >= slope(q[q.size()-1], i))
                q.pop_back();

            q.push_back(i);
//            for (int j = last; j < i; j++) {
//                ll temp = dp[pre][j]+(psa[i]-psa[j])*(psa[N]-psa[i]);
//                if(temp >=dp[now][i]) {
//                    dp[now][i] = temp; pos[k][i] = j; last = j;
//                }
//            }
        }
        pre ^= 1;
        now ^= 1;
    }
    for (int i = 1; i <= N; i++)
        if (dp[pre][i] > ans) {
            ans = dp[pre][i];
            cur = i;
        }

    printi(ans);
    for (int i = K; i >= 1; i--) {
        printf("%d ", cur);
        cur = pos[i][cur];
    }
    return 0;
}

Compilation message

sequence.cpp: In function 'void prints(std::string)':
sequence.cpp:24:42: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 | void prints(string k){ for (int i = 0; i < k.length(); i++)pc(k[i]); }
      |                                        ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1860 KB contestant found the optimal answer: 108 == 108
2 Correct 1 ms 1832 KB contestant found the optimal answer: 999 == 999
3 Correct 1 ms 972 KB contestant found the optimal answer: 0 == 0
4 Correct 1 ms 1868 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 1 ms 1840 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 1 ms 1060 KB contestant found the optimal answer: 1 == 1
7 Correct 1 ms 1068 KB contestant found the optimal answer: 1 == 1
8 Correct 1 ms 972 KB contestant found the optimal answer: 1 == 1
9 Correct 1 ms 1868 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 1 ms 1868 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 2 ms 1868 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 1 ms 1868 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 1 ms 1868 KB contestant found the optimal answer: 140072 == 140072
14 Correct 1 ms 1868 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 2 ms 1868 KB contestant found the optimal answer: 805 == 805
16 Correct 1 ms 1868 KB contestant found the optimal answer: 900189994 == 900189994
17 Correct 1 ms 1868 KB contestant found the optimal answer: 999919994 == 999919994
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1868 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 1 ms 1868 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 2 ms 2124 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 1 ms 1868 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 1 ms 1868 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 1 ms 1868 KB contestant found the optimal answer: 933702 == 933702
7 Correct 1 ms 1996 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 2 ms 1868 KB contestant found the optimal answer: 687136 == 687136
9 Correct 1 ms 1868 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 2 ms 1868 KB contestant found the optimal answer: 29000419931 == 29000419931
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1836 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 1 ms 1868 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 7 ms 2892 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 1 ms 1868 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 5 ms 2636 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 6 ms 2764 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 9 ms 2864 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 2 ms 2124 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 2 ms 1960 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 5 ms 2124 KB contestant found the optimal answer: 490686959791 == 490686959791
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1868 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 1 ms 1868 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 12 ms 3568 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 1 ms 1792 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 13 ms 3596 KB contestant found the optimal answer: 679388326 == 679388326
6 Correct 11 ms 3248 KB contestant found the optimal answer: 4699030287 == 4699030287
7 Correct 12 ms 3576 KB contestant found the optimal answer: 12418819758185 == 12418819758185
8 Correct 11 ms 3496 KB contestant found the optimal answer: 31093317350 == 31093317350
9 Correct 5 ms 2124 KB contestant found the optimal answer: 12194625429236 == 12194625429236
10 Correct 6 ms 2532 KB contestant found the optimal answer: 12345131038664 == 12345131038664
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1996 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 2 ms 2124 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Correct 76 ms 10672 KB contestant found the optimal answer: 4973126687469639 == 4973126687469639
4 Correct 2 ms 2080 KB contestant found the optimal answer: 3748491676694116 == 3748491676694116
5 Correct 60 ms 7232 KB contestant found the optimal answer: 1085432199 == 1085432199
6 Correct 60 ms 7992 KB contestant found the optimal answer: 514790755404 == 514790755404
7 Correct 71 ms 8520 KB contestant found the optimal answer: 1256105310476641 == 1256105310476641
8 Correct 49 ms 7224 KB contestant found the optimal answer: 3099592898816 == 3099592898816
9 Correct 69 ms 8104 KB contestant found the optimal answer: 1241131419367412 == 1241131419367412
10 Correct 101 ms 9768 KB contestant found the optimal answer: 1243084101967798 == 1243084101967798
# Verdict Execution time Memory Grader output
1 Correct 13 ms 4188 KB contestant found the optimal answer: 19795776960 == 19795776960
2 Correct 14 ms 4148 KB contestant found the optimal answer: 19874432173 == 19874432173
3 Correct 728 ms 81604 KB contestant found the optimal answer: 497313449256899208 == 497313449256899208
4 Correct 14 ms 4480 KB contestant found the optimal answer: 374850090734572421 == 374850090734572421
5 Correct 941 ms 81260 KB contestant found the optimal answer: 36183271951 == 36183271951
6 Correct 597 ms 57748 KB contestant found the optimal answer: 51629847150471 == 51629847150471
7 Correct 689 ms 62032 KB contestant found the optimal answer: 124074747024496432 == 124074747024496432
8 Correct 482 ms 50980 KB contestant found the optimal answer: 309959349080800 == 309959349080800
9 Correct 685 ms 57876 KB contestant found the optimal answer: 124113525649823701 == 124113525649823701
10 Correct 664 ms 73640 KB contestant found the optimal answer: 124309619349406845 == 124309619349406845