█████████ ████ ███░░░░░███ ░░███ ███ ░░░ ██████ ███████ ██████ ██████ ░███ ███░░███ ███░░███ ███░░███ ███░░███ ░███ ░███ ░███░███ ░███ ░███████ ░███ ░███ ░░███ ███░███ ░███░███ ░███ ░███░░░ ░███ ░███ ░░█████████ ░░██████ ░░████████░░██████ ░░██████ ░░░░░░░░░ ░░░░░░ ░░░░░░░░ ░░░░░░ ░░░░░░

Envío 2590

Problema 0x62 - Contar elementos mayores a X en un arreglo pequeño

  • Autor: endo27
  • Fecha: 2021-01-08 06:36:03 UTC (Hace más de 3 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.007 s 2 KBi
#2
Incorrecto
0.004 s 2 KBi
#3
Correcto
0.005 s 2 KBi
#4
Correcto
0.004 s 1 KBi
#5
Correcto
0.007 s 2 KBi
#6
Incorrecto
0.005 s 62 KBi
#7
Correcto
0.005 s 49 KBi
#8
Correcto
0.006 s 1 KBi
#9
Incorrecto
0.006 s 1 KBi
#10
Incorrecto
0.006 s 13 KBi
Puntos totales: 60 / 100

Código

#include <iostream>
#include <list>

using namespace std;

void quickSort(int* A, int izq, int der) {
    int piv = A[izq];
    int i = izq;
    int j = der;
    int aux;

    while (i < j) {
        while (A[i] <= piv && i < j) i++;
        while (A[j] > piv) j--;
        if (i < j) {
            aux = A[i];
            A[i] = A[j];
            A[j] = aux;
        }
    }

    A[izq] = A[j];
    A[j] = piv;
    if (izq < j - 1)
        quickSort(A, izq, j - 1);
    if (j + 1 < der)
        quickSort(A, j + 1, der);

}

int binarySearch(int arr[], int l, int r, int x)
{
    if (r >= l) {
        int mid = l + (r - l) / 2;

        if (arr[mid] == x)
            return mid;

        if (arr[mid] > x)
            return binarySearch(arr, l, mid - 1, x);

        return binarySearch(arr, mid + 1, r, x);
    }

    return -1;
}

int main()
{
    size_t arraySize;
    int element, querySize, x, pos;
    list<int> greaterElementsList;

    cin >> arraySize;

    int* elementsArray = new int[arraySize];

    for (size_t i = 0; i < arraySize; i++)
    {
        cin >> element;
        elementsArray[i] = element;
    }

    quickSort(elementsArray, 0, arraySize-1);

    cin >> querySize;

    for (size_t i = 0; i < querySize; i++)
    {
        cin >> x;

        if (x<elementsArray[0])
        {
            greaterElementsList.push_back(arraySize);
        }
        else if (x>=elementsArray[arraySize-1])
        {
            greaterElementsList.push_back(0);
        }
        else
        {
            pos = binarySearch(elementsArray, 0, arraySize - 1, x);
            while (elementsArray[pos] == elementsArray[pos + 1])
            {
                pos++;
            }
            greaterElementsList.push_back(arraySize-1 - pos);
        }
    }

    size_t greaterElementsSize = greaterElementsList.size();

    for (size_t i = 0; i < greaterElementsSize; i++)
    {
        cout << greaterElementsList.front() << endl;
        greaterElementsList.pop_front();
    }
}