bzoj1044

二分+贪心+动态规划

第一问就是二分+贪心,和跳石头挺像的

第二问是dp,dp[i][j]表示第i次切割切到了第j段木棍,转移就是dp[i][j] = sigma(dp[i-1][k]), sum[j]-sum[k]<=ans,ans是最大长度,这里第j段木棍表示现在正在分割1-j这些木棍。很明显这样时间空间都不满足,空间滚动数组就行了,时间我们需要用类似单调队列优化一下,其实也是双指针。每次sum[j]-sum[q[l]]不满足就向前移动

初值是dp[0][i]=(sum[i] <= ans)

最终答案是sigma(dp[0->m][n])

#include<bits/stdc++.h>
using namespace std;
const int N = 50010, mod = 10007;
int n, m;
int l[N], dp[2][N], sum[N], q[N];
bool check(int mid)
{
    int tot = 0, ret = 0;
    for(int i = 1; i <= n; ++i) if(l[i] > mid) return false;
    for(int i = 1; i <= n; ++i)
    {
        if(tot + l[i] > mid) tot = l[i], ++ret;
        else tot += l[i];
    }
    return ret <= m;
}
int main()
{
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; ++i) scanf("%d", &l[i]), sum[i] = sum[i - 1] + l[i];
    int l = 0, r = sum[n] + 1, ans = 0;
    while(r - l > 1)
    {
        int mid = (l + r) >> 1;
        if(check(mid)) r = ans = mid;
        else l = mid;
    } 
    int pre = 0, ret = 0;
    printf("%d ", ans);
//  dp[pre][0] = 1;
    for(int i = 1; i <= n; ++i) dp[pre][i] = (sum[i] <= ans);
    for(int i = 1; i <= m; ++i)
    {
        pre ^= 1;
//      memset(dp[pre], 0, sizeof(dp[pre]));
        int l = 1, r = 0, tot = 0;
        for(int j = 1; j <= n; ++j)
        {
            while(sum[j] - sum[q[l]] > ans && l <= r) tot = ((tot - dp[pre ^ 1][q[l]]) % mod + mod) % mod, ++l;
            dp[pre][j] = tot;
            tot = ((tot + dp[pre ^ 1][j]) % mod + mod) % mod;           
            q[++r] = j;
        }
        ret = (ret + dp[pre][n]) % mod;
    }
    printf("%d\n", ret);
    return 0;
}
View Code

 

posted @ 2017-09-01 12:07  19992147  阅读(118)  评论(0编辑  收藏