Pagini recente » Cod sursa (job #2578401) | Cod sursa (job #2472244) | Cod sursa (job #1907617) | Cod sursa (job #89488) | Cod sursa (job #1229236)
#include <iostream>
#include <fstream>
#include <stack>
#define Nmax 100001
using namespace std;
int N, vec[Nmax], best[Nmax], T[Nmax], sol, sol_pos;
void read()
{
ifstream f("scmax.in");
f>>N;
for(int i=1;i<=N;++i)
f>>vec[i];
f.close();
}
void dinamica()
{
//best[i] the longest sequence that starts with vec[i]
fill(best, best + N + 1, 1);
fill(T, T + N + 1, 0);
sol = -1;
for(int i=1;i<=N;++i)
{
for(int j=1;j<i;++j)
{
if(vec[j] < vec[i])
{
if(best[j] + 1 > best[i])
{
best[i] = best[j] + 1;
T[i] = j;
}
}
}
if(best[i] > sol)
{
sol = best[i];
sol_pos = i;
}
}
}
void write()
{
ofstream g("scmax.out");
g<<sol<<"\n";
stack<int> Q;
while(sol_pos != 0)
{
Q.push(vec[sol_pos]);
sol_pos = T[sol_pos];
}
while(!Q.empty())
{
g<<Q.top()<<" ";
Q.pop();
}
g.close();
}
int main()
{
read();
dinamica();
write();
return 0;
}