Submission #1095210

#TimeUsernameProblemLanguageResultExecution timeMemory
1095210LonlyR수열 (APIO14_sequence)C++17
28 / 100
131 ms131072 KiB
#include<bits/stdc++.h>
#define int long long
#define ld long double
#define sz(u) (int)u.size()
using namespace std;
const int maxn = 1e5 + 5;
int n, k;
int a[maxn], dp[maxn][202], pf[maxn], pre[maxn][202];

struct iii{
    int a, b;
    ld x;
    int p;
};

inline ld ori(iii a, iii b)
{
    int u = a.a, v = a.b;
    int x = b.a, y = b.b;
    if (u == x) return (ld)1e18;
    return (ld)(y - v) / (ld)(u - x);
}

void trace(int n, int k)
{
    if (pre[n][k] == 0) return;
    trace(pre[n][k], k - 1);
    cout << pre[n][k] << " ";
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
//    freopen("test.inp", "r", stdin);
//    freopen("test.out", "w", stdout);
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
        cin >> a[i], pf[i] = pf[i - 1] + a[i];
    /// dp[i][j] = dp[k - 1][j - 1] + (pf[i] - pf[k - 1]) * (pf[n] - pf[i])
    /// dp[i][j] = pf[k - 1] * pf[i] + dp[k - 1][j - 1] + pf[n] * pf[i] - pf[i] ^ 2 - pf[k - 1] * pf[n]
    vector<iii> b[202];
    vector<int> pt(202, -1);
    b[0].push_back({0, 0, -1e18, 0});
    k++;
    for (int i = 1; i <= n; i++) for (int j = 0; j < k; j++)
    {
        pt[j] = min(pt[j], sz(b[j]) - 2);
        while (pt[j] + 1 < sz(b[j]) && (ld)pf[i] >= b[j][pt[j] + 1].x) pt[j]++;
        dp[i][j + 1] = b[j][pt[j]].a * pf[i] + b[j][pt[j]].b - pf[i] * pf[i] + pf[n] * pf[i];
        pre[i][j + 1] = b[j][pt[j]].p;
        iii now;
        now.a = pf[i];
        now.b = dp[i][j + 1] - pf[i] * pf[n];
        now.p = i;
        now.x = -1e18;
        while (sz(b[j + 1]) >= 2 && ori(b[j + 1][sz(b[j + 1]) - 2], now) <= ori(b[j + 1][sz(b[j + 1]) - 2], b[j + 1][sz(b[j + 1]) - 1]))
            b[j + 1].pop_back();
        if (b[j + 1].size()) now.x = ori(now, b[j + 1].back());
        b[j + 1].emplace_back(now);
    }
    cout << dp[n][k] << "\n";
    trace(n, k);
}

#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...