이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<cstdio>
#include<algorithm>
#include<deque>
using namespace std;
typedef long long ll;
typedef pair<ll,int> pp;
ll d[2][100010],s[100010],N,K;
int r[201][100001];
void f(int x,int y){
if(x==1)return;
f(x-1,r[x][y]);
printf("%d ",r[x][y]);
}
struct convex{
deque<ll> a,b;
deque<int> c;
void init(){a.clear();b.clear();c.clear();}
void push(ll n,ll m,int v){
while(a.size()>1&&(b[b.size()-2]-b.back())*(n-a.back())>=(b.back()-m)*(a.back()-a[a.size()-2])){
a.pop_back();
b.pop_back();
c.pop_back();
}
a.push_back(n);
b.push_back(m);
c.push_back(v);
}
pp query(ll x){
while(a.size()>1&&(a[1]-a[0])*x>(b[0]-b[1])){
a.pop_front();
b.pop_front();
c.pop_front();
}
return pp(a[0]*x+b[0],c[0]);
}
};
int main(){
scanf("%lld%lld",&N,&K);
if(K==0)K++;
for(int i=1;i<=N;i++){
scanf("%lld",&s[i]);
s[i]+=s[i-1];
}
convex* pre=new convex;
convex* cur=new convex;
pp ret;
for(int g=1;g<=K+1;g++){
pre->push(0,0,0);
for(int i=g;i<=N;i++){
ret=pre->query(s[i]);
d[g%2][i]=ret.first+(s[N]-s[i])*s[i];
r[g][i]=ret.second;
cur->push(s[i],d[g%2][i]-s[i]*s[N],i);
}
pre->init();
swap(cur,pre);
}
printf("%lld\n",d[(K+1)&1][N]);
f(K+1,N);
delete cur,pre;
}
# | 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... |