Cod sursa(job #1526490)

Utilizator Vele_GeorgeVele George Vele_George Data 16 noiembrie 2015 19:45:50
Problema Subsir crescator maximal Scor 35
Compilator cpp Status done
Runda Arhiva educationala Marime 3.08 kb
#include <iostream>
#include <fstream>
#include <vector>
#define nmax 100003
#define inf 2000000005
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;
}

/*#include <iostream>
#include <fstream>
#define inf 2000000005
#define nmax 1005
using namespace std;

ifstream f("scmax.in");
ofstream g("scmax.out");

long long V[nmax];
int prev[nmax], B[nmax], mx =1, iBest;

int caut_binara(int lo, int hi, long long x)
{
    if (V[B[hi-1]]< x) return hi;
    if (V[B[lo+1]]> x) return lo+1;
    while (hi-lo!=1)
    {
        int mid = (hi+lo)/2;
        if (V[B[mid]] >= x) hi=mid;
                       else lo=mid;
    }
    return hi;
} //pozitia pe care trebuie pus V[i]

void recreare(int p)
{
    if (p==0) return;
    recreare(prev[p]);
    g << V[p] << " ";
    return;
}
int main()
{
    int n;
    f >> n;
    for(int i=1; i<=n; i++)
    {
        f >> V[i];
    }

    B[0]=0; B[1]=1; prev[1]=0;

    for(int i=2; i<=n; i++)
    {
        iBest = caut_binara(0, mx+1, V[i]);
        prev[i] = B[iBest-1];
        B[iBest]=i;
        if (iBest > mx) mx =iBest;
    }
    g << mx << "\n";
    recreare(B[mx]);

    return 0;
}
*/