L - Find the nondecreasing subsequences HDU - 2227 (离散化树状数组)

How many nondecreasing subsequences can you find in the sequence S = {s1, s2, s3, ...., sn} ? For example, we assume that S = {1, 2, 3}, and you can find seven nondecreasing subsequences , {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}.

Input

The input consists of multiple test cases. Each case begins with a line containing a positive integer n that is the length of the sequence S, the next line contains n integers {s1, s2, s3, ...., sn}, 1 <= n <= 100000, 0 <= si <= 2^31.

Output

For each test case, output one line containing the number of nondecreasing subsequences you can find from the sequence S, the answer should % 1000000007.

Sample Input

3 
1 2 3

Sample Output

7

题意+思路:这道题目就是问你给定你一个数列,问你在这个数列里面有多少个子序列是非递减子序列。由于数字大小0 <= si <= 2^31.所以不能开那么大的数组。所以就离散化一下就可以了。只用开n大小的空间,节省了不少空间。离散化就是先sort排序,然后unique,然后就是eraser 最后就是lower_bound()查找离散化后的位置就可以了。

#include<cstdio>
#include<algorithm>
#define FFC(i,a,b) for(int i=a;i<=b;i++)
#define LL long long
using namespace std;
LL tree[100010];
int maxn,mod=1000000007,l,r,t,len,mid;
int dt[100010],a[100010];
void init(int n)
{
    maxn=n;
    for(int i=0; i<=maxn; i++)tree[i]=0;
}
inline void add(int x,int v)
{
    for(int i=x; i<=maxn; i+=i&-i)tree[i]=(tree[i]+v)%mod;
}
inline LL sum(int x)
{
    LL an=0;
    for(; x>0; x-=x&-x)an=(an+tree[x])%mod;
    return an;
}
inline int lowb(int x)
{
    for(l=1,r=len; l<=r;)if(a[mid=(l+r)>>1]>=x)r=(t=mid)-1;
        else l=mid+1;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        FFC(i,1,n)
        {
            scanf("%d",&dt[i]);
            a[i]=dt[i];//离散化数组
        }
        sort(a+1,a+1+n);
        len=unique(a+1,a+1+n)-a-1;//离散化
        init(len);
        LL ans=0;
        FFC(i,1,n)
        {
            lowb(dt[i]);//二分查找
            int tt=sum(t);
            add(t,tt+1);
            ans=(ans+tt+1)%mod;//注意,一个数它本身也是一个递增子列,所以要加一
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值