BZOJ 1044 木棍分割

Posted on 2017-01-09 13:31  ziliuziliu  阅读(88)  评论(0编辑  收藏

二分+dp.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 50050
#define mod 10007
using namespace std;
int n,m,l[maxn],len,sums=0,lb[maxn],dp[maxn][2],sum[maxn][2],mx=0,ans=0;
bool check(int len)
{
    int now=l[1],ret=0;
    for (int i=2;i<=n;i++)
    {
        if (now+l[i]>len)     {ret++;now=l[i];}
        else now+=l[i];
    }
    return ret<=m;
}
void binary_search()
{
    int l=mx,r=sums;
    while (l<=r)
    {
        int mid=(l+r)>>1;
        if (check(mid)) {len=mid;r=mid-1;}
        else l=mid+1;
    }
}
void get_bnd()
{
    int ret=0,now=0;
    for (int i=1;i<=n;i++)
    {
        ret-=l[i-1];
        while ((ret+l[now+1]<=len) && (now+1<=n))
        {
            now++;ret+=l[now];
            lb[now]=i;
        }
    }
}
void dps()
{
    for (int i=1;i<=n;i++) if (lb[i]==1) dp[i][0]=1;
    for (int i=1;i<=n;i++) sum[i][0]=sum[i-1][0]+dp[i][0];
    ans+=dp[n][0];
    for (int i=1;i<=m;i++)
    {
        for (int j=1;j<=n;j++)
        {
            if (j<=i) {dp[j][i&1]=sum[j][i&1]=0;continue;}
            if (lb[j]-1) dp[j][i&1]=(sum[j-1][(i&1)^1]-sum[lb[j]-2][(i&1)^1]+mod)%mod;
            else dp[j][i&1]=sum[j-1][(i&1)^1]%mod;
            sum[j][i&1]=(sum[j-1][i&1]+dp[j][i&1])%mod;
        }
        ans=(ans+dp[n][i&1])%mod;
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++) {scanf("%d",&l[i]);sums+=l[i];mx=max(mx,l[i]);}
    binary_search();
    get_bnd();
    dps();
    printf("%d %d\n",len,ans);
    return 0;
}