Cod sursa(job #2682748)

Utilizator cristi_macoveiMacovei Cristian cristi_macovei Data 9 decembrie 2020 15:29:51
Problema Secv Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <iostream>
#include <fstream>
#include <algorithm>

const int NMAX = 5e3;

int n;

int a[1 + NMAX];

std::pair<int, int> aux[1 + NMAX];

int last[1 + NMAX];

void read() {
  std::ifstream in("secv.in");

  in >> n;

  for (int i = 1; i <= n; ++i) {
    in >> a[i];
    aux[i] = {a[i], i};
  }
}

int main() {
  std::ofstream out("secv.out");

  read();

  std::sort(aux + 1, aux + n + 1);

  int hash = 1;
  a[aux[1].second] = hash;

  for (int i = 2; i <= n; ++i) {
    if (aux[i].first > aux[i - 1].first)
      ++hash;

    a[aux[i].second] = hash;
  }

  if (hash == 1) {
    out << "1\n";
    return 0;
  }

  int mx_len = -1;

  for (int i = 1; i <= n; ++i) {
    if (last[a[i] - 1] == 0) {
      if (a[i] == 1)
        last[1] = i;
      continue;
    }

    last[a[i]] = last[a[i] - 1];
    if (a[i] == hash)
      mx_len = std::max(mx_len, i - last[a[i]] + 1);
  }

  out << mx_len << '\n';
  return 0;
}