【P2344 [USACO11FEB]Generic Cow Protests G】题解
题目链接
首先朴素dp不用讲,设 表示前 个数划分的总方案数, 表示前 个数的和。
其中 。
可是这样的时间复杂度为 。
然而由于这道题数据过水,我们只需要特判 就continue,因为这样前面 个数无论如何都划分不了。
暴力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;
}
然而当数据变强我们就无能为力了。
于是我们可以考虑优化。
其实我们只需要求所有 满足 即可,那可以直接套个数组数组把 存起来即可。
记得离散化。
初始化有点恶心,具体看代码吧。
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;
}
本文来自博客园,作者:zhangtingxi,转载请注明原文链接:https://www.cnblogs.com/zhangtingxi/p/15574309.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!