1. 程式人生 > >[BZOJ1044][HAOI2008]木棍分割

[BZOJ1044][HAOI2008]木棍分割

led 成了 har else style eof scu scrip for

1044: [HAOI2008]木棍分割

Time Limit: 10 Sec Memory Limit: 162 MB Submit: 3998 Solved: 1537 [Submit][Status][Discuss]

Description

  有n根木棍, 第i根木棍的長度為Li,n根木棍依次連結了一起, 總共有n-1個連接處. 現在允許你最多砍斷m個連 接處, 砍完後n根木棍被分成了很多段,要求滿足總長度最大的一段長度最小, 並且輸出有多少種砍的方法使得總長 度最大的一段長度最小. 並將結果mod 10007。。。

Input

  輸入文件第一行有2個數n,m.接下來n行每行一個正整數Li,表示第i根木棍的長度.n<=50000,0<=m<=min(n-1,10 00),1<=Li<=1000.

Output

  輸出有2個數, 第一個數是總長度最大的一段的長度最小值, 第二個數是有多少種砍的方法使得滿足條件.

Sample Input

3 2
1
1
10

Sample Output

10 2

HINT

兩種砍的方法: (1)(1)(10)和(1 1)(10)

第一問二分答案判定即可

第二問DP

設$f[i][j]$表示前$i$段切$j$刀滿足條件的方案數

轉移顯然,然後類似滑動窗口可以做到$O(nm)$

#include <cstdio>
#include <cstring> 
inline int readint(){
    
int f = 1, n = 0; char ch = getchar(); while(ch < 0 || ch > 9){ if(ch == -) f = -1; ch = getchar(); } while(ch <= 9 && ch >= 0){ n = (n << 1) + (n << 3) + ch - 0; ch = getchar(); } return f * n; } const int
maxn = 50000 + 10; int n, m; int num[maxn]; inline bool Judge(int L){ int tot = 0, cnt = 0; for(int i = 1; i <= n; i++){ if(num[i] > L) return false; if(tot + num[i] > L){ cnt++; tot = num[i]; } else tot += num[i]; } return cnt <= m; } const int mod = 10007; int f[2][maxn] = {0}, sum[maxn]; int q[maxn], h, t; int main(){ n = readint(); m = readint(); sum[0] = 0; for(int i = 1; i <= n; i++) sum[i] = (num[i] = readint()) + sum[i - 1]; int l = 1, r = sum[n], mid, ans; while(l <= r){ mid = l + r >> 1; if(Judge(mid)){ ans = mid; r = mid - 1; } else l = mid + 1; } int ans2 = 0; f[0][0] = 1; for(int tot, now, i = 1; i <= m; i++){ now = i & 1; tot = 0; q[h = t = 1] = 0; tot = f[now ^ 1][0]; for(int j = 1; j <= n; j++){ while(h <= t && sum[j] - sum[q[h]] > ans){ tot = (tot - f[now ^ 1][q[h]] + mod) % mod; h++; } f[now][j] = tot; q[++t] = j; tot = (tot + f[now ^ 1][j]) % mod; } for(int j = n - 1; j; j--) if(sum[n] - sum[j] > ans) break; else ans2 = (ans2 + f[now][j]) % mod; memset(f[now ^ 1], 0, sizeof(f[now ^ 1])); } printf("%d %d\n", ans, ans2); return 0; }

[BZOJ1044][HAOI2008]木棍分割