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

Envío 2340

Problema 0xde - Ordenar un arreglo grande

  • Autor: andreslel
  • Fecha: 2020-12-14 01:07:20 UTC (Hace alrededor de 4 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.005 s 47 KBi
#2
Correcto
0.006 s 1 KBi
#3
Correcto
0.008 s 2 KBi
#4
Correcto
0.005 s 1 KBi
#5
Correcto
0.006 s 2 KBi
#6
Correcto
0.004 s 2 KBi
#7
Correcto
0.068 s 2 KBi
#8
Correcto
0.13 s 17 KBi
#9
Correcto
0.113 s 19 KBi
#10
Correcto
0.119 s 2 KBi
#11
Correcto
0.081 s 8 KBi
#12
Correcto
0.058 s 10 KBi
#13
Correcto
0.161 s 3 KBi
#14
Correcto
0.183 s 3 KBi
#15
Correcto
0.147 s 3 KBi
#16
Correcto
0.144 s 3 KBi
#17
Correcto
0.133 s 3 KBi
#18
Correcto
0.164 s 3 KBi
#19
Correcto
0.114 s 2 KBi
#20
Correcto
0.09 s 1 KBi
#21
Correcto
0.121 s 3 KBi
#22
Correcto
0.167 s 2 KBi
#23
Correcto
0.138 s 3 KBi
#24
Correcto
0.139 s 2 KBi
#25
Correcto
0.126 s 2 KBi
#26
Correcto
0.212 s 2 KBi
#27
Correcto
0.14 s 2 KBi
Puntos totales: 100 / 100

Código

#include<bits/stdc++.h>
using namespace std;

int left(int val){
    return val<<1;
}

int right(int val){
    return (val<<1)+1;
}

int dad(int val){ return val>>1;}

void down(int from,vector<int> &arr){
    int next = from;
    int sz = arr.size();
    if(left(from) < sz && arr[left(from)] < arr[next])
        next = left(from);
    if(right(from) < sz && arr[right(from)] < arr[next])
        next = right(from);
    swap(arr[next],arr[from]);
    if(from != next)
        down(next,arr);
}

void up(int from, vector<int> &arr){
    while(from != 1 && arr[dad(from)] > arr[from]){
        swap(arr[dad(from)],arr[from]);
        from = dad(from);
    }
}

int top(vector<int> &arr){
    return arr[1];
}

void add(vector<int> &arr, int val){
    arr.push_back(val);
    up(arr.size()-1,arr);
}

void pop(vector<int> &arr){
    swap(arr[1],arr.back());
    arr.pop_back();
    if(arr.size() > 1)
        down(1,arr);
}

int main(){
    int n;
    scanf(" %d",&n);
    vector<int> nums = {0};
    for(int i = 0; i < n;++i){
        int val;
        scanf(" %d",&val);
        add(nums,val);
    }
    int first = 0;
    while(nums.size() > 1){
        if(first) printf(" ");
        printf("%d",top(nums));
        pop(nums);
        first = 1;
    }
    puts("");
    return 0;
}