제출 #33687

#제출 시각아이디문제언어결과실행 시간메모리
33687ztrong수열 (APIO14_sequence)C++14
0 / 100
0 ms1840 KiB
#include <bits/stdc++.h>
using namespace std;

#define llint long long

void openFile() {
    ios_base :: sync_with_stdio(false);
    cin.tie(NULL);
//    freopen("test.inp", "r", stdin);
//    freopen("test.out", "w", stdout);
}

const int maxN = 1e5 + 5;
const int maxM = 2e2 + 5;
const llint INF = 1e9 + 7;

int N, K;
llint a[maxN];
llint f[maxN][maxM];
struct line {
    llint a, b;
    int id;
    line() {}
    line(llint a, llint b, int id): a(a), b(b), id(id) {}
} l[maxN];
int nLines = 0;
int last = 0;

void enter() {
    cin >> N >> K;
    for (int i = 1; i <= N; ++i) {
        cin >> a[i];
        a[i] += a[i - 1];
    }
}

void clear() {
    nLines = 0;
    last = 1;
}

void update(int id, llint a, llint b) {
    while (nLines >= 2) {
        line lef = l[nLines - 1];
        line rig = l[nLines];

        if ((b - lef.b) * (lef.a - rig.a) < (lef.a - a) * (rig.b - lef.b)) {
            --nLines;
        }
        else {
            break;
        }
    }
    l[++nLines] = line(a, b, id);
}

inline llint get(int id, llint x) {
    return l[id].a * x + l[id].b;
}

int query(llint x) {
    while (last < nLines && get(last, x) < get(last + 1, x)) ++last;
    return l[last].id;
}

int t[maxN][maxM];

void solve() {
    for (int k = 1; k <= K; ++k) {
        clear();
        for (int i = 1; i <= N; ++i) {
            t[i][k] = query(a[i]);
            f[i][k] = get(t[i][k], a[i]);

            update(i, a[i], f[i][k - 1] - a[i] * a[i]);
        }
    }

    cout << f[N][K] << endl;
    int trace = t[N][K];
    vector<int> res;
    res.clear();
    while (trace) {
        res.push_back(trace);
        trace = t[trace][--K];
    }
    reverse(res.begin(), res.end());
    for (int x : res) {
        cout << x << " ";
    }
}

int main() {
    openFile();
    enter();
    solve();
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...