Codeforces Beta Round #10 D. LCIS(DP&LCIS),

來源:互聯網
上載者:User

Codeforces Beta Round #10 D. LCIS(DP&LCIS),

D. LCIStime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

This problem differs from one which was on the online contest.

The sequence a1, a2, ..., an is called increasing, if ai < ai + 1 for i < n.

The sequence s1, s2, ..., sk is called the subsequence of the sequence a1, a2, ..., an, if there exist such a set of indexes 1 ≤ i1 < i2 < ... < ik ≤ n that aij = sj. In other words, the sequence s can be derived from the sequence aby crossing out some elements.

You are given two sequences of integer numbers. You are to find their longest common increasing subsequence, i.e. an increasing sequence of maximum length that is the subsequence of both sequences.

Input

The first line contains an integer n (1 ≤ n ≤ 500) — the length of the first sequence. The second line contains nspace-separated integers from the range [0, 109] — elements of the first sequence. The third line contains an integer m (1 ≤ m ≤ 500) — the length of the second sequence. The fourth line contains m space-separated integers from the range [0, 109] — elements of the second sequence.

Output

In the first line output k — the length of the longest common increasing subsequence. In the second line output the subsequence itself. Separate the elements with a space. If there are several solutions, output any.

Sample test(s)input
72 3 1 6 5 4 641 3 5 6
output
33 5 6 
input
51 2 0 2 131 0 1
output
20 1 


題意:

給你長度分別為n,m(1<=n,m<=500)的序列。要你求這兩個序列的最長公用上升子序列。

思路:

最長公用子序列做過。最長上升子序列也做過。但是這題時最長公用上升子序列。。。解法肯定還是dp拉。

感覺這題的dp方程的思想很不錯,體現了一種加強約束的思想。dp[i][j]表示。處理完A序列的前i個,且上升序列以B序列的B[j]結尾的最長子序列。感覺這個把狀態體現以什麼結尾是很不錯的思想。然後轉移顯而易見了。

if(A[i]==B[j])

dp[i][j]=dp[i-1][k];//k是小於j且B[k]<B[j]

else

dp[i][j]=dp[i-1][j];

我們可以i,j迴圈這樣就可以省掉找k的時間。複雜度O(n*m)

詳細見代碼:

#include<bits/stdc++.h>using namespace std;const int INF=0x3f3f3f3f;const int maxn=100010;typedef long long ll;int dp[550][550],A[550],B[550],path[550][550],n,m;bool print(int x){    if(!x)        return false;    if(print(path[n][x]))        printf(" %d",B[x]);    else        printf("%d",B[x]);    return true;}int main(){    int i,j,tp,ans,pos;    while(~scanf("%d",&n))    {        for(i=1;i<=n;i++)            scanf("%d",&A[i]);        scanf("%d",&m);        for(i=1;i<=m;i++)            scanf("%d",&B[i]);        memset(dp,0,sizeof dp);        for(i=1;i<=n;i++)        {            tp=pos=0;            for(j=1;j<=m;j++)            {                dp[i][j]=dp[i-1][j];                path[i][j]=path[i-1][j];                if(A[i]==B[j]&&tp+1>dp[i][j])                    dp[i][j]=tp+1,path[i][j]=pos;                if(B[j]<A[i]&&dp[i-1][j]>tp)                    tp=dp[i-1][j],pos=j;            }        }        ans=1;        for(i=1;i<=m;i++)            if(dp[n][i]>dp[n][ans])                ans=i;        printf("%d\n",dp[n][ans]);        if(dp[n][ans])            print(ans);        printf("\n");    }    return 0;}



Round The Bend 歌詞

歌曲名:Round The Bend
歌手:The Beta Band
專輯:The Beta Band

Round The Bend
Beck
Sea Change
We don't have to worry
Life goes where it does
Faster than a bullet
From an empty gun
Turn yourself over
Loose change we could spend
Grinding down diamonds
Round, round, round the bend
Round, round, round the bend
People pushing harder
Up against themselves
Make their daggers sharper
Than their faces tell
Babe, its your time now
Loose change we could spend
Where we are going
Round, round, round the bend
Round, round, round the bend

music.baidu.com/song/2766592
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.