完美世界笔试题-递增子序列B-最长递增子序列打印

#include
#include
#include
using namespace std;

const int maxn = 3005;
int A[maxn],d[maxn],fron[maxn];

int f(int i)
{
    for(int j = 0; j < i; j ++)
    {
        if(A[j] < A[i])
        {
            if(d[j] + 1 > d[i])
            {
                d[i] = d[j] + 1;
                fron[i] = j;
            }
        }
    }
    return d[i];
}

int main()
{
    int t;
    cin >> t;
    int n;
    while(t --)
    {
        cin >> n;
        memset(A, 0, sizeof(A));
        for(int i = 0; i < n; i ++)
        {
            cin >> A[i];
            d[i] = 1;
            fron[i] = i;
        }
        int ans = -1, ansid = 0;
        for(int i = 0; i < n; i ++)
        {
            if(f(i) > ans)
            {
                ans = f(i);
                ansid = i;
            }
        }
        stack<int> st;
        while(fron[ansid] != ansid)
        {

            st.push(A[ansid]);
            ansid = fron[ansid];
        }
        st.push(A[ansid]);
        bool flag = false;
        while(!st.empty())
        {
            if(flag)cout << ' ';
            flag = true;
            cout << st.top();
            st.pop();
        }
        cout << endl;
    }
}

 已过。

转载于:https://www.cnblogs.com/mu-ye/p/7760890.html

你可能感兴趣的:(完美世界笔试题-递增子序列B-最长递增子序列打印)