이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;
int n, k;
pair<ll, int> dp[201][100001];
ll pref[100001]{0}, q[100001], l = 1, r = 1;
bool case1(int x, int y, int i, int indx) {
return (dp[indx][y].first - dp[indx][x].first >=
(pref[y] - pref[x]) * (pref[n] - pref[i]));
}
bool case2(int x, int y, int i, int indx) {
return ((dp[indx][y].first - dp[indx][x].first) * (pref[i] - pref[y]) <=
(dp[indx][i].first - dp[indx][y].first) * (pref[y] - pref[x]));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
FOR(i, 1, n + 1) {
int x;
cin >> x;
pref[i] = pref[i - 1] + x;
}
fill(dp[0], dp[0] + n + 1, make_pair(0, 0));
FOR(i, 1, k + 1) {
q[r++] = 0;
dp[i][0] = {0, 0};
FOR(j, 1, n + 1) {
while (r - l > 1 && case1(q[l], q[l + 1], j, i - 1)) l++;
ll x = q[l];
dp[i][j] = {dp[i - 1][x].first + (pref[j] - pref[x]) * (pref[n] - pref[j]), x};
while (r - l > 1 && case2(q[r - 2], q[r - 1], j, i - 1)) r--;
q[r++] = j;
}
l = r = 1;
}
ll mx = -1;
int indx = -1;
FOR(i, 1, n + 1) {
if (dp[k][i].first > mx) mx = dp[k][i].first, indx = i;
}
cout << mx << '\n';
FOR(i, 0, k) {
cout << indx << ' ';
indx = dp[k - i][indx].second;
}
cout << '\n';
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... |