Problem Description
How many nondecreasing subsequences can you find in the sequence S = {s1, s2, S3, ....., SN}? For example, we assume this 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 an integer n that's 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 | We have carefully selected several similar problems for you:2642 2688 1541 3030 3015
d p [ i " Indicates the number of non-descending sequences ending with the first element
d P[I]= ∑ i? 1 J=1 d p [ j " +1
n up to 100,000, so use a tree-like array to optimize
/************************************************************************* > File Name:hdu2227.cpp > Auth Or:alex > Mail: [email protected] > Created time:2015 June 02 Tuesday 18:33 24 Seconds ******************************** ****************************************/#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <bitset>#include <set>#include <vector>using namespace STD;Const DoublePI =ACOs(-1.0);Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-15;typedef Long LongLL;typedefPair <int,int> PLL;Static Const intN =100010;Static Const intMoD =1000000007; LL Tree[n];intLowbit (intx) {returnX & (-X);}voidAddintNintX, LL val) { for(inti = x; I <= N; i + = Lowbit (i)) {Tree[i] + = val; Tree[i]%= mod; }}ll sum (intx) {LL ans =0; for(inti = x; I I-= Lowbit (i)) {ans + = tree[i]; Ans%= MoD; }returnAns;} LL Dp[n];intArr[n];intXis[n];intCntintSearchintVal) {intL =1, r = CNT;intMid while(L <= R) {mid = (L + r) >>1;if(Xis[mid] = = val) { Break; }if(Xis[mid] > val) {R = mid-1; }Else{L = mid +1; } }returnMid;}intMain () {intN while(~scanf("%d", &n)) {cnt =0;memset(Tree,0,sizeof(tree)); for(inti =1; I <= N; ++i) {scanf("%d", &arr[i]); XIS[++CNT] = Arr[i]; } sort (XIs +1, XIs + cnt +1); CNT = unique (XIs +1, XIs + cnt +1)-XIs-1;memset(DP,0,sizeof(DP)); LL ans =0; for(inti =1; I <= N; ++i) {intval = search (Arr[i]); LL sum = SUM (val); Dp[i] =1+ Sum; Dp[i]%= mod; Add (N, Val, dp[i]); Ans + = dp[i]; Ans%= MoD; }printf("%lld\n", ans); }return 0;}
hdu2227---Find the nondecreasing subsequences (dp+ tree-like array)