Cod sursa(job #2969218)

Utilizator Robilika2007Robert Badea Robilika2007 Data 22 ianuarie 2023 18:57:43
Problema Subsir crescator maximal Scor 55
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.08 kb
/*
#include <fstream>
#include <iostream>

using namespace std;

ifstream fin ("scmax.in");
ofstream fout ("scmax.out");

#define MAX_n 100000
int v[MAX_n], pred[MAX_n], s[MAX_n];

int cautabin (int st, int dr, int a) // 0 1 15
{
    int pas = (1 << 16), ac = st - 1;
    while(pas)
    {
        if(ac + pas <= dr)
        {
            //cout << pas << " ";
            if(v[s[ac + pas]] < a)
            {
                ac += pas;
            }
        }
        pas >>= 1;
    }
    //cout << ac;
    return ac;
}

void scrie(int poz)
{
    if(poz != 1)
    {
        scrie(pred[poz]);
        fout << v[poz];
    }
}

int main()
{
    int n, ans = 0;
    fin >> n;

    for(int i = 0; i < n; ++i)
    {
        fin >> v[i];

        int lung = cautabin(0, ans - 1, v[i]) + 1;
        s[lung] = i;

        s[lung] = i; // s[1] = 1

        if(lung > 0)
            pred[i] = s[lung - 1];
        else
            pred[i] = -1;

        ans = max(ans, lung + 1);
    }

    fout << ans << '\n';
    scrie(s[ans - 1]);
}
*/

#include <stdio.h>
#include <fstream>

#define MAX_N 100000

using namespace std;

int v[MAX_N], s[MAX_N], pred[MAX_N];

ifstream fin ("scmax.in");
ofstream fout ("scmax.out");

inline int max(int a, int b) {
  return a > b ? a : b;
}

int binarySearch(int searchValue, int left, int right) {
  int i, step;
  i = left - 1;
  step = 1 << 16;
  while (step) {
    if (i + step <= right && v[s[i + step]] < searchValue)
      i += step;
    step >>= 1;
  }
  return i;
}


void write(int pos) {
  if (pos != -1) {
    write(pred[pos]);
    fout << v[pos];
  }
}



int main()
{
    int n, ans = 0;
    fin >> n;

    for(int i = 0; i < n; ++i)
    {
        fin >> v[i];

        int lung = binarySearch(v[i], 0, ans - 1) + 1;
        s[lung] = i;

        s[lung] = i; // s[1] = 1

        if(lung > 0)
            pred[i] = s[lung - 1];
        else
            pred[i] = -1;

        ans = max(ans, lung + 1);
    }

    fout << ans << '\n';
    write(s[ans - 1]);
}