이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MXN = 100001, MXK = 202;
int N, K, s[2], e[2], stk[MXN][2], back[MXN][MXK];
ll sum[MXN], v[MXN], dp[MXN][2];
double cross(int x, int y, int z) {
return 1.0 * ((v[y] + dp[y][z]) - (v[x] + dp[x][z])) / (sum[x] - sum[y]);
}
void insert(int x, int z) {
int zz = z % 2;
stk[e[zz]][zz] = x;
while (e[zz] > 1 && cross(stk[e[zz] - 2][zz], stk[e[zz] - 1][zz], zz) >
cross(stk[e[zz] - 1][zz], stk[e[zz]][zz], zz))
stk[e[zz] - 1][zz] = stk[e[zz]][zz], e[zz]--;
e[zz]++;
}
void query(int x, int z) {
z--;
int zz = z % 2;
while (s[zz] + 1 < e[zz] && cross(stk[s[zz]][zz], stk[s[zz] + 1][zz], zz) <= sum[x]) s[zz]++;
int y = stk[s[zz]][zz];
dp[x][!zz] = (sum[N] - sum[x]) * (sum[x] - sum[y]) + dp[y][zz];
back[x][z + 1] = y;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> K; K++;
for (int i = 1; i <= N; ++i) {
ll n; cin >> n;
sum[i] = sum[i - 1] + n;
}
insert(0, 0);
for (int k = 1; k <= K; ++k) {
s[k % 2] = e[k % 2] = 0;
for (int i = 1; i <= N; ++i) {
//dp[i][k] = max(dp[i][k], dp[j][k - 1] + (sum[N] - sum[i]) * (sum[i] - sum[j]));
if (sum[i] == sum[i - 1]) continue;
query(i, k);
v[i] = -sum[N] * sum[i];
insert(i, k);
}
}
cout << dp[N][K % 2] << '\n';
int cur = back[N][K], cnt = K - 1;
stack<int> st;
while (cnt) {
st.push(cur);
cur = back[cur][cnt];
cnt--;
}
while (!st.empty()) {
cout << st.top() << ' ';
st.pop();
}
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... |