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

Source: Internet
Author: User
Tags integer numbers

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 sequenceA1, bytes,A2, middle..., middle ,...,ANIs called increasing, ifAILatency <latencyAILatency + latency 1ILatency <latencyN.

The sequenceS1, bytes,S2, middle..., middle ,...,SKIs called the subsequence of the sequenceA1, bytes,A2, middle..., middle ,...,AN, If there exist such a set of indexes 1 limit ≤ limitI1 worker <workerI2 rows <values... rows <valuesIKLimit ≤ limitNThatAIJSignature = SignatureSJ. In other words, the sequenceSCan be derived from the sequenceABy 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 integerN(1 digit ≤ DigitNLimit ≤ limit 500)-the length of the first sequence. The second line containsNSpace-separated integers from the range [0, limit 109]-elements of the first sequence. The third line contains an integerM(1 digit ≤ DigitMLimit ≤ limit 500)-the length of the second sequence. The fourth line containsMSpace-separated integers from the range [0, limit 109]-elements of the second sequence.

Output

In the first line outputK-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 


Question:

The given sequence is n, m (1 <= n, m <= 500. You need to find the longest common ascending subsequence of the two sequences.

Ideas:

The longest common subsequence is used. The longest ascending subsequence is also done. But this question is the longest common ascending subsequence... The solution must be dp pull.

I feel that the idea of the dp equation for this question is very good, reflecting the idea of strengthening constraints. Dp [I] [j. The first I of sequence A is processed, and the ascending sequence ends with B [j] of sequence B. I feel that it is a good idea to express the end of the State. Then the transfer is obvious.

If (A [I] = B [j])

Dp [I] [j] = dp [I-1] [k]; // k is smaller than j and B [k] <B [j]

Else

Dp [I] [j] = dp [I-1] [j];

We can use the I and j loops to save the time for finding k. Complexity O (n * m)

For details, see the code:

#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 lyrics

Song name: Round The Bend
Artist: The Beta Band
Album: 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 cocould 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 cocould spend
Where we are going
Round, round, round the bend
Round, round, round the bend

Music.baidu.com/song/2766592


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.