problem 2236 14th targetAccept:17 submit:35 time limit:1000 mSec Memory limit:32768 KBproblem Description
The eyes of the police, Princess Miles, Dr. Agasa and other people in a succession of unknown identity of the person's plot, Conan tracking injury Dr. Agasa's murderer, according to several cases on the scene left clues found that the murderer in accordance with the sequence of playing cards. After a series of reasoning, Conan found that the names of the victims include the number of cards, and the size of poker is strictly increasing, in addition to the victim and the small Yogoro.
To avoid the appearance of the next victim, Conan ranked the number of people who might have been ambushed by relevance, that is, the order cannot be changed. Conan needs to know the total number of possible results, to meet the victim's name appears in the numbers are strictly increased, but he to find the key evidence, so this task is given to you.
(If you do not understand what is said above, the question is to find the number of Strictly ascending subsequence in a sequence.) For example, the sequence (1,3,2) of the strict increment subsequence has (1), (3), (2), (1,3), (five), a total of 5. A different sub-sequence, such as a sequence (3,3), that has the same length but different positions, is 2. )
Input
Multiple sets of data (<=10), processed to EOF.
The first line enters a positive integer n (n≤100 000), representing a total of n individuals.
The second line has a total of n integer Ai (1≤ai≤10^9), which represents the number in the first person name.
Output
Each set of data outputs an integer that represents all possible results. Since the result may be large, the output is modulo after 1 000 000 007.
Sample Input3 1 3 2Sample Output5SourceThe 13th session of Fuzhou University Program Design Competition Solution: DP thinking very well, dp[i] represents the number of increments of the end of I; dp[i] = 1 + sum{dp[j]} (J < I, A[j] < a[i]); see SUM (Dp[j]), A[]j < A[i] , we can easily think of a tree-like array; Because the numbers are too large, then we are discretized; code:
#include <iostream>#include<cmath>#include<cstring>#include<cmath>#include<algorithm>#include<Set>#include<queue>#include<vector>using namespacestd;Const intMAXN =100010;intDP[MAXN];intA[MAXN], B[MAXN];intTree[maxn];typedefLong LongLL;Const intMOD = 1e9 +7;intLowbit (intx) {returnX & (-x);}voidUpdateintIintx) { while(I <MAXN) {Tree[i]+=x; Tree[i]%=MOD; I+=lowbit (i); }}intSuminti) { intAns =0; while(I >0) {ans+=Tree[i]; Ans%=MOD; I-=lowbit (i); } returnans;}intMain () {intN; while(~SCANF ("%d", &N)) { for(inti =0; I < n; i++) {scanf ("%d", A +i); B[i]=A[i]; } sort (A, a+N); Memset (DP,0,sizeof(DP)); memset (Tree,0,sizeof(tree)); LL ans=0; intK = Unique (A, a + N)-A; for(inti =0; I < n; i++) { intp = Lower_bound (A, a + K, b[i])-A +1; Dp[i]= SUM (P-1) +1; Update (P, dp[i]); } for(inti =0; I < n; i++) {ans+=Dp[i]; Ans%=MOD; } printf ("%lld\n", ans%MOD); } return 0;}
The game was just beginning to think about using priority queue optimization, no use, or time-out;
Code:
#include <iostream>#include<cmath>#include<cstring>#include<cmath>#include<algorithm>#include<Set>#include<queue>using namespacestd;Const intMAXN =100010;intDP[MAXN];intA[MAXN];intTree[maxn];typedefLong LongLL;Const intMOD = 1e9 +7;structnode{intv,p; FriendBOOL operator<(Node A, Node B) {returnA.V >B.V; }};intMain () {intN; while(~SCANF ("%d", &N)) { for(inti =1; I <= N; i++) {scanf ("%d", A +i); } memset (DP,0,sizeof(DP)); LL ans=0; Priority_queue<Node>Q, Q1; Node x; for(inti =1; I <= N; i++) {Dp[i]=1; X.V= A[I];X.P =i; Q.push (x); while(Q.top (). V <A[i]) {Dp[i]= (Dp[i] + dp[q.top (). P])%MOD; Q1.push (Q.top ()); Q.pop (); } while(!Q1.empty ()) {Q.push (Q1.top ()); Q1.pop (); } } for(inti =1; I <= N; i++) {ans+=Dp[i]; Ans%=MOD; } printf ("%lld\n", ans%MOD); } return 0;}
14th Target (DP + Tree Array)