sin-titulo

Pasted Nov 1, 2026, 5:02 PM1 file123 lines

Scan to open on your phone

Point a camera at the QR code - it opens this exact paste, no app needed.

https://codepastes.com/zzgseyhqhh3vt83e
sin-titulo
#include <iostream>
#include <vector>
#include <string>

using namespace std;

struct Paciente {
    string codigo;
    string nombre;
    int urgencia;
    int ordenLlegada;

    // Sobrecarga para comparar prioridades (Min-Heap)
    // Retorna true si 'a' tiene MÁS prioridad que 'b'
    bool esMayorPrioridadQue(const Paciente& otro) const {
        if (urgencia != otro.urgencia) {
            return urgencia < otro.urgencia; // Menor número = Mayor urgencia
        }
        return ordenLlegada < otro.ordenLlegada; // Menor orden de llegada = Llevó antes
    }
};

class MinHeapClinica {
private:
    vector<Paciente> heap;
    int contadorLlegada;

    void siftUp(int i) {
        while (i > 0) {
            int padre = (i - 1) / 2;
            if (heap[i].esMayorPrioridadQue(heap[padre])) {
                swap(heap[i], heap[padre]);
                i = padre;
            } else {
                break;
            }
        }
    }

    void siftDown(int i) {
        int n = heap.size();
        while (2 * i + 1 < n) {
            int izquierdo = 2 * i + 1;
            int derecho = 2 * i + 2;
            int menor = i;

            if (heap[izquierdo].esMayorPrioridadQue(heap[menor])) {
                menor = izquierdo;
            }
            if (derecho < n && heap[derecho].esMayorPrioridadQue(heap[menor])) {
                menor = derecho;
            }

            if (menor != i) {
                swap(heap[i], heap[menor]);
                i = menor;
            } else {
                break;
            }
        }
    }

public:
    MinHeapClinica() : contadorLlegada(0) {}

    void insertar(string codigo, string nombre, int urgencia) {
        contadorLlegada++;
        Paciente p = {codigo, nombre, urgencia, contadorLlegada};
        heap.push_back(p);
        siftUp(heap.size() - 1);
        cout << "[+] Paciente " << codigo << " registrado correctamente.\n";
    }

    bool estaVacio() const {
        return heap.empty();
    }

    int cantidad() const {
        return heap.size();
    }

    Paciente consultarSiguiente() const {
        if (estaVacio()) {
            throw runtime_error("El heap esta vacio.");
        }
        return heap[0];
    }

    Paciente extraerAtender() {
        if (estaVacio()) {
            throw runtime_error("No hay pacientes en espera.");
        }
        Paciente raiz = heap[0];
        heap[0] = heap.back();
        heap.pop_back();
        if (!estaVacio()) {
            siftDown(0);
        }
        return raiz;
    }
};

int main() {
    MinHeapClinica clinica;

    // Datos de prueba solicitados: P01–Ana–3; P02–Luis–1; P03–Eva–2; P04–Raúl–1
    cout << "=== PRUEBA DE DATOS INICIALES CLINICA ===\n";
    clinica.insertar("P01", "Ana", 3);
    clinica.insertar("P02", "Luis", 1);
    clinica.insertar("P03", "Eva", 2);
    clinica.insertar("P04", "Raul", 1);

    cout << "\nOrden de atencion esperado segun enunciado: P02, P04, P03, P01\n";
    cout << "Orden de atencion real ejecutado:\n";
    while (!clinica.estaVacio()) {
        cout << "Cantidad en espera: " << clinica.cantidad() << " | ";
        Paciente p = clinica.extraerAtender();
        cout << "Atendiendo a -> Codigo: " << p.codigo << ", Nombre: " << p.nombre 
             << ", Urgencia: " << p.urgencia << endl;
    }

    return 0;
}

3,315 chars · codepastes.com