이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
int ar[100005];
struct line{
long long m,c;
int num;
line(int mm=0,int cc=0,int i=0){
m=mm;
c=cc;
num=i;
}
long long pnt(int x){
return m*x+c;
}
};
bool better(line a,line b,line c){
return (c.c-a.c)*(a.m-b.m)<=(b.c-a.c)*(a.m-c.m);
}
struct hull{
deque<line>dq;
void add(line x){
while(dq.size()>1&&better(dq[dq.size()-2],dq.back(),x)){
dq.pop_back();
}
dq.push_back(x);
}
pair<long long,int> fbest(long long x,int mxl){
while(dq.size()>1&&dq.front().num<mxl&&(dq.front().pnt(x)<=dq[1].pnt(x))){
dq.pop_front();
}
if(dq.empty()){
return {0,0};
}
return {dq.front().pnt(x),dq.front().num};
}
}dp[205];
long long sum[100005];
int path[205][100005];
int main(){
int n,k;
cin>>n>>k;
long long sm=0;
for(int i=1;i<=n;i++){
cin>>ar[i];
sm+=ar[i];
sum[i]=sm;
}
long long ans=0;
long long lnum;
//cerr<<"work"<<endl;
for(int i=1;i<=k;i++){
for(int j=i;j<n;j++){
//cerr<<"i:j "<<i<<" "<<j<<endl;
pair<long long,int>p=dp[i].fbest(sum[j],j);
//cerr<<"after fbest"<<endl;
long long x=p.first;
//cerr<<"x:"<<x<<endl;
int pn=p.second;
long long tans=x+sm*sum[j]-sum[j]*sum[j];
//cerr<<"ans:"<<tans<<endl;
long long nm=sum[j];
long long nc=tans-sm*sum[j];
dp[i+1].add(line(nm,nc,j));
//cerr<<"after add"<<endl;
path[i][j]=pn;
if(i==k){
if(tans>ans){
ans=tans;
lnum=j;
}
}
}
}
//cerr<<"out"<<endl;
cout<<ans<<"\n";
vector<int>v;
v.push_back(lnum);
for(int i=k;i>=2;i--){
v.push_back(lnum=path[i][lnum]);
}
for(int i=v.size()-1;i>=0;i--){
cout<<INT_MAX<<" ";
}
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'int main()':
sequence.cpp:77:16: warning: 'lnum' may be used uninitialized in this function [-Wmaybe-uninitialized]
77 | v.push_back(lnum);
| ~~~~~~~~~~~^~~~~~
# | 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... |