Cod sursa(job #1570994)

Utilizator antanaAntonia Boca antana Data 16 ianuarie 2016 23:05:14
Problema Subsir 2 Scor 45
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <cstdio>
#define MAX 5000
using namespace std;
int v[MAX+1], best[MAX+1], last[MAX+1], nr, poz[MAX+1];
int caut(int x)
{
    int st=0, dr=nr, mij;
    while(st<=dr)
    {
        mij=(st+dr)/2;
        if(v[poz[mij]]<x&&v[poz[mij+1]]>=x) return mij;
        if(x>v[poz[mij]]) st=mij+1;
        else dr=mij-1;
    }
    return nr;
}
void afisare(int poz)
{
    if(last[poz]!=0)
        afisare(last[poz]);
    printf("%d ", poz);
}
int main()
{
    freopen("subsir2.in", "r",stdin);
    freopen("subsir2.out", "w", stdout);
    int i, max, n, p;
    scanf("%d", &n);
    for(i=1;i<=n;i++)
        scanf("%d", &v[i]);
    nr=poz[1]=best[1]=1;
    for(i=2;i<=n;i++)
    {
        p=caut(v[i]);
        best[i]=p+1;
        last[i]=poz[p];
        poz[p+1]=i;
        if(nr<p+1)
            nr=p+1;
    }
    max=0;
    int pozmax;
    for(i=n;i>=1;i--)
        if(max<best[i])
        {
            max=best[i];
            pozmax=i;
        }
    printf("%d\n", max);
    afisare(pozmax);
    return 0;
}