Cod sursa(job #2053695)

Utilizator stefanst77Luca Stefan Ioan stefanst77 Data 1 noiembrie 2017 09:28:43
Problema Subsir crescator maximal Scor 65
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 kb
#include <bits/stdc++.h>
#define nrmax 100007

using namespace std;

int n, a[nrmax];
int dp[nrmax], m;

void Citire()
{
    int i;
    ifstream fin ("scmax.in");
    fin >> n;
    for (i=1; i<=n; i++)
        fin >> a[i];
    fin.close();
}

void CautBin(int x)
{
    if (x>dp[m])
    {
        dp[++m]=x;
        return;
    }
    if (x<dp[1])
    {
        dp[1]=x;
        return;
    }
    int st, dr;
    st=1;
    dr=m;
    int mij;
    int poz=1;
    while (st<=dr)
    {
        mij=(st+dr)/2;
        if (dp[mij]>=x)
        {
            poz=mij;
            dr=mij-1;
        }
        else /// dp[m]<x
            st=mij+1;
    }
    dp[poz]=x;
}

void Rezolvare()
{
    int i;
    dp[1]=a[1];
    m=1;
    for (i=2; i<=n; i++)
        CautBin(a[i]);

    ofstream fout ("scmax.out");
    fout << m << "\n";
    for (i=1; i<=m; i++)
        fout << dp[i] << " ";
    fout << "\n";
    fout.close();
}

int main()
{
    Citire();
    Rezolvare();
    return 0;
}