Cod sursa(job #2550516)

Utilizator tifui.alexandruTifui Ioan Alexandru tifui.alexandru Data 18 februarie 2020 20:35:49
Problema Sortare prin comparare Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.73 kb
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pii pair <int,int>
#define x first
#define y second
#define mp make_pair

using namespace std;

const int Nmax = 500005;
int v[Nmax];

struct node{

	int val;
	node* next;
	node* down;
};
node* layer[25];
int top_layer = 0;

void insert(int x){

	stack < node* > stk;
	node* curr_node = layer[top_layer];

	int curr_layer = top_layer;
	while(curr_layer){

		while(curr_node -> next != nullptr and curr_node -> next -> val < x)
			curr_node = curr_node -> next;
		stk.push(curr_node);
		curr_node = curr_node -> down;
		--curr_layer;
	}

	while(curr_node -> next != nullptr and curr_node -> next -> val < x)
			curr_node = curr_node -> next;

	node* p = new node;
	p -> val = x;
	p -> down = nullptr;
	p -> next = curr_node -> next;
	curr_node -> next = p;

	int up = rand() % 20;
	node *q;
	node *w;

	while(up--){

		++curr_layer;
		q = layer[curr_layer];
		if(!stk.empty()){

			q = stk.top();
			stk.pop();
		}

		w = new node;
		w -> val = x;
		w -> down = p;
		w -> next = q -> next;
		q -> next = w;
		p = w;
	}

	/*for(p = layer[0]; p; p = p -> next)
		cerr << p -> val << ' ';
	cerr << '\n';*/
}

void skiplistsort(int n){

	for(int i = 0; i < 25; ++i){

		layer[i] = new node;
		layer[i] -> next = layer[i] -> down = nullptr;
		layer[i] -> val = INT_MIN;
	}
	for(int i = 1; i <= n; ++i)
		insert(v[i]);
	int k = -1;
	for(node* p = layer[0]; p != nullptr; p = p -> next)
		v[++k] = p -> val;
}

int main(){

	freopen("algsort.in", "r", stdin);
	freopen("algsort.out", "w", stdout);

	srand(time(nullptr));
    int n;
    cin >> n;
    for(int i = 1; i <= n; ++i)
    	cin >> v[i];
    
    skiplistsort(n);

    for(int i = 1; i <= n; ++i)
    	cout << v[i] << ' ';
    
    return 0;
}