이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//çalışan kazanır//
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define pb push_back
#define S second
#define F first
#define all(x) x.begin(),x.end()
#define YOSIK() ios_base::sync_with_stdio(0),cin.tie(0)
int gcd (int a, int b) {if (b == 0){return a;}else {return gcd (b, a % b);}}
const int N = 2e5 + 1;
const int INF = 2e18 + 1;
const int MOD = 998244353ll;
const int MODD = 1070234587ll;
const double eps = 0.0001;
//const int p = 87;
int n, dp[100001][201], a[N], pref[N], suf[N];
int k;
void solution () {
cin >> n >> k;
for (int i = 1; i <= n; ++ i) cin >> a[i];
for (int i = 1; i <= n; ++ i) pref[i] = a[i] + pref[i - 1];
for (int i = n; i >= 1; -- i) suf[i] = suf[i + 1] + a[i];
for (int t = 1; t <= k; ++ t) {
for (int i = 1; i <= n; ++ i) dp[i][t] = -INF;
}
for (int i = 1; i < n; ++ i) dp[i][1] = pref[i] * suf[i + 1];
vector <pair <int, int>> ord;
for (int t = 2; t <= k; ++ t) {
ord.clear();
ord.pb ({INF, 0});
for (int i = 1; i < n; ++ i) {
int l = 0, r = ord.size() - 1, rs = 0;
while (l <= r) {
int mid = (l + r) >> 1;
if (ord[mid].F >= suf[i + 1]) l = mid + 1, rs = mid;
else r = mid - 1;
}
rs = ord[rs].S;
dp[i][t] = dp[rs][t - 1] + (pref[i] - pref[rs]) * suf[i + 1];
if (dp[i][t - 1] == -INF) continue;
int per = INF;
while (ord.size()) {
if (pref[ord.back().S] == pref[i]) {;
if (dp[i][t - 1] >= dp[ord.back().S][t - 1]) ord.pop_back();
else break;
continue;
}
per = (dp[i][t - 1] - dp[ord.back().S][t - 1]) / (-pref[ord.back().S] + pref[i]);
if (per > ord.back().F) ord.pop_back();
else break;
}
ord.pb ({per, i});
}
}
int res = 0, bg = 0;
for (int i = 1; i <= n; ++ i) {
res = max (res, dp[i][k]);
if (dp[i][k] == res) bg = i;
}
cout << res << '\n';
vector <int> ans;
ans.pb (bg);
for (int t = k - 1; t >= 1; -- t) {
for (int i = 1; i < bg; ++ i) {
if (dp[i][t] + (pref[bg] - pref[i]) * suf[bg + 1] == res) {
bg = i;
ans.pb (i);
res = dp[i][t];
break;
}
}
}
reverse (all (ans));
for (auto i: ans) cout << i << ' ';
cout << '\n';
return;
}
signed main() {
YOSIK();
// srand (time(0));
// freopen ("E.in", "r", stdin);
// freopen ("E.out", "w", stdout);
int TT = 1;// cin >> TT;
for (int i = 1; i <= TT; ++ i) {
// cout << "Case " << i << ": ";
solution ();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |