提交记录 3592


用户 题目 状态 得分 用时 内存 语言 代码长度
ruanxingzhi 1001a. 测测你的排序2 Compile Error 0 0 ns 0 KB C++ 1.39 KB
提交时间 评测时间
2018-07-16 18:42:15 2020-07-31 21:19:49
#include <algorithm>
using namespace std;

#define DEBUG printf("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)

struct Heap                 //小根堆
{
    unsigned int a[10005];
    int tot;

    #define LE(x) ((x)<<1)
    #define RT(x) (((x)<<1)+1)
    #define DAD(x) ((x)>>1)

    Heap() {tot=0;}

    void modify(int x)
    {
        int mi;

        if(LE(x)>tot || x>tot) return;
        if(RT(x)>tot) mi=LE(x);
        
        else if(a[LE(x)]<a[RT(x)]) mi=LE(x);
        else mi=RT(x);

        if(a[x]>a[mi]) swap(a[x],a[mi]);
    }

    void ins(int x)
    {
        tot++;
        a[tot]=x;
        for(x=DAD(tot);x;x=DAD(x))
            modify(x);
    }

    int top()
    {
        return a[1];
    }

    void repair()
    {
        int mi,x;

        for(x=1;x<=tot;x=mi)
        {
            if(LE(x)>tot) break;
            if(RT(x)>tot) mi=LE(x);
            
            else if(a[LE(x)]<a[RT(x)]) mi=LE(x);
            else mi=RT(x);

            if(a[x]>a[mi]) swap(a[x],a[mi]);
            else break;
        }
    }

    void pop()
    {
        swap(a[1],a[tot]);
        tot--;

        repair();
    }

    void debug()
    {
        int i;
        printf("[%d] ",tot);
        for(i=1;i<=tot;i++) printf("%d ",a[i]);
        puts("");
    }
};

Heap s;

void sort(unsigned *a, int n) {
	int i;

    for(i=0;i<n;i++)
        s.ins(a[i]);
    for(i=0;i<n;i++)
        a[i]=s.top(),s.pop();
}

CompilationN/AN/ACompile ErrorScore: N/A


Judge Duck Online | 评测鸭在线
Server Time: 2026-04-18 07:26:11 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠