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

Envío 5411

Problema 0xdd - Ordenar un arreglo pequeño

  • Autor: slash
  • Fecha: 2021-12-15 16:59:34 UTC (Hace alrededor de 3 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.004 s 12 KBi
#2
Correcto
0.001 s 0 KBi
#3
Correcto
0.006 s 8 KBi
#4
Correcto
0.004 s 6 KBi
#5
Correcto
0.005 s 1 KBi
#6
Correcto
0.003 s 0 KBi
#7
Correcto
0.004 s 17 KBi
#8
Correcto
0.005 s 1 KBi
#9
Correcto
0.001 s 0 KBi
#10
Correcto
0.006 s 0 KBi
#11
Correcto
0.002 s 0 KBi
#12
Correcto
0.001 s 0 KBi
#13
Correcto
0.006 s 1 KBi
#14
Correcto
0.002 s 0 KBi
#15
Correcto
0.004 s 0 KBi
#16
Correcto
0.004 s 0 KBi
#17
Correcto
0.002 s 0 KBi
#18
Correcto
0.002 s 0 KBi
#19
Correcto
0.002 s 0 KBi
#20
Correcto
0.007 s 7 KBi
Puntos totales: 100 / 100

Código

//
// Created by danie on 11/14/2021.
//

#include <iostream>
#include <vector>

#define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL);

using namespace std;

void merge(vector<int> &list, int from, int half, int to){
    int firstHalf = half;
    int firstFrom = from;
    int mergedSize = (to - from) + 1;
    int mergedArray[mergedSize];
    for (int i = 0; i < mergedSize; i++){
        if((from < firstHalf + 1 && list[from] < list[half + 1]) || (half == to)){
            mergedArray[i] = list[from];
            from ++;
        } else{
            mergedArray[i] = list[half + 1];
            half ++;
        }
    }
    for (int i = 0; i < mergedSize; i++){
        list[firstFrom + i] = mergedArray[i];
    }
}

void __merge_sort(vector<int> &cards, int from, int to){
    if (from < to){
        int half = (to + from) / 2;
        __merge_sort(cards, from, half);
        __merge_sort(cards, half + 1, to);
        merge(cards, from, half, to);
    }
}

void merge_sort(vector<int> &cards){
    __merge_sort(cards, 0, cards.size() - 1);
}

//void test_merge_sort(){
//    vector<int> cards ={5,2,4,7,1,3,2,6};
//    merge_sort(cards);
//    for(int card : cards) cout<<card<<' ';
//}


int main(){
    fast_io
//    vector<int> cards = {0,1,43653,7437,3473,23,5,352,235};
//    radix_sort_non_negatives(cards);
    vector<int> cards;
    int n, in; cin>>n;
    for  (int i = 0; i < n; i++){
        cin>>in;
        cards.push_back(in);
    }
    merge_sort(cards);
    for(int num: cards) cout<<num<<' ';
}