刚学OI 60分求助
查看原帖
刚学OI 60分求助
1048226
Red_Star_楼主2024/7/26 15:21

写的基数排序,错了。

#include<bits/stdc++.h>
using namespace std;
namespace IO
{
	template<typename T>
	void read(T &_x){_x=0;int _f=1;char ch=getchar();while(!isdigit(ch)) _f=(ch=='-'?-1:_f),ch=getchar();while(isdigit(ch)) _x=_x*10+(ch^48),ch=getchar();_x*=_f;}
	template<typename T,typename... Args>
	void read(T &_x,Args&...others){Read(_x);Read(others...);}
	const int BUF=20000000;char buf[BUF],top,stk[32];int plen;
	#define pc(x) buf[plen++]=x
	#define flush(); fwrite(buf,1,plen,stdout),plen=0;
	template<typename T>inline void print(T x){if(!x){pc(48);return;}if(x<0) x=-x,pc('-');for(;x;x/=10) stk[++top]=48+x%10;while(top) pc(stk[top--]);}
}
using namespace IO;
const int N = 2e5+10;
int n,a[N],b[N],sum[20],id[N],id1[N],o=10,o1=10;
signed main()
{
	read(n);
	for(int i = 1;i <= n;i++) read(a[i]),b[i]=a[i];
	for(int i = 1;i <= n;i++) sum[a[i]%o]++;
	for(int i = 1;i <= 9;i++) sum[i] += sum[i-1];
	for(int i = n;i >= 1;i--) id[sum[a[i]%o]--] = i,a[i]/=o; 
	o1*=10;
	while(o1 < 1e9)
	{
		for(int i = 0;i <= 9;i++) sum[i] = 0;
		for(int i = 1;i <= n;i++) id1[i] = id[i];
		for(int i = 1;i <= n;i++) sum[a[i]%o]++;
		for(int i = 1;i <= 9;i++) sum[i] += sum[i-1];
		for(int i = n;i >= 1;i--) id[sum[a[id1[i]]%o]--] = id1[i],a[id1[i]]/=o;
		o1*=10;
	}
	for(int i = 1;i <= n;i++) print(b[id[i]]),pc(' ');
	flush();
	return 0;
}
2024/7/26 15:21
加载中...