Cod sursa(job #66179)
#include <cstdio>
#include <algorithm>
#include <memory>
#define maxn 5001
FILE *in = fopen("secv.in","r"), *out = fopen("secv.out","w");
int n;
int a[maxn];
int b[maxn];
int c[maxn];
int l1[maxn], l2[maxn];
void read()
{
fscanf(in, "%d", &n);
for ( int i = 1; i <= n; ++i )
fscanf(in, "%d", &a[i]), b[i] = a[i];
}
int main()
{
read();
std::sort(b+1, b+n);
int k = 0;
c[++k] = b[1];
for ( int i = 2; i <= n; ++i )
if ( b[i] != c[k] )
c[++k] = b[i];
for ( int i = 1; i <= n; ++i )
{
for ( int j = 1; j <= k; ++j )
{
if ( a[i] == c[j] )
l1[j] = l2[j-1] + 1;
else
{
if ( l2[j] > l2[j-1] )
l1[j] = l2[j] + 1;
else
l1[j] = l2[j-1] + 1;
}
}
memcpy(l2, l1, sizeof(l2));
}
// for ( int i = 1; i <= n; ++i )
// {
// for ( int j = 1; j <= k; ++j )
// printf("%d ", l[i][j]);
// printf("\n");
// }
fprintf(out, "%d\n", l2[k]);
return 0;
}
/*
8
2 1 3 2 1 3 4 5
1 2 3 4 5
*/