Pagini recente » Cod sursa (job #2901552) | Cod sursa (job #28548) | Cod sursa (job #3194001) | Cod sursa (job #2922492) | Cod sursa (job #3281283)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
const int NMAX = 100000;
int n, posmax;
int v[NMAX+1];
int dp[NMAX+1];//subsirul crescator maximal de lungime maxima care se termina in i
int pos[NMAX+1];
int rez;
vector <int> ans;
int main()
{
fin >> n;
for(int i=1; i<=n; i++)
fin >> v[i], dp[i] = 1;
pos[1] = -1;
for(int i=2; i<=n; i++)
{
int maxi = 0;
int ind = -1;
for(int j=1; j<i; j++)
if(v[i] > v[j])
if(dp[j] > maxi)
maxi = dp[j], ind = j;
dp[i] = maxi+1;
pos[i] = ind;
if(dp[i] > rez)
posmax = i, rez = dp[i];
}
while(posmax!=-1)
{
ans.push_back(posmax);
posmax = pos[posmax];
}
fout << rez << '\n';
reverse(ans.begin(),ans.end());
for(auto a : ans)
fout << v[a] << ' ';
return 0;
}