LCIS code force 10D
生活随笔
收集整理的這篇文章主要介紹了
LCIS code force 10D
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這一道題,我用的是O(n^2)的算法,仔細分析一下可以發現,用O(n^3)的算法很危險,所以我建議用O(n^2)的算法
#include<cstdio>
#include<algorithm>
using namespace std;
int y[705],x[705];
int f[705][705];int path[1005];
int n,m,len,w;
void output(int now){//回朔打印路徑
if(now!=0){
output(path[now]);
printf("%d ",y[now]);
}//先找到第一個數,然后在遞歸輸出
}
int main(){
? ?scanf("%d",&n);
? ?for(int i=1;i<=n;i++)scanf("%d",&x[i]);
? ?scanf("%d",&m);
? ?for(int i=1;i<=m;i++)scanf("%d",&y[i]);
? ?int ans=0;
? ?for(int i=1;i<=n;i++){
? ? ? len=0,w=0;//len是用來記錄x[1....i-1]和y[1...j]的最長子序列的長度
? ? ? for(int j=1;j<=m;j++){
? ? ? ? ?f[i][j]=f[i-1][j];
? ? ? ? ?if(y[j]<x[i]&&len<f[i-1][j]){更新len,取最大值,替代了k的循環
? ? ? ? ? ?len=f[i-1][j],w=j;//記錄下來
? ? ? ? ?}
? ? ? ? ? if(y[j]==x[i]){
? ? ? ? ? ? f[i][j]=len+1,path[j]=w;//我們把路徑記錄下來,并把f[i][j]用len+1存下來(因為x[1...i]內包括x[1...i-1],y[1...j]內包括y[1...j-1])
? ? ? ? ? }
? ? ? ?}
? ?}
? ?for(int i=1;i<=m;i++){
if(ans<f[n][i]){
ans=f[n][i],w=i;//找最大答案
}
? ?}
? ? printf("%d\n",ans);
? ? if(ans)output(w);
? ? return 0;
}
轉載于:https://www.cnblogs.com/c201904xyorz/p/9990789.html
總結
以上是生活随笔為你收集整理的LCIS code force 10D的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IT项目协调-网络整改项目
- 下一篇: LAMP架构之个人博客搭建