HDU find the nondecreasing subsequences

来源:互联网 发布:手机渗透软件 编辑:程序博客网 时间:2024/06/07 03:17

Problem Description
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

 

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;

typedef long long ll;

ll n;
ll a[100005],b[100005];
ll c[100005],dp[100005];

struct Node
{
    ll value;
    int index;
    bool operator < (const Node&b)const
    {
        return value<b.value;
    }
}node[100005];

ll lowbit(ll i)
{
    return i&(-i);
}

void updata(ll i,ll data)
{
    while(i<=n)
    {
        c[i]+=data;
        c[i]=c[i]%1000000007;
        i+=lowbit(i);
    }
}

ll query(ll i)
{
    ll res=0;
    while(i>0)
    {
        res+=c[i];
        i-=lowbit(i);
    }
    return res%1000000007;
}

int main()
{
    int i,j;
    while(~scanf("%lld",&n))
    {
        memset(dp,0,sizeof(dp));
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        for(i=1;i<=n;i++)
        {
            scanf("%lld",&node[i].value);
            node[i].index=i;
        }
        sort(node+1,node+n+1);
        b[ node[1].index ]=1;
        for(i=2;i<=n;i++)
        {
            if(node[i].value==node[i-1].value)
                b[ node[i].index ]=b[ node[i-1].index ];
            else
                b[ node[i].index ]=i;
        }
        for(i=1;i<=n;i++)
        {
            dp[i]=query(b[i]);
            updata(b[i],dp[i]+1);
        }
        cout<<query(n)<<endl;
    }
    return 0;
}

 

 

 

原创粉丝点击