标签:int char lower col ios upd end += reg
解题思路
树状数组优化dp,f[i]表示前i个奶牛的分组的个数,那么很容易得出$f[i]=\sum\limits_{1\leq j\leq i}f[j-1]*(sum[i]\ge sum[j-1])$,但是这样的时间复杂度是$O(n^2)?$,所以考虑优化,发现必须满足$sum[i]\ge sum[j-1]?$才能进行转移,那么直接离散化后用树状数组维护一个前缀和即可。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> using namespace std; const int MAXN = 100005; const int mod = 1000000009; inline int rd(){ int x=0,f=1;char ch=getchar(); while(!isdigit(ch)) {f=ch==‘-‘?0:1;ch=getchar();} while(isdigit(ch)) {x=(x<<1)+(x<<3)+ch-‘0‘;ch=getchar();} return f?x:-x; } //f[i] 到了第i个人 //f[i]=f[i-k]+1 int f[MAXN],t[MAXN],rk[MAXN]; int n,sum[MAXN],cpy[MAXN]; inline bool cmp(int x,int y){ return x<y; } int query(int x){ int ret=0; for(;x;x-=x&-x) ret+=t[x],ret%=mod; return ret; } void update(int x,int k){ for(;x<=n;x+=x&-x) t[x]+=k,t[x]%=mod; } int main(){ n=rd();f[0]=1; for(register int i=1;i<=n;i++) sum[i]=sum[i-1]+rd(),cpy[i]=sum[i]; sort(cpy+1,cpy+1+n,cmp);int u=unique(cpy+1,cpy+1+n)-cpy-1; for(register int i=1;i<=n;i++) rk[i]=lower_bound(cpy+1,cpy+1+u,sum[i])-cpy; for(register int i=1;i<=n;i++){ if(sum[i]>=0) f[i]=1; f[i]+=query(rk[i]); update(rk[i],f[i]); } cout<<f[n]<<endl; return 0; }
标签:int char lower col ios upd end += reg
原文地址:https://www.cnblogs.com/sdfzsyq/p/9736952.html