이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define pii pair<int,int>
#define mpr make_pair
typedef long long ll;
const int maxn = 1e4+10;
const int mod = 1e9+7;
const ll inf = 1e18+10;
int n, K;
int dp[maxn][210], up[maxn][210];
int ps[maxn], a[maxn];
void upd(int i, int k, int j)
{
if(j < 0 || j >= i) return;
int sum = ps[i]-ps[j];
int val = dp[j][k-1] + sum*(sum-1)/2;
if(val < dp[i][k])
{
dp[i][k] = val;
up[i][k] = j;
}
}
signed main()
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>> n >> K; K++;
for(int i = 1; i <= n; i++)
{
cin>> a[i];
ps[i] = ps[i-1] + a[i];
}
dp[0][0] = 0;
for(int k = 1; k <= K; k++)
{
for(int i = 1; i <= n; i++)
{
dp[i][k] = inf;
int hi = i, lo = 0;
while(hi-lo > 1)
{
int tm = (hi + lo) >> 1;
if(ps[i] - ps[tm] >= ps[i]/k) lo = tm;
else hi = tm;
}
/// ps[i] - ps[lo] >= ps[i] / k
for(int j = 0; j < i; j++) upd(i,k,j);
/* upd(i,k,lo-2);
upd(i,k,lo-1);
upd(i,k,lo);
upd(i,k,lo+1);
upd(i,k,lo+2);
*/
}
}
ll ans = ps[n] * (ps[n] - 1) / 2;
ans -= dp[n][K] * (dp[n][K]-1) / 2;
int i = n, k = K;
vector<int> out;
while(k)
{
out.push_back(up[i][k]);
i = up[i][k];
k--;
}
cout<< ans <<"\n";
for(auto x : out) cout<< x <<" ";
}
/*
7 3
4 1 3 4 0 2 3
*/
# | 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... |