比赛 2024国庆练习3 评测结果 AAAAAAAAAAAAAAAAAAAAAAEEE
题目名称 划分 最终得分 88
用户昵称 darkMoon 运行时间 2.348 s
代码语言 C++ 内存使用 13.36 MiB
提交时间 2024-10-06 16:14:54
显示代码纯文本
#include<bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define mp make_pair
using namespace std;
auto IN = freopen("2019partition.in", "r", stdin);
auto OUT = freopen("2019partition.out", "w", stdout);
auto mread = [](){int x;scanf("%lld", &x);return x;};
const int N = 5e5 + 5;
int n = mread(), type = mread(), a[N], f[N][2], s[N];
signed main(){
    for(int i = 1; i <= n; i ++){
        cin >> a[i];
        s[i] = s[i - 1] + a[i];
    }
    memset(f, 0x3f, sizeof(f));
    f[0][0] = f[0][1] = 0;
    int p = 0;
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>> > q;
    for(int i = 1; i <= n; i ++){
        while(q.size() && q.top().fi <= s[i]){
            p = max(p, q.top().se + 1);
            q.pop();
        }
        f[i][0] = s[i] - s[p];
        f[i][1] = f[p][1] + f[i][0] * f[i][0];
        q.push(mp(f[i][0] + s[i], i - 1));
    }
    printf("%lld", f[n][1]);
    return 0;
}