标签:
Time Limit: 1000MS | Memory Limit: 32768KB | 64bit IO Format: %I64d & %I64u |
Description
Input
Output
Sample Input
8 389 207 155 300 299 170 158 65
Sample Output
2
子序列:
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; //动态分析 /* | | | | | | | | | | | | | | */ //从右到左呈现递增趋势 //运用最长上升子序列 int a[3005]; int dp[3005]; int main(){ int n; //freopen("D://imput.txt","r",stdin); while(~scanf("%d",&n)){ for(int i=1;i<=n;i++){ scanf("%d",&a[i]); } memset(dp,0,sizeof(dp)); int reco=1; for(int i=1;i<=n;i++){ dp[i]=1; for(int j=1;j<i;j++){ if(a[j]<a[i]){ dp[i]=max(dp[i],dp[j]+1); } } reco=max(reco,dp[i]); } printf("%d\n",reco); } return 0; }
#include <cstdio> #include <cstring> #include <algorithm> int a[3005],dp[3005],keep[3005]; //贪心算法,每个阶段寻找最优解(正解时间比动态规划要少空间消耗更大) using namespace std; int main(){ int cur=1,res=0,rec,reco,n; //freopen("D://output.txt","r",stdin); while(~scanf("%d",&n)){ if(n==0)continue; scanf("%d",&a[1]);reco=cur=keep[1]=dp[1]=1; for(int i=2;i<=n;i++){ scanf("%d",&a[i]);rec=0; for(int k=1;k<=cur;k++){ if(a[res=keep[k]]>=a[i]) dp[i]=dp[rec||a[rec]>a[res]? res:rec=res];//将导弹加在最低数的尾部 } if(a[rec]<a[i]) keep[dp[i]=++cur]=i; else keep[dp[i]=dp[rec]]=i; reco=max(reco,dp[i]);//比较,得出最多系统 //printf("[%d]\n",dp[i]); } printf("%d\n",reco); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/qq_18661257/article/details/47080343