Cod sursă (job #470978)

Utilizator avatar tziplea_stefan Tiplea Stefan Alexandru tziplea_stefan IP ascuns
Problemă S2C Compilator cpp | 1,13 kb
Rundă Arhiva de probleme Status evaluat
Dată 15 apr. 2019 19:08:50 Scor 100
#include <fstream>
#include <algorithm>

using namespace std;

ifstream fin("s2c.in");
ofstream fout("s2c.out");

const int VAL=2005;

int N, M, i, j, k;
int T, mx, v[VAL], X;
int nr[VAL][VAL], ANS;
pair <int, int> P[VAL];

int main()
{
    fin >> T;
    while (T>0)
    {
        T--;
        fin >> N;
        for (i=1; i<=N; i++)
        {
            fin >> P[i].first;
            P[i].second=i;
        }
        if (N<=2)
        {
            fout << N << '\n';
            continue;
        }
        sort(P+1, P+N+1);
        ANS=mx=0;
        for (i=1; i<=N; i++)
        {
            if (P[i].first!=P[i-1].first)
                mx++;
            v[P[i].second]=mx;
        }
        for (j=1; j<=N; j++)
        {
            for (i=1; i<=mx; i++)
                nr[j][i]=0;
            for (i=1; i<j; i++)
            {
                X=max(2, 1+nr[i][v[j]-1]);
                ANS=max(ANS, X);
                nr[j][v[i]]=max(nr[j][v[i]], X);
            }
            for (i=1; i<=mx; i++)
                nr[j][i]=max(nr[j][i-1], nr[j][i]);
        }
        fout << ANS << '\n';
    }
    fin.close();
    fout.close();
    return 0;
}