hdu 4991 Ordered Subsequence

來源:互聯網
上載者:User

標籤:des   blog   http   os   io   java   ar   strong   for   

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 many 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官方題解
1003 Ordered Subsequence首先數字有1萬個,先離散化一下,把所有數字對應到1到n之間。這樣對結果不影響。dp[i][j]代表以第i個數字結尾上升子序列長度為j的種數。dp[i][j]=sum{dp[k][j-1]}  for each a[k]<a[i]&&k<i直接寫迴圈會逾時。需要最佳化。可以用平衡樹進行最佳化,上述的迴圈過程可以看成是一個區間求和過程。用線段樹或者樹狀數組可以解決。這樣最終的複雜度是n*m*log(n)

這裡我用100個數組數組搞了搞,注意中間的模數
#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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.