이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ld = long double;
using ll = long long;
#define int long long
#define pb push_back
#define F first
#define S second
ll dp[100010];
int red[205][100010];
struct line{
int slope;
ll c;
ll eval(int x){
return slope*1ll*x+c;
}
ld interx(line & a){
return (ld)(a.c-c)/(ld)(slope-a.slope);
}
};
deque<pair<line, int>> cht;
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
int a[n];
ll pref[n+1];
pref[0]=0;
for(int i=0; i< n; i++){
cin >> a[i];
pref[i+1]=pref[i]+a[i];
}
for(int j=1; j<=k; j++){
cht.pb({{-pref[j-1], dp[j]}, j});
for(int i=j+1; i<=n; i++){
int x = pref[n]-pref[i-1];
while(cht.size()>=2){
pair<line, int> l = cht.back();
cht.pop_back();
if(l.F.eval(x)>cht.back().F.eval(x)){
cht.push_back(l);
break;
}
}
pair<line, int> np = {{-pref[i-1], dp[i]}, i};
dp[i]=cht.back().F.eval(x)+pref[i-1]*(pref[n]-pref[i-1]);
red[j][i]=cht.back().S;
while(cht.size()>=2){
pair<line, int> f = cht.front();
cht.pop_front();
if(f.F.slope==np.F.slope){
if(f.F.c<=np.F.c)continue;
else break;
}
ld inter1 = f.F.interx(cht.front().F);
ld inter2 = np.F.interx(cht.front().F);
if(inter2<inter1){
cht.push_front(f);
break;
}
}
cht.push_front(np);
}
while(cht.size())cht.pop_back();
}
ll ans=0;
int ind=0;
for(int i=1; i<=n; i++){
if(dp[i]>=ans){
ans=dp[i];
ind=i;
}
}
cout << ans << '\n';
vector<int> con;
con.pb(ind);
for(int i=k; i>1; i--){
con.pb(red[i][ind]);
ind=red[i][ind];
}
sort(con.begin(), con.end());
for(int i=0; i<k; i++){
cout << con[i]-1 << " ";
}
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... |