Pagini recente » Cod sursa (job #1852038) | Cod sursa (job #541850) | Cod sursa (job #346376) | Cod sursa (job #3250491) | Cod sursa (job #2062983)
#include <iostream>
#include <fstream>
using namespace std;
int main(){
fstream f("fisier.in",ios::in);
fstream g("scmax.out",ios::out);
int n;
f >> n;
int best[n], last[n], v[n];
best[0] = 1;
last[0] = -1;
int i = 0, j, poz, maxim, rezultat_lungime = 1, rezultat_poz = 0;
while(f >> v[i])
++i;
for(i = 1;i < n; ++i){
maxim = -1;
for(j = 0;j < i; ++j)
if(maxim < best[j] && v[j] < v[i]){
maxim = best[j];
poz = j;
}
if(maxim == -1){
best[i] = 1;
last[i] = -1;
}else{
best[i] = maxim + 1;
last[i] = poz;
if(best[i] > rezultat_lungime){
rezultat_lungime = best[i];
rezultat_poz = i;
}
}
}
int counter = 0;
while(rezultat_poz != -1){
best[counter] = v[rezultat_poz];
++counter;
rezultat_poz = last[rezultat_poz];
}
g << rezultat_lungime << "\n";
for(int i = rezultat_lungime - 1;i >= 0; --i)
g << best[i] << " ";
}