이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <set>
#include <deque>
using namespace std;
/*
Add a[i]*a[j] to the score if i and j are in different parts.
dp[i][p] = maximum score if 1..i is split into p parts
dp[i][p] = max{dp[j][p-1] + (a_sum[i] - a_sum[j])*(a_sum[j]) | j=1..i-1}
dp[i][p] = max{(-a_sum[j])*a_sum[i] + (dp[j][p-1] + a_sum[j]*a_sum[j]) | j=1..i-1}
*/
long long INF = 1'000'000'000'000'000'000;
int main()
{
long long n, k;
cin >> n >> k;
long long a[n+1], a_sum[n+1];
a_sum[0] = 0;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
a_sum[i] = a_sum[i-1] + a[i];
}
long long dp[n+1];
int cut_count_lo[n+1];
int cut_count_hi[n+1];
long long lo = 0, hi = INF/k;
long long ct;
dp[0] = cut_count_lo[0] = cut_count_hi[0] = 0;
dp[1] = cut_count_lo[1] = cut_count_hi[1] = 0;
while(1)
{
ct = (lo+hi)/2;
for(int i = 2; i <= n; i++)
{
dp[i] = 0;
cut_count_lo[i] = cut_count_hi[i] = 0;
for(int j = 1; j < i; j++)
{
long long new_dp = dp[j] + (a_sum[i] - a_sum[j])*a_sum[j] - ct;
// cerr << "i = " << i << ", j = " << j << " => new_dp = " << new_dp << '\n';
if(new_dp > dp[i])
{
dp[i] = new_dp;
cut_count_lo[i] = cut_count_lo[j] + 1;
cut_count_hi[i] = cut_count_hi[j] + 1;
}
else if(new_dp == dp[i])
{
cut_count_lo[i] = min(cut_count_lo[i], cut_count_lo[j] + 1);
cut_count_hi[i] = max(cut_count_hi[i], cut_count_hi[j] + 1);
}
}
}
// cerr << ct << ": " << cut_count_lo[n] << ' ' << cut_count_hi[n] << '\n';
if(lo == hi)
{
break;
}
else
{
if(cut_count_lo[n] > k) lo = ct+1;
else if(cut_count_lo[n] <= k && k <= cut_count_hi[n]) break;
else if(cut_count_hi[n] < k) hi = ct-1;
}
}
// for(int i = 1; i <= n; i++) cerr << i << ' ' << dp[i] << ' ' << cut_count_lo[i] << ' ' << cut_count_hi[i] << '\n';
cout << dp[n] + k*ct << '\n';
vector<int> res;
int X = n, K = k;
while(K >= 1)
{
for(int i = 1; i < X; i++)
{
long long new_dp = dp[i] + (a_sum[X] - a_sum[i])*a_sum[i] - ct;
if(cut_count_lo[i] <= K-1 && K-1 <= cut_count_hi[i] && new_dp == dp[X])
{
X = i;
K--;
res.push_back(X);
break;
}
}
}
for(int r:res) cout << r << ' ';
cout << '\n';
}
# | 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... |