Find the nondecreasing subsequences
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1072 Accepted Submission(s): 370
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
Author
8600
Recommend
lcy
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int mod=1000000007; const int N=100010; struct node{ int val,id; }a[N]; int n,b[N],c[N],arr[N]; int lowbit(int x){ return x&(-x); } void update(int i,int val){ while(i<=n){ arr[i]+=val; arr[i]%=mod; i+=lowbit(i); } } int Sum(int i){ int ans=0; while(i>0){ ans+=arr[i]; ans%=mod; i-=lowbit(i); } return ans; } int cmp(node a,node b){ return a.val<b.val; } int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d",&n)){ memset(b,0,sizeof(b)); memset(arr,0,sizeof(arr)); for(int i=1;i<=n;i++){ scanf("%d",&a[i].val); a[i].id=i; } sort(a+1,a+n+1,cmp); b[a[1].id]=1; for(int i=2;i<=n;i++) if(a[i].val==a[i-1].val) b[a[i].id]=b[a[i-1].id]; else b[a[i].id]=i; for(int i=1;i<=n;i++){ c[i]=Sum(b[i]); update(b[i],c[i]+1); } printf("%d\n",Sum(n)); } return 0; }