이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pf push_front
#define fi first
#define se second
const ll mod = 1e9+7, mxn = 1e5+7;
ll n, k, dp[mxn][207], trace[mxn][207], a[mxn], sum = 0, all_pair = 0, ans = 0, mo_l = 1, mo_r = 0;
void add(ll i)
{
ans += sum*a[i];
sum += a[i];
}
void rmv(ll i)
{
ans -= a[i]*(sum-a[i]);
sum -= a[i];
}
ll get(ll l, ll r)
{
while (mo_l < l) rmv(mo_l++);
while (mo_l > l) add(--mo_l);
while (mo_r < r) add(++mo_r);
while (mo_r > r) rmv(mo_r--);
return ans;
}
vector<vector<ll>> opt;
void dnc(ll j, ll l, ll r, ll opt_l, ll opt_r)
{
ll m = (r+l)>>1;
pair<ll,ll> res = {1e18, opt_l};
for (ll i = opt_l; i <= min(m,opt_r); i++) res = min(res,{dp[i-1][j-1]+get(i,m),i});
dp[m][j] = res.fi; trace[m][j] = res.se;
if (l <= m-1) dnc(j,l,m-1,opt_l,res.se);
if (m+1 <= r) dnc(j,m+1,r,res.se,opt_r);
}
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen("test.inp","r",stdin); freopen("test.out","w",stdout); freopen("test.err","w",stderr);
cin >> n >> k; ll sum = 0; k++;
for (ll i = 1; i <= n; i++)
{
cin >> a[i];
all_pair += sum*a[i];
sum += a[i];
}
for (ll i = 1; i <= n; i++) dp[i][1] = get(1,i);
for (ll i = 2; i <= k; i++) dnc(i,1,n,1,n);
cout << all_pair-dp[n][k] << '\n';
vector<ll> v;
while (k > 1)
{
v.pb(trace[n][k]-1);
n = trace[n][k]-1; k--;
}
reverse(v.begin(),v.end());
for (ll i : v) cout << i << ' ';
}
# | 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... |