Pagini recente » Cod sursa (job #1107808) | Cod sursa (job #2976641) | Cod sursa (job #2715986) | Cod sursa (job #2544602) | Cod sursa (job #1425157)
#include <iostream>
#include <fstream>
using namespace std;
#define nmax 1000000+7
ifstream in("scmax.in");
ofstream out("scmax.out");
int main()
{
int n;
in >> n;
int a[n];
for( int i = 0 ; i < n ; i++)
in >> a[i];
int best[n],sol[n],k=0;
best[0] = 1;
for(int i = 0 ; i < n ; i++)
sol[i] = i;
for(int i = 1 ; i < n ; i ++)
{
int best_current = 1;
for(int j = 0 ; j < i ; j ++)
if (best[j]>=best_current && a[j]<a[i])
{
best_current = best[j]+1;
sol[i] = j;
}
best[i] = best_current;
}
int max_pos = 0,max_val=0 ;
for( int i = 0 ; i < n ; i++)
if (best[i]>max_val)
{
max_val = best[i];
max_pos = i;
}
int actual_solution[n],p = 0;
while(1)
{
actual_solution[++p] = a[max_pos];
if(sol[max_pos]==max_pos)
break;
max_pos = sol[max_pos];
}
out << max_val<<"\n";
for(int i = p; i >0; i --)
out << actual_solution[i]<<" ";
return 0;
}