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;
}