Pagini recente » Cod sursa (job #664900) | Cod sursa (job #634500) | Cod sursa (job #1100232) | Cod sursa (job #1231799) | Cod sursa (job #1767381)
#include <fstream>
#include <vector>
using namespace std;
const int kLmax = 100001;
int GasestePozitie(int x, const vector<int> &num, const vector<int> &poz, int start)
{
while (start > 0) {
if (num[poz[start]] < x)
return poz[start];
--start;
}
return 0;
}
void Afiseaza(int poz, const vector<int> &pred, const vector<int> &val, ofstream &f)
{
if (pred[poz] > 0)
Afiseaza(pred[poz], pred, val, f);
f << val[poz] << " ";
}
int main()
{
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int n;
fin >> n;
vector<int> numere(n + 1);
vector<int> lungime_maxima(n + 1, 0);
vector<int> predecesori(n + 1, 0);
vector<int> pozitie_lungime(kLmax, 0);
int lmax = 0, sfarsit_sir = 0;
numere[0] = (1 << 30);
for (int i = 1; i <= n; ++i) {
fin >> numere[i];
int x = numere[i];
int poz = GasestePozitie(x, numere, pozitie_lungime, lmax);
lungime_maxima[i] = lungime_maxima[poz] + 1;
predecesori[i] = poz;
if (x < numere[pozitie_lungime[lungime_maxima[i]]])
pozitie_lungime[lungime_maxima[i]] = i;
if (lungime_maxima[i] > lmax) {
lmax = lungime_maxima[i];
sfarsit_sir = i;
}
}
fout << lmax << "\n";
Afiseaza(sfarsit_sir, predecesori, numere, fout);
return 0;
}