Pagini recente » Cod sursa (job #1470417) | Cod sursa (job #2163782) | Cod sursa (job #486216) | Cod sursa (job #3213797) | Cod sursa (job #3194702)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#include <stack>
#include <set>
#include <map>
using namespace std;
ifstream f("scmax.in");
ofstream g("scmax.out");
const int nmax = 100005;
int n;
int v[nmax];
int best[nmax];
int pre[nmax];
int lung_max;
int start_poz;
int rez[nmax];
void scmax_slab()
{
for (int i = n; i >= 1; i--) {
best[i] = 1;
pre[i] = -1;
for (int j = i + 1; j <= n; j++) {
if (v[j] > v[i]) {
if (best[j] + 1 > best[i]) {
best[i] = best[j] + 1;
pre[i] = j;
}
}
}
if (best[i] > lung_max)
{
lung_max = best[i];
start_poz = i;
}
}
}
void varianta_1_slab()
{
scmax_slab();
g << lung_max << '\n';
bool last = 0;
while (!last)
{
if (pre[start_poz] == -1)
{
last = 1;
}
g << v[start_poz] << ' ';
start_poz = pre[start_poz];
}
}
void afis(int indice)
{
if (rez[indice] != 0 && rez[indice] != -1)
{
afis(rez[indice]);
}
g << v[indice] << ' ';
}
int cautare_binara(int a)
{
int st=0;
int dr=lung_max;
int mij = (st+dr)/2;
while (st <= dr)
{
if (v[best[mij]] < a && v[best[mij+1]] >= a)
{
return mij;
}
else if (v[best[mij+1]] < a)
{
st = mij + 1;
}
else {
dr = mij;
}
mij = (st + dr) / 2;
}
return lung_max;
}
void varianta_2()
{
/*
//Ciudat, nu merge cum ar trebui
auto a = lower_bound(v + 1, v + 1 + n, 12);
for (int i = 1; i <= n; i++)
{
g << v[i] << ' ';
}
g << '\n';
g << *a;
*/
for (int i = 1; i <= n; i++)
{
rez[i] = -1;
}
best[1] = 1;
lung_max = 1;
int curent = 1;
for (int i = 2; i <= n; i++)
{
int pozitieTemp = cautare_binara(v[i]);
best[pozitieTemp + 1] = i;
rez[i] = best[pozitieTemp];
pre[i] = pozitieTemp + 1;
if (lung_max < pozitieTemp + 1)
{
lung_max = pozitieTemp + 1;
start_poz = i;
}
}
g << lung_max << '\n';
afis(start_poz);
}
int main()
{
f >> n;
for (int i = 1; i <= n; i++)
{
f >> v[i];
}
//varianta_1_slab();
varianta_2();
}