codeforces 463D Gargari and Permutations(dp)

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   ar   for   art   

題目

 

參考網上的代碼的、、、

 

//要找到所有序列中的最長的公用子序列,//定義狀態dp[i]為在第一個序列中前i個數字中的最長公用子序列的長度,//狀態轉移方程為dp[i]=max(dp[i],dp[j]+1); j<i//先預先處理出兩個數在所有序列中的位置關係,//例如兩個數a和b,只要在任意一個序列中a在b的後面,則記after[a][b]=1。//在遞推的時候如果!after[a][b],則進行狀態轉移。#include <cstdio>#include <cstring>#include <algorithm>using namespace std ;int a[6][1010];int after[1010][1010];int dp[1010];int main () {    int n,k;    scanf("%d%d",&n,&k);    int ii=0;    for(int i=0;i<k;i++)    {        for(int j=0;j<n;j++)        {            scanf("%d",&a[i][j]);        }    }    memset(after,0,sizeof(after));    memset(dp,0,sizeof(dp));    for(int i=0;i<k;i++)    {        for(int j=0;j<n;j++)        {            for(int k=j+1;k<n;k++)            {                after[a[i][k]][a[i][j]]=1; //存在 k在j後面             }        }    }    int ans=0;        //直接對1~n進行狀態轉移不可以    //要根據第一行來dp    for(int i=0;i<n;i++)    {        dp[i]=1;        for(int j=0;j<i;j++)        {            if(after[a[0][j]][a[0][i]]==0)dp[i]=max(dp[i],dp[j]+1);        }        ans=max(dp[i],ans);    }    printf("%d\n",ans);    return 0 ;}        
View Code

 

codeforces 463D Gargari and Permutations(dp)

聯繫我們

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