题解:
第一问二分答案
第二问dp+单调队列
代码:
#include<bits/stdc++.h> const int M=10007,N=50005; typedef long long ll; using namespace std; int n,m,ans1,ans2,a[N],sum[N],f[2][N],q[N]; int jud(int x) { int tmp=0,sum=0; for (int i=1;i<=n;i++) { sum+=a[i]; if (sum>x){tmp++;sum=a[i];} if (tmp>m)return 0; if (a[i]>x)return 0; } return 1; } void solve1() { int l=1,r=sum[n]; while (l<=r) { int mid=(l+r)>>1; if (jud(mid)){ans1=mid;r=mid-1;} else l=mid+1; } } void solve2() { f[0][0]=1; int pre,cur,tot; for (int i=1;i<=m;i++) { pre=i&1;cur=pre^1; int l=1,r=1; q[1]=0;tot=f[cur][0]; for (int j=1;j<=n;j++) { while (l<=r&&sum[j]-sum[q[l]]>ans1)tot=(tot-f[cur][q[l++]]+M)%M; f[pre][j]=tot;q[++r]=j; tot=(tot+f[cur][j]+M)%M; } for (int j=n-1;j;j--) { if (sum[n]-sum[j]>ans1)break; ans2=(ans2+f[pre][j]+M)%M; } memset(f[cur],0,sizeof(f[cur])); } printf("%d ",ans2); } int main() { scanf("%d%d",&n,&m); for (int i=1;i<=n;i++)scanf("%d",&a[i]); for (int i=1;i<=n;i++)sum[i]=sum[i-1]+a[i]; solve1(); printf("%d ",ans1); solve2(); }