本文搬运自本人初中博客园博客,若图片加载不出来,可到原文查看:https://www.cnblogs.com/zhangtingxi/p/15574309.html

题目链接

首先朴素dp不用讲,设 dpidp_i 表示前 ii 个数划分的总方案数,SiS_i 表示前 ii 个数的和。

dpi=j=0i1dpj(SiSj0)dp_i=\sum_{j=0}^{i-1}dp_j\,\,\,(S_i-S_j\geqslant 0)

其中 dp0=1dp_0=1

可是这样的时间复杂度为 O(n2)O(n^2)

然而由于这道题数据过水,我们只需要特判 Si<0S_i<0 就continue,因为这样前面 ii 个数无论如何都划分不了。

暴力Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Problem: P2344 [USACO11FEB]Generic Cow Protests G
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P2344
// Memory Limit: 125 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
//#define M
#define mo 1000000009
#define N 100010
int n, m, i, j, k;
int s[N], f[N];

signed main()
{
// freopen("tiaoshi.in", "r", stdin);
// freopen("tiaoshi.out", "w", stdout);
n=read(); f[0]=1;
for(i=1; i<=n; ++i)
{
k=read();
s[i]=s[i-1]+k;
if(s[i]<0) continue;
for(j=0; j<i; ++j)
if(s[i]-s[j]>=0&&s[j]>=0)
f[i]=(f[i]+f[j])%mo;
f[i]%=mo;
}
// if(f[n]==0) printf("Impossible");
printf("%lld", f[n]);
return 0;
}

然而当数据变强我们就无能为力了。

于是我们可以考虑优化。

其实我们只需要求所有 fjf_j 满足 SjSiS_j\leqslant S_i 即可,那可以直接套个数组数组把 SS 存起来即可。

记得离散化。

初始化有点恶心,具体看代码吧。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
//#define M
#define mo 1000000009
#define N 100010
struct node
{
int x, y, id;
}a[N];
int n, m, i, j, k;
int f[N], cnt[N];

void psh(int x, int y)
{
while(x<=j)
{
cnt[x]=(cnt[x]+y)%mo;
x+=x&-x;
}
}

int qiu(int x)
{
int ans=0;
while(x)
{
ans=(ans+cnt[x])%mo;
x-=x&-x;
}
return ans;
}

bool cmp(node x, node y)
{
return x.x<y.x;
}

bool Cmp(node x, node y)
{
return x.id<y.id;
}

signed main()
{
// freopen("tiaoshi.in", "r", stdin);
// freopen("tiaoshi.out", "w", stdout);
n=read();
a[1].x=0; a[1].id=1; ++n;
for(i=2; i<=n; ++i) a[i].x=a[i-1].x+read(), a[i].id=i;
// a[++n].x=0; a[n].id=1;
sort(a+1, a+n+1, cmp);
for(a[1].y=j=1, i=2; i<=n; ++i)
{
if(a[i].x!=a[i-1].x) ++j;
a[i].y=j;
}
// printf("%lld ", j);
sort(a+1, a+n+1, Cmp);
f[1]=1; psh(a[1].y, 1);
for(i=2; i<=n; ++i)
{
f[i]=qiu(a[i].y);
psh(a[i].y, f[i]);
// printf("%lld\n", f[i]);
}
printf("%lld", f[n]);
return 0;
}