zoukankan      html  css  js  c++  java
  • 基数排序板子

    基数排序板子

    noip好像t4还有20分是这个

    另外,挑排(松排)是把基数U设为 (2^8) 卡进cache,需要卡常的话可以试试

    #include<bits/stdc++.h>
    using namespace std;
    const int N = 1e5+200,U=65536,L=16;
    #define FOR(i,a,b) for(int i=a;i<=b;++i)
    #define ROF(i,a,b) for(int i=a;i>=b;--i)
    int n,a[N];
    int read(){
    	int x=0,pos=1;char ch=getchar();
    	for(;!isdigit(ch);ch=getchar()) if(ch=='-') pos=0;
    	for(;isdigit(ch);ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
    	return pos?x:-x;
    }
    int b[N],cnt[U];
    int get(int x,int d){
    	return (x>>(d*L))&(U-1);
    }
    void radix_sort(int *x){
    	int *y=b;
    	for(int d=0;d<2;d++){ // U^2 < max(wi) 从低往高排 
    		memset(cnt,0,sizeof cnt);
    		FOR(i,1,n) ++cnt[get(x[i],d)];
    		FOR(i,1,U-1) cnt[i]+=cnt[i-1];
    		ROF(i,n,1) y[cnt[get(x[i],d)]--]=x[i]; // 值相等的是前一次排的顺序 
    		swap(x,y); 
    	}
    }
    int main(){
    	n=read();
    	FOR(i,1,n) a[i]=read();
    	radix_sort(a);
    	FOR(i,1,n) printf("%d ",a[i]);
    	return 0;
    }
    
  • 相关阅读:
    软链接
    yum
    vm
    tengine
    创智LIUNX
    作业11
    作业10
    作业9
    作业8
    作业7
  • 原文地址:https://www.cnblogs.com/lcyfrog/p/14358475.html
Copyright © 2011-2022 走看看