hdu 1257(最长递增子序列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1257

思路:其实,仔细一想,该序列的最长递增子序列即为至少要多少套导弹拦截系统;

dp[i]表示拦截第i个导弹时的最长递增子序列的长度;

View Code
 1 #include<iostream>
 2 #include<algorithm>
 3 const int N=1010;
 4 using namespace std;
 5 
 6 int h[N];
 7 int dp[N];
 8 
 9 int main(){
10     int n;
11     while(scanf("%d",&n)!=EOF){
12         for(int i=0;i<n;i++){
13             scanf("%d",&h[i]);
14         }
15         memset(dp,0,sizeof(dp));
16         int ans=0;
17         for(int i=0;i<n;i++){
18             ans=0;
19             for(int j=0;j<=i;j++){
20                 if(h[j]<h[i]&&ans<dp[j]){
21                     ans=dp[j];
22                 }
23             }
24             dp[i]=ans+1;//相当于加上本身
25         }
26         ans=0;
27         for(int i=0;i<n;i++){
28             ans=max(ans,dp[i]);
29         }
30         printf("%d\n",ans);
31     }
32     return 0;
33 }
原文地址:https://www.cnblogs.com/wally/p/2955768.html