#include <cstddef>
namespace ya
{
//template <typename ValType>
//void fill(ValType *Begin, ValType *End, ValType Val);
//Fill [Begin, End) with Val.
template <typename ValType>
void fill(ValType *Begin, ValType *End, ValType Val)
{
for(; Begin != End; ) (*(Begin++)) = Val;
//std::size_t n(End - Begin);
//while(n--) (*(Begin++)) = Val;
}
//template <typename PtrType, typename ValType, typename SizeType>
//void memset(PtrType Dest, ValType Val, SizeType Size)
//Fill [Dest, Dest + Size) with (unsigned char)Val.
//It only works On 64-bit Architecture
template <typename PtrType, typename ValType, typename SizeType>
void memset(PtrType Dest, ValType Val, SizeType Size)
{
union CharPointerConverter64{
PtrType PtrPointer;
unsigned char *CharPointer;
unsigned long long *LLPointer;
unsigned long long Number;
};
const unsigned char ValChar(Val);
CharPointerConverter64 Begin, End, Margin;
Begin.PtrPointer = Dest;
Margin.Number = Begin.Number + ((unsigned long long)Size & 7ull);
End.CharPointer = Begin.CharPointer + Size;
fill(Begin.CharPointer, Margin.CharPointer, ValChar);
unsigned long long Blk(ValChar);
Blk |= (Blk << 8);
Blk |= (Blk << 16);
Blk |= (Blk << 32);
fill(Margin.LLPointer, End.LLPointer, Blk);
/*
for (char *DestEnd(Dest + Size); Dest != DestEnd;)
(*(Dest++)) = ValChar;*/
}
};
#include<cstddef>
namespace ya
{
#define YA_RADIX_SORT_BASE 256
#define YA_RADIX_SORT_LOOP(Begin,End,Begin2,End2,OP,CntArr) \
i=Begin2;\
for(k=0;k!=YA_RADIX_SORT_BASE;++k)\
La[k]=(i+=CntArr[k]);\
i=End;\
do{\
--i;\
(*(--La[OP(*i)]))=(*i);\
}while(i!=Begin);
void radix_sort(unsigned int *Begin,unsigned int *End){
size_t N(End-Begin),k;
unsigned int Begin2[N],*End2(Begin2+N),*La[YA_RADIX_SORT_BASE],*i;
size_t Cnt1[YA_RADIX_SORT_BASE];memset(Cnt1,0,YA_RADIX_SORT_BASE*sizeof(size_t));
size_t Cnt2[YA_RADIX_SORT_BASE];memset(Cnt2,0,YA_RADIX_SORT_BASE*sizeof(size_t));
size_t Cnt3[YA_RADIX_SORT_BASE];memset(Cnt3,0,YA_RADIX_SORT_BASE*sizeof(size_t));
size_t Cnt4[YA_RADIX_SORT_BASE];memset(Cnt4,0,YA_RADIX_SORT_BASE*sizeof(size_t));
#define YA_RADIX_SORT_UNSIGNED_INT_OP1(x) ((x)&255u)
#define YA_RADIX_SORT_UNSIGNED_INT_OP2(x) ((x)>>8&255u)
#define YA_RADIX_SORT_UNSIGNED_INT_OP3(x) ((x)>>16&255u)
#define YA_RADIX_SORT_UNSIGNED_INT_OP4(x) ((x)>>24)
for(i=Begin;i!=End;++i){
++Cnt1[YA_RADIX_SORT_UNSIGNED_INT_OP1(*i)];
++Cnt2[YA_RADIX_SORT_UNSIGNED_INT_OP2(*i)];
++Cnt3[YA_RADIX_SORT_UNSIGNED_INT_OP3(*i)];
++Cnt4[YA_RADIX_SORT_UNSIGNED_INT_OP4(*i)];
}
YA_RADIX_SORT_LOOP(Begin,End,Begin2,End2,YA_RADIX_SORT_UNSIGNED_INT_OP1,Cnt1);
YA_RADIX_SORT_LOOP(Begin2,End2,Begin,End,YA_RADIX_SORT_UNSIGNED_INT_OP2,Cnt2);
YA_RADIX_SORT_LOOP(Begin,End,Begin2,End2,YA_RADIX_SORT_UNSIGNED_INT_OP3,Cnt3);
YA_RADIX_SORT_LOOP(Begin2,End2,Begin,End,YA_RADIX_SORT_UNSIGNED_INT_OP4,Cnt4);
}
#undef YA_RADIX_SORT_LOOP
};
void sort(unsigned *a, int n){
ya::radix_sort(a,a+n);
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 1.006 ms | 804 KB | Accepted | Score: 34 | 显示更多 |
Testcase #2 | 1.101 s | 762 MB + 984 KB | Accepted | Score: 33 | 显示更多 |
Testcase #3 | 2.203 s | 1525 MB + 924 KB | Accepted | Score: 33 | 显示更多 |