# 【BZOJ】1044: [HAOI2008]木棍分割（二分+dp）

http://www.lydsy.com/JudgeOnline/problem.php?id=1044

orz

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mkpii make_pair<int, int>
#define pdi pair<double, int>
#define mkpdi make_pair<double, int>
#define pli pair<ll, int>
#define mkpli make_pair<ll, int>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
#define printarr1(a, b) for1(_, 1, b) cout << a[_] << '\t'; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }

const int N=50005, MD=10007;
int n, a[N], sum[N], f[N], d[N], m, ans;

bool check(int x) {
int tot=0, s=0;
for1(i, 1, n) {
if(a[i]>x) return false;
s+=a[i];
if(s>x) { s=a[i]; ++tot; }
if(tot>m) return false;
}
return true;
}

int main() {
int l=1, r=0;
for1(i, 1, n) read(a[i]), r+=a[i], sum[i]=sum[i-1]+a[i];
while(l<=r) {
int mid=(l+r)>>1;
if(check(mid)) r=mid-1;
else l=mid+1;
}
ans=r+1;
printf("%d", ans);
for1(i, 0, n) if(sum[i]<=ans) d[i]=1; else break;
for1(j, 1, m) {
int k=0;
f[0]=d[0];
for1(i, 1, n) f[i]=(f[i-1]+d[i])%MD;
for1(i, j+1, n) {
while(k<i && sum[i]-sum[k]>ans) ++k;
d[i]=(f[i-1]-f[k-1]+MD)%MD;
}
// for1(i, j+1, n) {
// 	for1(k, 0, i-1) if(sum[i]-sum[k]<=ans) d[i][j]=(d[i][j]+d[k][j-1]);
// }
}
printf(" %d\n", d[n]);
return 0;
}


3 2
1
1
10

10 2

## HINT

n<=50000, 0<=m<=min(n-1,1000).

1<=Li<=1000.

## Source

posted @ 2014-11-14 14:25  iwtwiioi  阅读(247)  评论(0编辑  收藏