1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<queue> #include<map> #include<stack> #include<cmath> #include<set> #include<bitset> #include<complex> #include<cstdlib> #include<assert.h> #define inc(i,l,r) for(int i=l;i<=r;i++) #define dec(i,l,r) for(int i=l;i>=r;i--) #define link(x) for(edge *j=h[x];j;j=j->next) #define mem(a) memset(a,0,sizeof(a)) #define ll long long #define mid (x+y>>1) #define eps 1e-8 #define succ(x) (1ll<<(x)) #define lowbit(x) (x&(-x)) #define sqr(x) ((x)*(x)) #define NM 100005 #define nm 10005 using namespace std; const double pi=acos(-1); const int inf=998244353; ll read(){ ll x=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); return f*x; }
int n,m; ll d[NM],ans,a[NM]; int p[NM],q[NM],qh,qt,mmax[NM],mmin[NM],c[NM]; vector<int>pre[NM];
double slope(int x,int y){ if(a[x]==a[y])return d[x]>d[y]?inf:-inf; return 1.0*(d[x]-d[y])/(a[y]-a[x])+a[x]+a[y]; }
bool check(ll t){ q[qh=qt=0]=0; inc(i,1,n){ while(qh<qt&&slope(q[qh],q[qh+1])<a[i])qh++; d[i]=d[q[qh]]+(a[i]-a[q[qh]])*a[q[qh]]-t; p[i]=p[q[qh]]+1; while(qh<qt&&slope(q[qt-1],q[qt])>slope(q[qt],i))qt--; q[++qt]=i; } return p[n]<=m+1; } void _check(ll _t){ q[qh=qt=0]=0; inc(i,1,n){ while(qh<qt&&slope(q[qh],q[qh+1])<a[i])qh++; d[i]=d[q[qh]]+(a[i]-a[q[qh]])*a[q[qh]]-_t; mmin[i]=n+1; for(int k=qh,t=q[qh];k<=qt&&d[i]+_t==d[t]+(a[i]-a[t])*a[t];t=q[++k]){ pre[i].push_back(t); mmax[i]=max(mmax[i],mmax[t]+1); mmin[i]=min(mmin[i],mmin[t]+1); } while(qh<qt&&slope(q[qt-1],q[qt])>slope(q[qt],i))qt--; q[++qt]=i; } printf("%lld\n",d[n]+ans*(m+1)); for(int x=n,t=m;t;){ for(auto&j:pre[x])if(mmin[j]<=t&&t<=mmax[j]){ x=c[t]=j; t--; break; } } }
int main(){ n=read();m=read(); inc(i,1,n)a[i]=read()+a[i-1]; for(ll x=0,y=a[n]*a[n];x<=y;) if(check(mid)){ ans=mid;y=mid-1; }else x=mid+1; _check(ans); inc(i,1,m)printf("%d%c",c[i]," \n"[i==m]); return 0; }
|