Cod sursa(job #1526436)

Utilizator Vele_GeorgeVele George Vele_George Data 16 noiembrie 2015 11:44:32
Problema Subsir crescator maximal Scor 15
Compilator cpp Status done
Runda Arhiva educationala Marime 2.06 kb
#include <iostream>
#include <fstream>
#include <vector>
#define nmax 100003
#define inf 200000005
using namespace std;

int best;
long long V[4*nmax], M[4*nmax]; // val, max
int B[4*nmax]; // best


void afis(int i, int st, int dr)
{
    cout << "(" << st << "," << dr << ")" << V[i] <<" "<< B[i] << " " << M[i] << "\n";
    if (st == dr) return;
    else
    {
        int mid = (st+dr)/2;
       afis(2*i, st, mid); //st
       afis(2*i+1, mid+1, dr); //dr
    }
    return;
}
void update(int i, int st, int dr, int poz, long long val, int best)
{
    if (st == dr) {V[i] = val; M[i]=val; B[i]=best;}
    else
    {
        int mid = (st+dr)/2;
        if (poz <= mid) update(2*i, st, mid, poz, val, best); //st
                  else  update(2*i+1, mid+1, dr, poz, val, best); //dr
        M[i] = min(M[2*i], M[2*i+1]);
        if (B[2*i] > B[2*i+1]) {B[i]=B[2*i]; V[i]=V[2*i];}
        if (B[2*i] < B[2*i+1]) {B[i]=B[2*i+1]; V[i]=V[2*i+1];}
        if (B[2*i] == B[2*i+1]) {B[i]=B[2*i]; V[i]=min(V[2*i], V[2*i+1]);}
    }


    return;
}

void cauta(int i, int st, int dr, int a, int b, long long val)
{
    if (M[i] > val) return;
    if (a<=st && dr<=b) {
        if (V[i] == val) best = max(B[i], best);
        if (V[i] < val ) best = max(B[i]+1, best);
    }

    int mid = (st+dr)/2;
    if (a<=mid) cauta(2*i, st, mid, a, b, val);
    if (mid< b) cauta(2*i+1, mid+1, dr, a, b, val);

    return;
}

int main()
{
    int n, x;
    ifstream f("scmax.in");
    ofstream g("scmax.out");

    f >> n;
    //int prev[n+1];
    for(int i=1; i<=4*nmax; i++)
    {
        V[i]=inf;
        M[i]=inf;
        B[i]=0;
    }
    f >> x;
    best=1;
    update(1, 1, n, 1, x, 1);
    for(int i=2; i<=n; i++)
    {
        f >> x;
        best = 1;
        cauta(1, 1, n, 1, i-1, x);
        //cout << best <<" cand i=" << i << "pe val" << x <<"\n";
        update(1, 1, n, i, x, best);
    }

   // cout << V[1] <<" " <<B[1] <<"\n";

    //afis(1, 1, n);

    g << B[1];

    f.close();
    g.close();
    return 0;
}