Cod sursa(job #2324089)

Utilizator andreisavulescuSavulescu Andrei andreisavulescu Data 20 ianuarie 2019 11:37:14
Problema Subsir 2 Scor 45
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <fstream>

using namespace std;

ifstream f("subsir2.in");
ofstream g("subsir2.out");
int nrs;

struct pozitie
{
    int val,poz;
};

pozitie vf[5001], z;

int CautBin(pozitie x)
{
    int p = 1, u = nrs, poz = -1;
    while(p <= u)
    {
        int m = (p + u) / 2;
        if(x.val < vf[m].val)
        {
            poz = m;
            u = m - 1;
        }
        else
            p = m + 1;
    }
    return poz;
}

int main()
{
    int n;
    f >> n >> z.val;
    z.poz = 1;
    nrs = 1;
    vf[nrs] = z;
    for(int i = 2; i <= n; i++)
    {
        f >> z.val;
        z.poz = i;
        if(z.val >= vf[nrs].val)
            vf[++nrs] =z;
        else
        {
            int j = CautBin(z);
            vf[j] = z;
        }
    }
    g << nrs << '\n';
    for(int i = 1; i <= nrs; i++)
        g << vf[i].poz << ' ';
    return 0;
}