Problem p02268 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

#define N_MAX 100001
#define Q_MAX 50000
int count = 0;
int n, q;
int s_array[N_MAX], t_array[Q_MAX];
void down_heap(int array[], int, int);

void printArray(int array[], int n) {
  for (int i = 1; i <= n; i++) {
    std::cout << array[i] << ' ';
  }
  std::cout << '\\n';
}

void sort(int array[], int n) {
  int length = n;
  for (int i = length / 2; i > 0; i--) {
    down_heap(array, length, i);
  }
  while (length != 1) {
    array[0] = array[1];
    array[1] = array[length];
    array[length] = array[0];
    length--;
    for (int i = length / 2; i > 0; i--) {
      down_heap(array, length, i);
    }
  }
}

void down_heap(int array[], int length, int k) {
  int child;

  while (k <= length / 2) {

    child = 2 * k;
    if (child + 1 <= length && array[child] < array[child + 1]) {
      child += 1;
    }

    if (array[k] < array[child]) {
      array[0] = array[k];
      array[k] = array[child];
      array[child] = array[0];
      k = child;

    } else {
      break;
    }
  }
}
void search(int k, int left, int right) {
  while (left <= right) {
    int center = (left + right) / 2;
    if (s_array[center] == k) {
      count++;
      return;
    } else if (s_array[center] < k) {
      left = center + 1;
    } else if (s_array[center] > k) {
      right = center - 1;
    }
  }
}

int main() {
  int max = 0;

  std::cin >> n;
  for (int i = 1; i <= n; i++) {
    std::cin >> s_array[i];
    if (s_array[i] < max) {
      sort(s_array, i);
    }
  }
  // printArray(s_array,n);

  // sort(s_array,n);

  // printArray(s_array,n);

  std::cin >> q;
  for (int i = 0; i < q; i++) {
    std::cin >> t_array[i];
    search(t_array[i], 1, n);
  }

  std::cout << count << '\\n';

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>

#define N_MAX 100001
#define Q_MAX 50000
int count = 0;
int n, q;
int s_array[N_MAX], t_array[Q_MAX];
void down_heap(int array[], int, int);

void printArray(int array[], int n) {
  for (int i = 1; i <= n; i++) {
    std::cout << array[i] << ' ';
  }
  std::cout << '\\n';
}

void sort(int array[], int n) {
  int length = n;
  for (int i = length / 2; i > 0; i--) {
    down_heap(array, length, i);
  }
  while (length != 1) {
    array[0] = array[1];
    array[1] = array[length];
    array[length] = array[0];
    length--;
    for (int i = length / 2; i > 0; i--) {
      down_heap(array, length, i);
    }
  }
}

void down_heap(int array[], int length, int k) {
  int child;

  while (k <= length / 2) {

    child = 2 * k;
    if (child + 1 <= length && array[child] < array[child + 1]) {
      child += 1;
    }

    if (array[k] < array[child]) {
      array[0] = array[k];
      array[k] = array[child];
      array[child] = array[0];
      k = child;

    } else {
      break;
    }
  }
}
void search(int k, int left, int right) {
  while (left <= right) {
    int center = (left + right) / 2;
    if (s_array[center] == k) {
      count++;
      return;
    } else if (s_array[center] < k) {
      left = center + 1;
    } else if (s_array[center] > k) {
      right = center - 1;
    }
  }
}

int main() {

  std::cin >> n;
  for (int i = 1; i <= n; i++) {
    std::cin >> s_array[i];
  }
  // printArray(s_array,n);

  sort(s_array, n);

  // printArray(s_array,n);

  std::cin >> q;
  for (int i = 0; i < q; i++) {
    std::cin >> t_array[i];
    search(t_array[i], 1, n);
  }

  std::cout << count << '\\n';

  return 0;
}