이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define X first
#define Y second
struct line
{
ll m,c;
};
ll n,m,dp[100005][2],a[100005],qs[100005],u,g;
ll sum[100005],pos,cut;
int back[100005][205];
line t,now,pre;
stack<ll> stk;
vector<pair<line,ll> > v;
ll cal(line node,ll ptr)
{
return node.m*sum[ptr]+node.c;
}
ll intersect(line l1,line l2,line now)
{
if(now.c-l2.c>=1e18&&l1.m-now.m>=1e18)return 1;
if(now.c-l1.c>=1e18&&l2.m-now.m>=1e18)return 0;
return (now.c-l1.c)*(l2.m-now.m)-(now.c-l2.c)*(l1.m-now.m)<=0;
}
int main()
{
scanf("%lld%lld",&n,&m);
for(int i=1;i<=n;i++)scanf("%lld",&a[i]),sum[i]=sum[i-1]+a[i],qs[i]=qs[i-1]+a[i]*a[i];
for(int i=0;i<=n;i++)for(int j=0;j<=1;j++)dp[i][j]=2e18;
dp[0][0]=0;
memset(back,-1,sizeof back);
for(int i=1;i<=m+1;i++)
{
for(int j=0;j<=n;j++)dp[j][i%2]=2e18;
for(int j=0;j<=n;j++)
{
if(pos>=v.size())pos=v.size()-1;
while(pos+1<v.size()&&cal(v[pos+1].X,j)<=cal(v[pos].X,j))pos++;
if(v.size()>0)
{
dp[j][i%2]=cal(v[pos].X,j)+sum[j]*sum[j]-qs[j];
back[j][i]=v[pos].Y;
}
t={-2*sum[j],dp[j][1-(i%2)]+(sum[j]*sum[j]+qs[j])};
while(v.size()>=1)
{
now=v[v.size()-1].X;
if(now.m==t.m&&t.c<=now.c)
{
v.pop_back();
}else
{
break;
}
}
while(v.size()>=2)
{
now=v[v.size()-1].X;
pre=v[v.size()-2].X;
cut=intersect(now,pre,t);
if(cut)
{
v.pop_back();
}else
{
break;
}
}
v.pb({t,j});
//printf("%d %d %lld\n",i,j,dp[j][i%2]);
}
while(!v.empty())v.pop_back();
}
u=n;
g=m+1;
while(1)
{
u=back[u][g];
--g;
if(u==0)break;
stk.push(u);
}
printf("%lld\n",(sum[n]*sum[n]-qs[n]-dp[n][(m+1)%2])/2);
while(!stk.empty())
{
printf("%lld ",stk.top());
stk.pop();
}
printf("\n");
}
/*
7 3
4 1 3 4 0 2 3
*/
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'int main()':
sequence.cpp:39:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<line, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | if(pos>=v.size())pos=v.size()-1;
| ~~~^~~~~~~~~~
sequence.cpp:40:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<line, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | while(pos+1<v.size()&&cal(v[pos+1].X,j)<=cal(v[pos].X,j))pos++;
| ~~~~~^~~~~~~~~
sequence.cpp:29:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | scanf("%lld%lld",&n,&m);
| ~~~~~^~~~~~~~~~~~~~~~~~
sequence.cpp:30:31: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | for(int i=1;i<=n;i++)scanf("%lld",&a[i]),sum[i]=sum[i-1]+a[i],qs[i]=qs[i-1]+a[i]*a[i];
| ~~~~~^~~~~~~~~~~~~~
# | 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... |