이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/* Murad Eynizade */
#include <bits/stdc++.h>
#define int long long
#define FAST_READ ios_base::sync_with_stdio(0);cin.tie(0);
#define F first
#define S second
//#define endl '\n'
using namespace std;
const int maxx = 100005;
int n , k , idx;
int dp[maxx][201] , pre[maxx] , arr[maxx] , sz[201] , in[201];
vector< pair< int , pair<int,int> > >v[201];
int inter(pair<int,int>a,pair<int,int>b) {
return (a.S - b.S) / (b.F - a.F);
}
pair<int,int> sec(int i) {
pair< int , pair<int,int> >t , ret;
t = v[i].back(); v[i].pop_back();
ret = v[i].back(); v[i].push_back(t);
return ret.S;
}
main() {
FAST_READ
cin>>n>>k;
for (int i = 1;i<=n;i++)cin>>arr[i] , pre[i] = pre[i - 1] + arr[i];
for (int j = 1;j<=n;j++) {
dp[j][1] = pre[j] * (pre[n] - pre[j]);
int i = 1;
pair<int,int>line = {pre[j] , -pre[j] * pre[n] + dp[j][1]};
while (sz[i] >= 2) {
pair<int,int>tp = v[i].back().S;
pair<int,int>tp2 = sec(i);
if (tp.F == line.F) {
v[i].pop_back();
sz[i]--;
continue;
}
int inter1 = inter(tp , tp2);
int inter2 = inter(tp2 , line);
if (inter2 < inter1)v[i].pop_back() , sz[i]--;
else {
break;
}
}
if (sz[i]) {
pair< int , pair<int,int> >cur = v[i].back();
v[i].pop_back();
sz[i]--;
if (cur.S.F != line.F) {
cur.F = inter(line , cur.S);
v[i].push_back(cur);
sz[i]++;
}
}
v[i].push_back({1000000001 , line});
sz[i]++;
}
for (int j = 2;j<=k;j++) {
for (int i = j;i<=n;i++) {
while (in[j - 1] < sz[j - 1] && v[j - 1][in[j - 1]].F < pre[i])in[j - 1]++;
pair< int , pair<int,int> >cur = v[j - 1][in[j - 1]];
dp[i][j] = cur.S.F * pre[i] + cur.S.S + pre[i] * pre[n] - pre[i] * pre[i];
int ii = j;
pair<int,int>line = {pre[i] , -pre[i] * pre[n] + dp[i][j]};
while (sz[ii] >= 2) {
pair<int,int>tp = v[ii].back().S;
pair<int,int>tp2 = sec(ii);
if (tp.F == line.F) {
v[ii].pop_back();
sz[ii]--;
continue;
}
int inter1 = inter(tp , tp2);
int inter2 = inter(tp2 , line);
if (inter2 < inter1)v[ii].pop_back() , sz[ii]--;
else {
break;
}
}
if (sz[ii]) {
pair< int , pair<int,int> >cur = v[ii].back();
v[ii].pop_back();
sz[ii]--;
if (cur.S.F != line.F) {
cur.F = inter(line , cur.S);
v[ii].push_back(cur);
sz[ii]++;
}
}
v[ii].push_back({1000000001 , line});
sz[ii]++;
}
}
idx = -1;
for (int i = 1;i<=n;i++) {
if (idx == -1)idx = i;
if (dp[i][k] > dp[idx][k])idx = i;
}
cout<<dp[idx][k]<<endl;
vector<int>res;
res.push_back(idx);
k--;
while (k) {
for (int ii = idx;ii;ii--) {
if (dp[ii - 1][k] + (pre[idx] - pre[ii - 1]) * (pre[n] - pre[idx]) == dp[idx][k + 1]) {
idx = ii - 1;
break;
}
}
res.push_back(idx);
k--;
}
reverse(res.begin(),res.end());
for (int i : res)cout<<i<<" ";
cout<<endl;
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp:31:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main() {
^
# | 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... |