提交记录 16401


用户 题目 状态 得分 用时 内存 语言 代码长度
d0j1a_1701 1001. 测测你的排序 Time Limit Exceeded 0 5 s 390660 KB C++ 1.99 KB
提交时间 评测时间
2021-07-29 10:58:31 2021-07-29 10:58:40
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
int n, maxdeep;
priority_queue<int, vector<int>, greater<int> > pq;
void insertSort(unsigned *arr,int l, int r) {
    for(int i = l + 1; i <= r; i++) {
        int j = i;
        while(j > l) {
            if(arr[j] < arr[j - 1]) {
                swap(arr[j], arr[j - 1]);
                j--;
            } else {
                break;
            }
        }
    }
}
int getMaxDeep(int x) {
    int k;
    for(k = 0; x > 1; x >>= 1)  k++;
    return k * 2;
}
int getPivot(unsigned *arr,int l, int r) {
    int ix = l, iy = r - 1, iz = (l + r - 1) >> 1;
    int x = arr[ix], y = arr[iy], z = arr[iz];
    if(x > y) {
        if(x > z) {
            if(y > z) {
                return iy;
            } else {
                return iz;
            }
        } else {
            return ix;
        }
    } else {
        if(x > z) {
            return ix;
        } else {
            if(y > z) {
                return iz;
            } else {
                return iy;
            }
        }
    }
}
void heapSort(unsigned *arr,int l, int r) {
    for(int i = l; i <= r; i++) pq.push(arr[i]);
    for(int i = l; i <= r; i++) {
        arr[i] = pq.top();
        pq.pop();
    }
}
void introSortLoop(unsigned *arr,int l, int r, int deep) {
    if(l >= r)  return;
    if(r - l <= 16) {
        return;
    }
    if(deep >= maxdeep) {
        heapSort(arr,l, r);
        return;
    }
    int i = l, j = r;
    swap(arr[l], arr[getPivot(arr,l, r)]);
    int pivot = arr[l];
    while(i < j) {
        while(i < j && arr[j] >= pivot) j--;
        while(i < j && arr[i] <= pivot) i++;
        if(i < j)
            swap(arr[i], arr[j]);
    }
    swap(arr[l], arr[i]);
    introSortLoop(arr,l, i - 1, deep + 1);
    introSortLoop(arr,i + 1, r, deep + 1);
}
void introSort(unsigned *arr,int l,int r) {
    introSortLoop(arr,l, r - 1, 0);
    insertSort(arr,l, r - 1);
}

void sort(unsigned *a, int n) {
        maxdeep = getMaxDeep(n);
	introSort(a,1,n);
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #15 s381 MB + 516 KBTime Limit ExceededScore: 0


Judge Duck Online | 评测鸭在线
Server Time: 2021-09-21 13:20:56 | Loaded in 0 ms | Server Status
个人娱乐项目,仅供学习交流使用