Cod sursa(job #1052226)

Utilizator sebinechitasebi nechita sebinechita Data 10 decembrie 2013 22:33:04
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 2.01 kb
//nu ii frumos sa te uiti pe sursele altora:))...hotule....nu o sti face singur?:))
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
FILE *fin=fopen("scmax.in", "r");
FILE *fout=fopen("scmax.out", "w");
#define MAX 100010

const unsigned maxb=8192;
char buf[maxb];
unsigned ptr=maxb;

inline int getInt(){
    int nr=0,mul=1;
    while(buf[ptr]<'0'||'9'<buf[ptr]||buf[ptr]=='-'){
        if(buf[ptr]=='-'){
            mul=-1;
        }
        if(++ptr>=maxb)
            fread(buf,maxb,1,fin),ptr=0;
    }
    while('0'<=buf[ptr]&&buf[ptr]<='9'){
        nr=nr*10+buf[ptr]-'0';
        if(++ptr>=maxb)
            fread(buf,maxb,1,fin),ptr=0;
    }
    return nr*mul;
}

struct sebi{
    int x, y;
};
inline bool cmp1(sebi a, sebi b){
    if(a.x==b.x)
        return a.y>b.y;
    return a.x<b.x;
}
inline bool cmp2(sebi a, sebi b){
    return a.y<b.y;
}

int AIB[MAX], n, v[MAX], rez[MAX], cpy[MAX];
sebi a[MAX];

int cauta(int p)
{
    int m=0;
    while(p)
    {
        if(AIB[p]>m)
            m=AIB[p];
        p-=(p&(-p));
    }
    return m;
}

void introdu(int p, int val)
{
    while(p<=n)
    {
        AIB[p]=max(AIB[p], val);
        p+=(p&(-p));
    }
}

int main()
{
    n=getInt();
    int i;

    for(i=1;i<=n;i++)
    {
        a[i].x=getInt();
        cpy[i]=a[i].x;
        a[i].y=i;
    }
    //inc normalizare
    sort(a+1, a+n+1, cmp1);
    for(i=1;i<=n;i++)
    {
        a[i].x=i;
    }
    sort(a+1, a+n+1, cmp2);
    //sf normalizare
    int maxi=1;
    for(i=1;i<=n;i++)
    {
        v[i]=cauta(a[i].x)+1;
        introdu(a[i].x, v[i]);
        if(v[i]>v[maxi])
            maxi=i;
    }
    fprintf(fout, "%d\n", v[maxi]);
    int g=0;
    rez[++g]=cpy[maxi];
    for(i=maxi;i>=1;i--)
    {
        if(v[i]==v[maxi]-1 && a[i].x<a[maxi].x)
        {
            rez[++g]=cpy[i];
            maxi=i;
        }
    }
    for(i=g;i>=1;i--)
    {
        fprintf(fout, "%d ", rez[i]);
    }

}