poj2533 最長上升子序列

題意:求最長上升子序列的長度

代碼:

#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <cstdio>
#include <string>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <list>
#include <map>
#include <set>
#define sss(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a) memset(a,0,sizeof(a))
#define ss(a,b) scanf("%d%d",&a,&b)
#define s(a) scanf("%d",&a)
#define INF 0x3f3f3f3f
#define w(a) while(a)
#define PI acos(-1.0)
#define LL long long
#define eps 10E-9
#define N 100010<<1
#define mod 100000000
using namespace std;
void mys(int& res)
{
    int flag=0;
    char ch;
    while(!(((ch=getchar())>='0'&&ch<='9')||ch=='-'))
        if(ch==EOF)  res=INF;
    if(ch=='-')  flag=1;
    else if(ch>='0'&&ch<='9')  res=ch-'0';
    while((ch=getchar())>='0'&&ch<='9')  res=res*10+ch-'0';
    res=flag?-res:res;
}
void myp(int a)
{
    if(a>9)
        myp(a/10);
    putchar(a%10+'0');
}
/*************************THE END OF TEMPLATE************************/
int arr[1010];
int dp[1001];//dp[i]表示從i到n的最長上升子序列
int main(){
    int n;
    w(~s(n)){
        for(int i=1; i<=n; i++) s(arr[i]);
        dp[n] = 1;
        int bg;
        for(int i=n; i>=1; i--){
            bg = 0;
            for(int j=i+1; j<=n; j++){
                    if(arr[j]>arr[i]){
                        if(dp[j] > bg)
                            bg = dp[j];
                    }
            }
            dp[i] = bg + 1;
        }
        sort(dp+1, dp+1+n);//因爲最優解可能在別的位置
        cout<<dp[n]<<endl;
    }
    return 0;
}


發佈了81 篇原創文章 · 獲贊 71 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章