首页 > 代码库 > hdu 3450 Counting Sequences

hdu 3450 Counting Sequences

/*n*n暴力 这个很好想 */#include<cstdio>#define maxn 100010#define mod 9901using namespace std;int n,k,a[maxn],f[maxn],ans;int Abs(int a){    return a<0?-a:a;}int max(int a,int b){    return a<b?b:a;}int main(){    freopen("cin.txt","r",stdin);    freopen("right.out","w",stdout);    scanf("%d%d",&n,&k);    for(int i=1;i<=n;i++)        scanf("%d",&a[i]);    for(int i=1;i<=n;i++){        for(int j=1;j<i;j++){            if(Abs(a[i]-a[j])>k)continue;            f[i]=(f[i]+f[j]+1)%mod;        }        ans=(ans+f[i])%mod;    }        printf("%d\n",ans);    return 0;}
/*搞个数据结构来优化开始想错了题意 就写了线段树+离散化 后来反应过来了..树状数组就行QAQ开始写wa了 改着改着就搞出了两个线段树这就跑的有点慢了 但A点没问题 */#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define maxn 100010#define mod 9901#define lc k*2#define rc k*2+1#define mid (l+r)/2using namespace std;int n,m,d,a[maxn],c[maxn];int sum[maxn*4],tot[maxn*4];int init(){    int x=0,f=1;char s=getchar();    while(s<0||s>9){if(s==-)f=-1;s=getchar();}    while(s>=0&&s<=9){x=x*10+s-0;s=getchar();}    return x*f;}void Change(int k,int l,int r,int x,int y){    if(l==x&&r==x){        sum[k]=(sum[k]+y)%mod;return;    }    if(x<=mid)Change(lc,l,mid,x,y);    else Change(rc,mid+1,r,x,y);    sum[k]=(sum[lc]+sum[rc])%mod;}int Query(int k,int l,int r,int x,int y){    if(x<=l&&y>=r){        return sum[k];    }    int ret=0;    if(x<=mid)ret=(ret+Query(lc,l,mid,x,y))%mod;    if(y>mid)ret=(ret+Query(rc,mid+1,r,x,y))%mod;    return ret;}void change(int k,int l,int r,int x,int y){    if(l==x&&r==x){        tot[k]=(tot[k]+y)%mod;return;    }    if(x<=mid)change(lc,l,mid,x,y);    else change(rc,mid+1,r,x,y);    tot[k]=(tot[lc]+tot[rc])%mod;}int query(int k,int l,int r,int x,int y){    if(x<=l&&y>=r){        return tot[k];    }    int ret=0;    if(x<=mid)ret=(ret+query(lc,l,mid,x,y))%mod;    if(y>mid)ret=(ret+query(rc,mid+1,r,x,y))%mod;    return ret;}int main(){        while(~scanf("%d%d",&n,&d)){        memset(tot,0,sizeof(tot));        memset(sum,0,sizeof(sum));        m=0;        for(int i=1;i<=n;i++){            a[i]=init();c[i]=a[i];        }        sort(c+1,c+1+n);        m=unique(c+1,c+1+n)-c-1;        int L,R,M,S,s;        for(int i=1;i<=n;i++){            L=lower_bound(c+1,c+1+m,a[i]-d)-c;            R=upper_bound(c+1,c+1+m,a[i]+d)-c-1;            M=lower_bound(c+1,c+1+m,a[i])-c;            S=Query(1,1,m,L,R);            s=query(1,1,m,L,R);            Change(1,1,m,M,S+s);            change(1,1,m,M,1);        }        printf("%d\n",sum[1]);    }    return 0;}

 

hdu 3450 Counting Sequences