hdu 3450 Counting Sequences

/*
n*n暴力 这个很好想
*/
#include<cstdio>
#define maxn 100010
#define mod 9901
using 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)/2
using 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;
}
时间: 2024-10-10 20:57:02

hdu 3450 Counting Sequences的相关文章

hdu 3450(树状数组+dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3450 Counting Sequences Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Others) Total Submission(s): 1815    Accepted Submission(s): 618 Problem Description For a set of seque

hdu 1396 Counting Triangles (递推)

Counting Triangles Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2012    Accepted Submission(s): 966 Problem Description Given an equilateral triangle with n the length of its side, program to

HDU 1264 Counting Squares (线段树-扫描线-矩形面积并)

Problem A : Counting Squares From:HDU, 1264 Problem Description Your input is a series of rectangles, one per line. Each rectangle is specified as two points(X,Y) that specify the opposite corners of a rectangle. All coordinates will be integers in t

hdu 2952 Counting Sheep

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2952 Counting Sheep Description A while ago I had trouble sleeping. I used to lie awake, staring at the ceiling, for hours and hours. Then one day my grandmother suggested I tried counting sheep after I'

HDU 1264 Counting Squares(线段树求面积的并)

Counting Squares Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1885    Accepted Submission(s): 946 Problem Description Your input is a series of rectangles, one per line. Each rectangle is sp

HDU 5862 Counting Intersections

题目:Counting Intersections 链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 题意:给出n 条平行于坐标轴的线段,问这些线段有多少交点(题目保证没有两条线段共享一个端点.保证没有重叠.保证线段长度大于0)n范围10万 思路: 将n 条线段2n 个点按x 排序,然后当遇到一个横向的左端点时,对应的y++,遇到右端点,y--,遇到竖线,交点数目加上 下端点到上端点 之间的y 的和.离散化加树状数组可做.要注意细节,

hdu 3450 离散化+dp+线段树优化

链接:http://acm.hdu.edu.cn/showproblem.php?pid=3450 题意: 给你一串长度为n的序列,给一个d,要求找出有几个子序列能够满足两个相邻的元素之间差值不超过d. 思路: dp.定义dp[i]表示以第i个为结束的满足条件的子序列的个数. 转移方程:dp[i]=(∑i?1j=1dp[j])+1(abs(num[i]?num[j])<=d) 答案就是dp数组的总和最后扣掉n就可以了. 此时会发现更新的时间复杂度是O(n2),这个显然是过不了的. 转移的复杂度是

【DFS序+树状数组】HDU 3887 Counting Offspring

http://acm.hdu.edu.cn/showproblem.php?pid=3887 [题意] 给定一棵树,给定这棵树的根 对于每个结点,统计子树中编号比他小的结点个数 编号从小到大一次输出 [思路] 从小到大处理每个结点,即统计当前结点的结果后,把当前结点插入到树状数组中 [AC] 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 5 const int maxn=1e5+2; 6 c

HDU 3887 Counting Offspring(DFS序)

Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3054    Accepted Submission(s): 1031 Problem Description You are given a tree, it’s root is p, and the node is numbered from 1