Cod sursa(job #2097829)

Utilizator tziplea_stefanTiplea Stefan tziplea_stefan Data 1 ianuarie 2018 18:19:35
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.63 kb
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <vector>
#define VAL 100005
#define F first
#define S second

using namespace std;

int N, M, i, j, v[VAL], poz, aib[VAL];
int fat[VAL], dp[VAL], cop[VAL];
pair <int, int> nr[VAL];
vector <int> SOL;

char buff[VAL];
int pos=0;

int citire()
{
    int X=0;
    while (buff[pos]>'9' || buff[pos]<'0')
      if (++pos==VAL)
        fread(buff, 1, VAL, stdin), pos=0;
    while ('0'<=buff[pos] && buff[pos]<='9')
    {
        X=X*10+buff[pos]-'0';
        if (++pos==VAL)
          fread(buff, 1, VAL, stdin), pos=0;
    }
    return X;
}

int zero(int X)
{
    return X & (-X);
}

void Update(int X, int poz)
{
    while (X<=N)
    {
        if (dp[aib[X]]<dp[poz])
          aib[X]=poz;
        X+=zero(X);
    }
}

int Query(int X)
{
    int mx=0;
    while (X>0)
    {
        if (dp[aib[X]]>dp[mx])
          mx=aib[X];
        X-=zero(X);
    }
    return mx;
}

int main()
{
    freopen("scmax.in", "r", stdin);
    freopen("scmax.out", "w", stdout);
    N=citire();
    for (i=1; i<=N; i++)
    {
        v[i]=citire();
        nr[i].F=cop[i]=v[i];
        nr[i].S=i;
    }
    sort(nr+1, nr+N+1);
    for (i=1; i<=N; i++)
    {
        if (nr[i].F!=nr[i-1].F)
          M++;
        v[nr[i].S]=M;
    }
    M=0;
    for (i=1; i<=N; i++)
    {
        fat[i]=Query(v[i]-1);
        dp[i]=dp[fat[i]]+1;
        if (M<dp[i])
        {
            M=dp[i];
            poz=i;
        }
        Update(v[i], i);
    }
    printf("%d\n", M);
    while (poz!=0)
    {
        SOL.push_back(cop[poz]);
        poz=fat[poz];
    }
    for (i=M-1; i>=0; i--)
      printf("%d ", SOL[i]);
    return 0;
}