提交记录 39778


用户 题目 状态 得分 用时 内存 语言 代码长度
saffah_dsh_260814 1001b. 测测你的排序3 Accepted 100 1.272 s 1049676 KB C++ 2.00 KB
提交时间 评测时间
2026-08-16 19:41:03 2026-08-16 19:41:09
#include <string.h>
#include <stdint.h>
typedef unsigned int u32;
typedef unsigned char u8;

static u32 tmp[(1u<<27) + (1u<<21)] __attribute__((aligned(64)));
static u32 cnt[512];
static u32 rbase[512];
static u32 pbase[512];
static u32 off[512];
static u8 pos[512];
static u32 staging[512 * 16] __attribute__((aligned(64)));
static u32 scratch[524288] __attribute__((aligned(64)));
static u32 scratch2[524288] __attribute__((aligned(64)));
static u32 bc[256];

static void scatter_top(u32 *src, u32 *dst, int n) {
    int i, j;
    for (j = 0; j < 512; j++) cnt[j] = 0;
    for (i = 0; i < n; i++) cnt[src[i] >> 23]++;
    { u32 t = 0, p = 0; for (j = 0; j < 512; j++) { u32 c = cnt[j]; rbase[j] = t; pbase[j] = p; t += c; p = (p + c + 15) & ~15u; } }
    for (j = 0; j < 512; j++) { off[j] = pbase[j]; pos[j] = 0; }
    for (i = 0; i < n; i++) {
        u32 x = src[i]; u32 b = x >> 23; u32 p = pos[b];
        staging[b * 16 + p] = x; p++;
        if (p == 16) { u32 go = off[b]; __builtin_memcpy(dst+go, staging+b*16, 64); off[b] = go + 16; p = 0; }
        pos[b] = (u8)p;
    }
    for (j = 0; j < 512; j++) { u32 p = pos[j]; if (p > 0) memcpy(dst+off[j], staging+j*16, p*4); }
}

static void sort_bucket(u32 *src, u32 *dst, int m) {
    int i, j, shift;
    u32 *from = src, *to = scratch;
    int total = 23;
    for (shift = 0; shift < total; shift += 8) {
        int bits = total - shift; if (bits > 8) bits = 8;
        u32 mask = (1u << bits) - 1;
        for (j = 0; j < 256; j++) bc[j] = 0;
        for (i = 0; i < m; i++) bc[(from[i] >> shift) & mask]++;
        for (j = 1; j < 256; j++) bc[j] += bc[j-1];
        for (i = m - 1; i >= 0; i--) { u32 x = from[i]; to[--bc[(x >> shift) & mask]] = x; }
        u32 *t = from; from = to; to = t;
    }
    if (from != dst) __builtin_memcpy(dst, from, m * 4);
}

void sort(unsigned *aa, int n) {
    u32 *a = (u32*)aa;
    int j;
    scatter_top(a, tmp, n);
    for (j = 0; j < 512; j++) {
        int m = (int)cnt[j];
        if (m > 0) sort_bucket(tmp + pbase[j], a + rbase[j], m);
    }
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #11.272 s1025 MB + 76 KBAcceptedScore: 100


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