답안 #390053

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
390053 2021-04-15T06:49:45 Z vonatlus 수열 (APIO14_sequence) C++17
100 / 100
641 ms 86932 KB
/// adil sultanov | vonat1us 

#pragma GCC optimize("O3")
//#pragma comment(linker, "/STACK:36777216")

#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back
#define sz(x) (int) x.size()
#define all(z) (z).begin(), (z).end()
 
using namespace std;

using ll = long long;
using pii = pair<int, int>;                                   

const int MOD = 1e9 + 7; 
const int INF = 1e9 + 1e2;
  
void fin() {
#ifdef AM
    freopen(".in", "r", stdin);
#endif        
}                   

const bool flag = 0;

const int N = 1e5+10;
const int K = 210;

int p[N];

ll dp[N][2];

int pr[N][K];

void f(int r, int k) {
    if (k == 1) return;
    int cut = pr[r][k];
    cout << cut+1 << " ";
    f(cut, k-1);                 
}

struct Line {
    ll k, b, id;
    Line() {}
    
    Line(int k, ll b, int id): k(k), b(b), id(id) {}
    
    ll get(int x) {
        return x*1ll*k + b;
    }

    ll cross(Line l) {
        if (k == l.k) return -INF;
        return (b-l.b)/(l.k-k);
    }
} v[N*K];

int l, r;

void add(Line L) {
    while (r > 1 && v[r-1].cross(L) <= v[r-2].cross(v[r-1])) {
        r--;
    }
    v[r++] = L;
}

int get(int x) {
    if (l > r) return 0;
    while (l+1 < r && v[l].get(x) <= v[l+1].get(x)) ++l;
    return l;
}
                    
int n;

void solve(int k) {
    l = 0, r = 0;
    int h = (k&1);      
    for (int i = 0; i < n; ++i) {
        if (i >= k-1) {
            int pos = get(p[i]);
            dp[i][h] = v[pos].get(p[i]);
            pr[i][k] = v[pos].id;
        }
        add(Line(p[i], dp[i][1-h]-p[i]*1ll*p[i], i));
    }
}

void ma1n() {
    int k;
    cin >> n >> k;
    k++;
    for (int i = 0, x; i < n; ++i) {
        cin >> x;
        p[i] = (i ? p[i-1] : 0)+x;
    }
    memset(pr, -1, sizeof(pr));
    for (int l = 2; l <= k; ++l) {
        solve(l);
    }
    cout << dp[n-1][k&1] << "\n";
    f(n-1, k);
} 

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr), fin();
    int ts = 1;
    if (flag) {
        cin >> ts;
    }
    while (ts--) {
        ma1n(); 
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 82508 KB contestant found the optimal answer: 108 == 108
2 Correct 39 ms 82404 KB contestant found the optimal answer: 999 == 999
3 Correct 38 ms 82468 KB contestant found the optimal answer: 0 == 0
4 Correct 40 ms 82480 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 38 ms 82500 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 38 ms 82532 KB contestant found the optimal answer: 1 == 1
7 Correct 40 ms 82556 KB contestant found the optimal answer: 1 == 1
8 Correct 38 ms 82432 KB contestant found the optimal answer: 1 == 1
9 Correct 38 ms 82480 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 36 ms 82432 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 38 ms 82496 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 37 ms 82424 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 37 ms 82508 KB contestant found the optimal answer: 140072 == 140072
14 Correct 39 ms 82452 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 38 ms 82500 KB contestant found the optimal answer: 805 == 805
16 Correct 38 ms 82512 KB contestant found the optimal answer: 900189994 == 900189994
17 Correct 38 ms 82472 KB contestant found the optimal answer: 999919994 == 999919994
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 82460 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 37 ms 82416 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 39 ms 82500 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 38 ms 82432 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 43 ms 82468 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 38 ms 82508 KB contestant found the optimal answer: 933702 == 933702
7 Correct 39 ms 82644 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 38 ms 82500 KB contestant found the optimal answer: 687136 == 687136
9 Correct 38 ms 82432 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 38 ms 82512 KB contestant found the optimal answer: 29000419931 == 29000419931
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 82508 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 38 ms 82488 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 39 ms 82468 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 39 ms 82508 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 39 ms 82464 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 39 ms 82476 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 45 ms 82496 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 39 ms 82436 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 38 ms 82504 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 38 ms 82416 KB contestant found the optimal answer: 490686959791 == 490686959791
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 82456 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 37 ms 82460 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 45 ms 82508 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 37 ms 82524 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 48 ms 82480 KB contestant found the optimal answer: 679388326 == 679388326
6 Correct 42 ms 82552 KB contestant found the optimal answer: 4699030287 == 4699030287
7 Correct 43 ms 82532 KB contestant found the optimal answer: 12418819758185 == 12418819758185
8 Correct 41 ms 82440 KB contestant found the optimal answer: 31093317350 == 31093317350
9 Correct 38 ms 82540 KB contestant found the optimal answer: 12194625429236 == 12194625429236
10 Correct 40 ms 82500 KB contestant found the optimal answer: 12345131038664 == 12345131038664
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 82828 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 43 ms 82872 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Correct 88 ms 82948 KB contestant found the optimal answer: 4973126687469639 == 4973126687469639
4 Correct 41 ms 82912 KB contestant found the optimal answer: 3748491676694116 == 3748491676694116
5 Correct 69 ms 82832 KB contestant found the optimal answer: 1085432199 == 1085432199
6 Correct 79 ms 82884 KB contestant found the optimal answer: 514790755404 == 514790755404
7 Correct 77 ms 82948 KB contestant found the optimal answer: 1256105310476641 == 1256105310476641
8 Correct 68 ms 82944 KB contestant found the optimal answer: 3099592898816 == 3099592898816
9 Correct 75 ms 82916 KB contestant found the optimal answer: 1241131419367412 == 1241131419367412
10 Correct 86 ms 82836 KB contestant found the optimal answer: 1243084101967798 == 1243084101967798
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 86320 KB contestant found the optimal answer: 19795776960 == 19795776960
2 Correct 54 ms 86340 KB contestant found the optimal answer: 19874432173 == 19874432173
3 Correct 623 ms 86812 KB contestant found the optimal answer: 497313449256899208 == 497313449256899208
4 Correct 56 ms 86792 KB contestant found the optimal answer: 374850090734572421 == 374850090734572421
5 Correct 641 ms 86348 KB contestant found the optimal answer: 36183271951 == 36183271951
6 Correct 503 ms 86340 KB contestant found the optimal answer: 51629847150471 == 51629847150471
7 Correct 510 ms 86820 KB contestant found the optimal answer: 124074747024496432 == 124074747024496432
8 Correct 412 ms 86880 KB contestant found the optimal answer: 309959349080800 == 309959349080800
9 Correct 487 ms 86580 KB contestant found the optimal answer: 124113525649823701 == 124113525649823701
10 Correct 582 ms 86932 KB contestant found the optimal answer: 124309619349406845 == 124309619349406845