提交记录 6134


用户 题目 状态 得分 用时 内存 语言 代码长度
luohy15 1001. 测测你的排序 Compile Error 0 0 ns 0 KB C++11 1.16 KB
提交时间 评测时间
2018-09-27 23:58:15 2020-08-01 00:39:25
//****************************************************************************
// BEGIN INT SORT
//
// Problem: sort an array of 32 bit integers as fast as possible.
//
// Basic idea:
//	First sort the lower 16 bits using radix sort (O(65536) + O(n)), 
//	then sort the higher 16 bits stably using radix sort (O(65536) + O(n))

// CONFIG:
#define MAX_N 5000000

// IMPL:
#include <cstring>
#define l16(a) ((a)&65535)
#define h16(a) (((unsigned)(a))>>16)
size_t cnt[65536];
unsigned store[MAX_N];

static inline void pass1(unsigned* a, size_t n) {
	memset(cnt, 0, sizeof(cnt));
	for (unsigned i = 0; i < n; i++) cnt[l16(a[i])]++;
	for (unsigned i = 1; i < 65536; i++) cnt[i] += cnt[i-1];
	for (unsigned i = n-1; i >= 0; i--) store[--cnt[l16(a[i])]] = a[i];
}

static inline void pass2(unsigned* a, size_t n) {
	memset(cnt, 0, sizeof(cnt));
	for (unsigned i = 0; i < n; i++) cnt[h16(a[i])]++;
	for (unsigned i = 1; i < 65536; i++) cnt[i] += cnt[i-1];
	for (unsigned i = n-1; i >= 0; i--) a[--cnt[h16(store[i])]] = store[i];
}

void sort(unsigned* a, size_t n) {
	pass1(a, n);
	pass2(a, n);
}

// END INT SORT
//****************************************************************************

CompilationN/AN/ACompile ErrorScore: N/A


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