Pagini recente » Cod sursa (job #849957) | Cod sursa (job #1754089) | Cod sursa (job #2153148) | Cod sursa (job #204815) | Cod sursa (job #2477583)
#include <fstream>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int l[100001], a[100001], n, lmax, imax;
void citeste () {
int i;
fin >> n;
for (i = 1; i <= n; i++)
fin >> a[i];
}
void dinamic() {
int ml, i, j;
l[n] = 1;
for (i = n - 1; i >= 1; i--){
ml = 0;
for (j = i + 1; j <= n; j++)
if (ml < l[j] and a[i] < a[j])
ml = l[j];
l[i] = ml + 1;
}
}
void maxim () {
int i;
lmax = l[1]; imax = 1;
for (i = 1; i <= n; i++)
if (l[i] > lmax){
lmax = l[i];
imax = i;
}
}
void scrie() {
int i, is, id;
fout << lmax << endl;
fout << a[imax] << ' ';
for (is = imax, id = is + 1; id <= n; id++)
if (a[is] < a[id] and l[id] == l[is]-1){
fout << a[id] << ' ';
is = id;
}
}
int main() {
citeste();
dinamic();
maxim();
scrie();
return 0;
}
/*
void citeste () {
int i;
fin >> n;
for (i = 1; i <= n; i++)
fin >> a[i];
}
void dinamic() {
int ml, i, j;
// ml - maximul lungimilor subsirurilor crescatoare care incep dupa pozitia "i" cu un element > a[i]
l[n] = 1;
//fout << l[n] << ' ';
for (i = n - 1; i>=1; i--) {
ml = 0;
for (j = i + 1; j <= n; j++) // pentru fiecare element din dreapta elementului curent
if (ml < l[j] and a[i] < a[j])
ml = l[j]; // Actualizam maximul lungimilor subsirurilor care incep dupa elementul curent.
l[i] = ml + 1; // lungimea celui mai lung subsir care incepe pe locul i
//fout << l[i] << ' ';
}
}
void maxim () {
int i;
lmax = l[1]; imax = 1;
for (i = 1; i <= n; i++)
if (l[i] > lmax) {
lmax = l[i]; imax = i;
}
}
void scrie() {
int i, is, id;
//fout << endl;
fout << lmax << endl; // Afisam lungimea maxima.
fout << a[imax] << ' '; // primul element din subsir
for (is = imax, id = is + 1; id <= n; id++) // indici pentru elementele unei perechi: stanga, dreapta
if (a[is] < a[id] and l[id] == l[is] - 1) {
fout << a[id] << ' '; // Scriem elementul.
is = id;
}
}
// 75 5 16 15 69 40 16 19 48 43 33 79 88 90 43
// d
// s
*/