Cod sursa(job #1778187)

Utilizator amaliarebAmalia Rebegea amaliareb Data 13 octombrie 2016 16:40:08
Problema Subsir crescator maximal Scor 65
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
#include <iostream>
#include <fstream>

using namespace std;
ifstream f("scmax.in");
ofstream g("scmax.out");
unsigned int st,dr,m,n,i,j,maxbest,v[100001],best[100001],previ[100001],current[100001];

int main()
{
    f>>n;
    for(i=1;i<=n;i++) f>>v[i];
    maxbest=0; best[0]=0;
    for(i=1;i<=n;i++) best[i]=1<<31;
    for(i=1;i<=n;i++)
    {
        if(v[i]<best[maxbest])
        {
            st=0; dr=maxbest;
            while(st<dr)
            {
                m=(st+dr)/2;
                if(best[m]<v[i]) st=m+1;
                else dr=m-1;
            }
            while(best[st]>=v[i]) st--;
            if(best[st+1]>v[i])
            {
                best[st+1]=v[i];
                current[st+1]=i;
                previ[st+1]=current[st];
            }
            cout<<1<<' '<<current[st+1]<<' '<<previ[st+1]<<'\n';
        }
        else if(v[i]>best[maxbest])
        {
            best[++maxbest]=v[i];
            current[maxbest]=i;
            previ[maxbest]=current[maxbest-1];
            cout<<2<<' '<<current[maxbest]<<' '<<previ[maxbest]<<'\n';
        }
    }
    g<<maxbest<<'\n';
    for(i=1;i<=maxbest;i++) g<<v[current[i]]<<' ';
    return 0;
}