HDU 4991 ordered subsequence

Source: Internet
Author: User
Ordered subsequence

Time Limit: 4000/2000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 114 accepted submission (s): 58


Problem descriptiona numeric sequence of AI is ordered if A1 <A2 <...... <An. Let the subsequence of the given numeric sequence (A1, A2 ,......, An) be any sequence (aI1, ai2 ,......, AIK), where 1 <= I1 <I2 <...... <Ik <= n. for example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, eg. (1, 7), (3, 4, 8) and other others.

Your program, when given the numeric sequence, must find the number of its ordered subsequence with exact M numbers.

 

Inputmulti test cases. each case contain two lines. the first line contains two integers n and m, n is the length of the sequence and M represent the size of the subsequence you need to find. the second line contains the elements of sequence-N integers in the range from 0 to 987654321 each.
Process to the end of file.
[Technical Specification]
1 <= n <= 10000
1 <= m <= 100

 

Outputfor each case, output answer % 123456789.

 

Sample input3 21 1 27 31 7 3 5 9 4 8

 

Sample output212

 

Sourcebestcoder round #8 Official question
1003 ordered subsequence: first, there are 10 thousand numbers. First, discretization them to correspond all numbers to 1 to n. This will not affect the results. DP [I] [J] indicates the number of ascending subsequences whose length is J at the end of the number I. DP [I] [J] = sum {DP [k] [J-1]} for each a [k] <A [I] & K <I direct write loop times out. Optimization is required. The balance tree can be used for optimization. The above cyclic process can be considered as a range summation process. You can use a line segment tree or a tree array. The final complexity is N * m * log (n)

Here I used 100 array arrays. Pay attention to the modulo in the middle.
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<vector>#include<set>#include<stack>#include<map>#include<ctime>#define maxn 10010#define LL long long#define INF 999999#define mod 123456789LLusing namespace std;struct node{    int id;    LL val;    bool operator <(const node&s)const    {        return val < s.val ;    }}qe[maxn];LL xt[101][maxn] ;int n ,a[maxn] ;LL dp[maxn][101] ;void insert(int id,int x,int add){    while( x <= n )    {        xt[id][x]+= add;        if(xt[id][x] >= mod) xt[id][x] -= mod;        x += (x&-x) ;    }}LL sum(int id,int x){    LL ans=0;    while(x >0)    {        ans += xt[id][x] ;        if(ans>=mod) ans -= mod;        x -= (x&-x) ;    }    return ans;}int main(){   int m,i,j ;   while( scanf("%d%d",&n,&m) != EOF)   {       for( i = 1 ; i <= n ;i++)       {           scanf("%I64d",&qe[i].val) ;           qe[i].id= i;       }       sort(qe+1,qe+1+n) ;       j = 1 ;       a[qe[1].id] = j ;       for( i = 2 ; i <= n ;i++)       {           if(qe[i].val==qe[i-1].val) a[qe[i].id]=j ;           else a[qe[i].id] = ++j;       }       memset(xt,0,sizeof(xt)) ;       memset(dp,0,sizeof(dp)) ;       LL ans=0;       for(i = 1 ; i <= n ;i++)       {           dp[i][1] = 1 ;           for( j = 2 ; j <= m && j <= i ;j++)           {               dp[i][j] = sum(j-1,a[i]-1) ;           }           ans = (ans+dp[i][m])%mod;           for( j = 1 ; j <= m && j <= i;j++)           {               insert(j,a[i],dp[i][j]) ;           }       }       cout << ans << endl;   }   return 0 ;}

  

 

HDU 4991 ordered subsequence

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.