답안 #541711

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
541711 2022-03-24T08:44:39 Z Aldas25 수열 (APIO14_sequence) C++14
100 / 100
1282 ms 96044 KB
#include <bits/stdc++.h>

using namespace std;

#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(n) FOR(O, 1, (n))
#define f first
#define s second
#define pb push_back
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long double ld;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MAXN = 100100;
const ll INF = 1e16;

/*struct line {
    ll k, b;
    int id;

    line (ll _k, ll _b, int _id) : k (_k), b (_b), id (_id) {}

    ll operator() (ll x) {
        return k * x + b;
    }
};*/

/*ld intersection (line p, line q) {
    ld dy = ((ld)(q.b - p.b));
    ld dx = ((ld)(p.k - q.k));
    return dy / dx;
}*/

ld intersection (pair<pair<ll, ll>, int> p, pair<pair<ll, ll>, int> q) {
    ld dy = ((ld)(q.f.s - p.f.s));
    ld dx = ((ld)(p.f.f - q.f.f));
    return dy / dx;
}

//ll dp[210][MAXN];
int pr[210][MAXN];
int n, k;
ll a[MAXN], pref[MAXN];
deque<pair<pair<ll, ll>, int>> dq[210];
//int lstId[210];

void ins (int g, int id, ll dp) {
    ll k = pref[id];
    ll b = dp - pref[id] * pref[id];
   // line newL (k, b, id);
    pair<pair<ll, ll>, int> newL = {{k, b}, id};

    while (dq[g].size() > 0 && dq[g][dq[g].size()-1].f.f == newL.f.f) {
        dq[g].pop_back();
    }

    while (dq[g].size() > 1 &&
           intersection(dq[g][dq[g].size()-2], newL) < intersection(dq[g][dq[g].size()-2], dq[g][dq[g].size()-1])) {
        dq[g].pop_back();
    }

    dq[g].push_back(newL);
}

pair<int, ll> getMaxId (int g, ll x) {
    while (dq[g].size() > 1 && x >= intersection(dq[g][0], dq[g][1])) {
        dq[g].pop_front();
    }

    return {dq[g][0].s, dq[g][0].f.f * x + dq[g][0].f.s};
}

int main()
{
    FAST_IO;

    cin >> n >> k;
    FOR(i, 1, n) cin >> a[i];
    FOR(i, 1, n) pref[i] = pref[i-1] + a[i];

    ins(0, 0, 0);

    FOR(i, 1, n) for (int g = k+1; g > 0; g--) {
        if (i < g) continue;

        ll x = pref[i];
        auto p = getMaxId(g-1, x);
        int j = p.f;
        ll dp = p.s;
        pr[g][i] = j;

        //lstDp = dp;

        if (i == n && g == k+1)
            cout << dp << "\n";

        //cout << " i = " << i << " g = " << g << " dp = " << dp << endl;

        ins(g, i, dp);
    }

    //cout << lstDp << "\n";
    int g = k+1, i = n;
    while (g > 0) {
        i = pr[g][i];
        g--;
        if (g > 0) cout << i << " ";
    }
    cout << "\n";

    return 0;
}

/*

7 3
4 1 3 4 0 2 3

*/
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB contestant found the optimal answer: 108 == 108
2 Correct 1 ms 468 KB contestant found the optimal answer: 999 == 999
3 Correct 1 ms 468 KB contestant found the optimal answer: 0 == 0
4 Correct 1 ms 476 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 1 ms 468 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 1 ms 480 KB contestant found the optimal answer: 1 == 1
7 Correct 1 ms 468 KB contestant found the optimal answer: 1 == 1
8 Correct 1 ms 468 KB contestant found the optimal answer: 1 == 1
9 Correct 1 ms 468 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 1 ms 468 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 1 ms 476 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 0 ms 468 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 1 ms 468 KB contestant found the optimal answer: 140072 == 140072
14 Correct 1 ms 468 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 1 ms 476 KB contestant found the optimal answer: 805 == 805
16 Correct 1 ms 468 KB contestant found the optimal answer: 900189994 == 900189994
17 Correct 1 ms 468 KB contestant found the optimal answer: 999919994 == 999919994
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 468 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 1 ms 472 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 1 ms 724 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 1 ms 468 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 1 ms 480 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 1 ms 468 KB contestant found the optimal answer: 933702 == 933702
7 Correct 1 ms 600 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 1 ms 476 KB contestant found the optimal answer: 687136 == 687136
9 Correct 1 ms 468 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 1 ms 468 KB contestant found the optimal answer: 29000419931 == 29000419931
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 1 ms 468 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 2 ms 1500 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 1 ms 480 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 2 ms 1236 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 2 ms 1372 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 2 ms 1492 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 1 ms 724 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 1 ms 596 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 1 ms 724 KB contestant found the optimal answer: 490686959791 == 490686959791
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 1 ms 596 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 10 ms 2212 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 1 ms 596 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 10 ms 2280 KB contestant found the optimal answer: 679388326 == 679388326
6 Correct 9 ms 2008 KB contestant found the optimal answer: 4699030287 == 4699030287
7 Correct 11 ms 2260 KB contestant found the optimal answer: 12418819758185 == 12418819758185
8 Correct 10 ms 2540 KB contestant found the optimal answer: 31093317350 == 31093317350
9 Correct 3 ms 852 KB contestant found the optimal answer: 12194625429236 == 12194625429236
10 Correct 5 ms 1236 KB contestant found the optimal answer: 12345131038664 == 12345131038664
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1236 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 4 ms 1384 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Correct 112 ms 10828 KB contestant found the optimal answer: 4973126687469639 == 4973126687469639
4 Correct 4 ms 1364 KB contestant found the optimal answer: 3748491676694116 == 3748491676694116
5 Correct 74 ms 7180 KB contestant found the optimal answer: 1085432199 == 1085432199
6 Correct 89 ms 7628 KB contestant found the optimal answer: 514790755404 == 514790755404
7 Correct 99 ms 8484 KB contestant found the optimal answer: 1256105310476641 == 1256105310476641
8 Correct 79 ms 7548 KB contestant found the optimal answer: 3099592898816 == 3099592898816
9 Correct 84 ms 7960 KB contestant found the optimal answer: 1241131419367412 == 1241131419367412
10 Correct 104 ms 9748 KB contestant found the optimal answer: 1243084101967798 == 1243084101967798
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 7704 KB contestant found the optimal answer: 19795776960 == 19795776960
2 Correct 30 ms 8072 KB contestant found the optimal answer: 19874432173 == 19874432173
3 Correct 1256 ms 96044 KB contestant found the optimal answer: 497313449256899208 == 497313449256899208
4 Correct 35 ms 9132 KB contestant found the optimal answer: 374850090734572421 == 374850090734572421
5 Correct 1282 ms 94164 KB contestant found the optimal answer: 36183271951 == 36183271951
6 Correct 888 ms 65764 KB contestant found the optimal answer: 51629847150471 == 51629847150471
7 Correct 1076 ms 75040 KB contestant found the optimal answer: 124074747024496432 == 124074747024496432
8 Correct 821 ms 63052 KB contestant found the optimal answer: 309959349080800 == 309959349080800
9 Correct 887 ms 69912 KB contestant found the optimal answer: 124113525649823701 == 124113525649823701
10 Correct 1090 ms 86076 KB contestant found the optimal answer: 124309619349406845 == 124309619349406845