이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define int long long
#define forinc(a,b,c) for(int a=b,_c=c;a<=_c;++a)
#define fordec(a,b,c) for(int a=b,_c=c;a>=_c;--a)
#define forv(a,b) for(auto&a:b)
#define pb push_back
#define db double
#define pii pair<int,int>
#define fi first
#define se second
const int N=100010;
int f[N],n,k,a[N],s[N],p[N],g[N],tr[N][4],top;
db lef[N];
int st[N];
vector<int> kq;
db giao(int u,int v)
{
return 1.0*(g[v]-g[u])/(s[v]-s[u]);
}
void add(int i,int j)
{
if(top>=0&&s[i-1]==s[st[top]]) --top;
while(top>=1&&lef[top]<=giao(i-1,st[top])) --top;
st[++top]=i-1;lef[top]=giao(st[top],st[top-1]);
}
main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//freopen("sequence.inp","r",stdin);
cin>>n>>k;
forinc(i,1,n) cin>>a[i],s[i]=s[i-1]+a[i];
forinc(i,1,n) p[i]=s[n]-s[i];
forinc(j,1,k)
{
top=-1;
forinc(i,j,n-1)
{
add(i,j);
int l=1,r=top,o=0;
while(l<=r)
{
int mid=(l+r)/2;
if(lef[mid]>p[i]) o=mid,l=mid+1;
else r=mid-1;
}
f[i]=-p[i]*s[st[o]]+g[st[o]]+p[i]*s[i];
tr[i][j]=st[o];
}
forinc(i,j,n-1) g[i]=f[i];
}
int I=0,J=k;
forinc(i,k,n-1) if(f[i]>=f[I]) I=i;
cout<<f[I]<<"\n";
while(J)
{
kq.pb(I);
I=tr[I][J],J--;
}
reverse(kq.begin(),kq.end());
forv(x,kq) cout<<x<<" ";
}
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp:29: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... |