Cod sursa(job #1070828)

Utilizator crisbodnarCristian Bodnar crisbodnar Data 2 ianuarie 2014 10:25:49
Problema Subsir crescator maximal Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.47 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <memory.h>

using namespace std;

ifstream fin("euro2.in");
ofstream fout("euro2.out");

const int Nmax = 10010;

short N, top = 1, best1[Nmax], best2[Nmax], AIB[Nmax];
double V[Nmax], A[Nmax];

void Update(int poz, int i, short best[])
{
    while(poz <= top)
    {
        if(best[i] > best[AIB[poz]])
            AIB[poz] = i;
        poz += (poz&-poz);
    }
}

int Query(int poz, short best[])
{
    int max = 0;
    while(poz)
    {
        if(best[max] < best[AIB[poz]])
            max = AIB[poz];
        poz -= (poz&-poz);
    }
    return max;
}

void Scmax(short best[])
{
    memset(AIB, 0, top + 1);
    for(int i=1; i <= N; i++)
    {
        int poz = lower_bound(A + 1, A + top + 1, V[i]) - A;
        best[i] = best[Query(poz - 1, best)] + 1;
        Update(poz, i, best);
    }
}

int main()
{
    fin>>N;
    for(int i=1; i <= N; i++)
    {
        fin>>V[i];

        A[i] = V[i];
    }

    sort(A + 1, A + N + 1);
    for(int i=2; i <= N; i++)
        if(A[top] != A[i])
            A[++top] = A[i];

    Scmax(best1);
    for(int i=1; i*2<=N; i++)
        swap(V[i], V[N-i+1]);
    Scmax(best2);

    int sol = 0;
    for(int i=1; i<=N; i++)
        if(sol < best1[i] + best2[N - i + 1] - 1 && best1[i] >= 2 && best2[N - i + 1] >= 2)
            sol = best1[i] + best2[N - i + 1] - 1;
    fout<<sol;
    return 0;
}