• [HAOI2008]木棍分割


    Description

    BZOJ1044

    Luogu2511

    Solution

    第一问二分答案,第二问就是一个简单的DP,前缀和优化一下就行。

    Code

    const int N = 5e4 + 10;
    const int ha = 1e4 + 7;
    
    int a[N], f[N], s[N], sf[N], nxt[N];
    int n, m;
    
    bool check(int x) {
        int l = 1, r = 0, ans = 0;
        while (r <= n) {
            while (r <= n && s[r] - s[l - 1] <= x) r++;
            // if (x == 1) printf("%d %d
    ", l, r);
            ans++;
            if (r > n) break;
            if (l == r) return false;
            l = r;
        }
        return ans <= m;
    }
    int solve(int x) {
        f[1] = 1;
        for (int i = 1; i <= n; ++i) sf[i] = 1;
        int ans = 0;
        int k = n - 1;
        for (int j = n; j > 1; --j) {
            while (k > 0 && s[j] - s[k] <= x) k--;
            nxt[j] = k;
        }
        for (int i = 1; i <= m; ++i) {
            for (int j = n; j > 1; --j) {
                f[j] = 0;
                f[j] = (sf[j - 1] - sf[nxt[j]] + ha);
                if (f[j] >= ha) f[j] -= ha;
            }
            f[1] = sf[1] = 0;
            for (int j = 2; j <= n; ++j) {
                sf[j] = (sf[j - 1] + f[j]);
                if (sf[j] >= ha) sf[j] -= ha;
            }
            (ans += f[n]) %= ha;
        }
        return ans;
    }
    
    int main() {
        scanf("%d%d", &n, &m);
        m++;
        n++;
        for (int i = 2; i <= n; ++i) {
            scanf("%d", &a[i]);
            s[i] = s[i - 1] + a[i];
        }
        int l = 0, r = s[n], ans = -1;
        while (l <= r) {
            int mid = (l + r) >> 1;
            if (check(mid)) {
                ans = mid;
                r = mid - 1;
            } else {
                l = mid + 1;
            }
        }
        printf("%d %d
    ", ans, solve(ans));
        return 0;
    }
    
  • 相关阅读:
    原创 动态卷积
    BZOJ1565 植物大战僵尸
    BZOJ1143 [CTSC2008] 祭祀river
    BZOJ3438 小M的作物
    BZOJ3144 [HNOI2013]切糕
    BZOJ2039 [2009国家集训队]employ人员雇佣
    BZOJ1066[SCOI2007]蜥蜴
    BZOJ3874 codevs3361 宅男计划
    Codeforces Round #343 (Div. 2)
    [转]后缀自动机(SAM)
  • 原文地址:https://www.cnblogs.com/wyxwyx/p/bzoj1044.html
Copyright © 2020-2023  润新知