Cod sursa(job #1437986)

Utilizator DenisacheDenis Ehorovici Denisache Data 18 mai 2015 21:50:49
Problema Subsir crescator maximal Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.37 kb
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <list>
#include <queue>
#include <set>
#include <unordered_map>
//#include <conio.h>
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define newl printf("\n")
#define NMAX 100005
#define nst (nod<<1)
#define ndr (nst+1)
#define vi vector<int>
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
int n,v[NMAX],scmax[NMAX],TT[NMAX];
int lSol,pozSol,currentL;
const int INF = (1<<30);
int searchPos(int x)
{
    int L=1,R=currentL,M;
    while (L<=R)
    {
        M=(L+R)>>1;
        if (v[scmax[M]]<x && v[scmax[M+1]]>=x)
            return M+1;
        else if (v[scmax[M]]<=x) L=M+1;
        else R=M-1;
    }
    return 1;
}
void printSol(int poz)
{
    if (TT[poz]) printSol(TT[poz]);
    printf("%d ",v[poz]);
}
int main()
{
    freopen("scmax.in","r",stdin);
    freopen("scmax.out","w",stdout);
    scanf("%d",&n);
    for (int i=1;i<=n;i++) scanf("%d",&v[i]);
    lSol=pozSol=scmax[1]=currentL=1;
    v[0]=INF;
    for (int i=2;i<=n;i++)
    {
        int pos=searchPos(v[i]);
        if (pos>lSol) lSol=pos,pozSol=i;
        scmax[pos]=i;
        TT[i]=scmax[pos-1];
        currentL=pos;
    }
    printf("%d\n",lSol);
    printSol(pozSol);
    return 0;
}