标签:style blog class c code tar
一个序列有N个数:A[1],A[2],A[3]……A[N],求最长非降子序列的长度。
#include <iostream>
using namespace std;
int lis(int
a[],int n){
int
d[n];
int len=1;
for(int i=0;i<n;i++){
d[i]=1;
for(int j=0;j<i;j++){
if(a[j]<=a[i]&&d[j]+1>d[i]){
d[i]=d[j]+1;
}
if(d[i]>len)len=d[i];
}
}
return
len;
}
int main()
{
int a[]={5,3,4,8,6,7};
cout<<lis(a,6)<<endl;
return 0;
}
Problem Statement for ZigZag | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2010, TopCoder, Inc. All rights reserved. |
#include<stdio.h>
#include<iostream>
using namespace std;
#define LEN 100
int
a[LEN];
int d[LEN];
int
lis(int n){
if(n==1){return 0;}
int len=0;
for(int i=0;i<n;i++){
d[i]=1;
}
for(int i=0;i<n;i++){
for(int j=0;j<i;j++){
if(j>0&&i>0&&(a[j-1]-a[i-1])*(a[j]-a[i])<0 && d[j]+1 >d[i])
d[i]=d[j]+1;
if(len<d[i])len=d[i];
}
}
return len;
}
int main(){
int n;
freopen("in.txt","r",stdin);
while(cin>>n){
for(int i=0;i<n;i++)
cin>>a[i];
cout<<lis(n)+1<<endl;
}
return 0;
}
【动态规划初级】longest increasing subsequence,布布扣,bubuko.com
【动态规划初级】longest increasing subsequence
标签:style blog class c code tar
原文地址:http://www.cnblogs.com/zhaokongnuan/p/e0958fbb1784a7871c3eb8d5ffe6120d.html