Pagini recente » Cod sursa (job #1548327) | Cod sursa (job #3269670) | Cod sursa (job #245289) | Cod sursa (job #19588) | Cod sursa (job #3196665)
#include <fstream>
using namespace std;
const int N = 1e5;
ifstream in("scmax.in");
ofstream out("scmax.out");
int v[N], l_max[N], val_min[N+1];
void refac_subsir(int poz, int val, int lungime)
{
if (lungime == 0)
{
return;
}
if (v[poz] <= val && l_max[poz] == lungime)
{
refac_subsir(poz - 1, v[poz] - 1, lungime - 1);
out << v[poz] << " ";
}
else
{
refac_subsir(poz - 1, val, lungime);
}
}
int caut_binar(int x[], int n, int val)
{
///cauta binar cel mai mare indice rez cu propr. ca x[rez] < val
int st = 1, dr = n, rez = 0;
while (st <= dr)
{
int m = (st + dr) / 2;
if (x[m] < val)
{
rez = m;
st = m + 1;
}
else
{
dr = m - 1;
}
}
return rez;
}
int main()
{
int n, poz_l_max = 0, n_val_min = 0;
in >> n;
for (int i = 0; i < n; i++)
{
in >> v[i];
int j0 = caut_binar(val_min, n_val_min, v[i]);
l_max[i] = j0 + 1;
if (j0 == n_val_min)
{
n_val_min++;
}
val_min[j0+1] = v[i];
if (l_max[i] > l_max[poz_l_max])
{
poz_l_max = i;
}
}
out << l_max[poz_l_max] << "\n";
refac_subsir(poz_l_max, v[poz_l_max], l_max[poz_l_max]);
in.close();
out.close();
return 0;
}