【P2344 [USACO11FEB]Generic Cow Protests G】题解

题目链接

首先朴素dp不用讲,设 \(dp_i\) 表示前 \(i\) 个数划分的总方案数,\(S_i\) 表示前 \(i\) 个数的和。

\[dp_i=\sum_{j=0}^{i-1}dp_j\,\,\,(S_i-S_j\geqslant 0) \]

其中 \(dp_0=1\)

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

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

暴力Code
// 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; 
}

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

于是我们可以考虑优化。

其实我们只需要求所有 \(f_j\) 满足 \(S_j\leqslant S_i\) 即可,那可以直接套个数组数组把 \(S\) 存起来即可。

记得离散化。

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

Code

#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; 
}

posted @ 2021-11-18 20:31  zhangtingxi  阅读(59)  评论(0编辑  收藏  举报