Pagini recente » Cod sursa (job #953685) | Cod sursa (job #3169176) | Cod sursa (job #508234) | Cod sursa (job #2302722) | Cod sursa (job #1813537)
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <queue>
#define pb push_back
#define mp make_pair
#define MAXN 500001
#define DIM 8192
#define RAD 0xFF
#define RADS 8
#define get_byte(x) ((x>>(byte * 8))&RAD)
using namespace std;
class Reader
{
public:
Reader(const char* filename)
{
if(filename[0]=='.'||filename[0]==0)
{
cout<<"Check INFILE.\n";
return;
}
try
{
f.open(filename);
pos = 0;
if(f.fail())throw 1;
f.read(buffer,DIM);
}
catch(int e)
{
cout<<"No INFILE.\n";
delete this;
}
}
inline Reader& operator >>(int& x)
{
x=0;
sgn=1;
while((buffer[pos]<'0'||buffer[pos]>'9')&&buffer[pos]!='-')
if(++pos==DIM)
f.read(buffer,DIM),pos=0;
if(buffer[pos]=='-')sgn=-1,pos++;
while(buffer[pos]>='0'&&buffer[pos]<='9')
{
x=x*10+buffer[pos]-'0';
if(++pos==DIM)f.read(buffer,DIM),pos=0;
}
x*=sgn;
return (*this);
}
inline Reader& operator >>(char* c)
{
aux = 0;
while(isspace(buffer[pos]))
if(++pos==DIM)
f.read(buffer,DIM),pos=0;
while(!isspace(buffer[pos]))
{
c[aux++]=buffer[pos];
if(++pos==DIM)f.read(buffer,DIM),pos=0;
}
return (*this);
}
~Reader()
{
f.close();
}
private:
ifstream f;
int pos,aux,sgn;
char buffer[DIM];
};
class Writer
{
public:
Writer(const char* filename)
{
if(filename[0]=='.'||filename[0]==0)
cout<<"Check OUTFILE. Nothing created.\n";
else
{
g.open(filename);
pos = 0;
}
}
inline Writer& operator <<(int xx)
{
aux = 0;
x = xx;
if(xx<0)x=-x;
if(x==0)
nr[aux++]='0';
while(x)
{
nr[aux++]=x%10+'0';
x/=10;
}
if(xx<0)nr[aux++]='-';
while(aux>0)
{
buffer[pos++]=nr[--aux];
if(pos==DIM)g.write(buffer,DIM),pos=0;
}
return (*this);
}
inline Writer& operator <<(const char* c)
{
aux = 0;
while(c[aux])
{
buffer[pos++]=c[aux++];
if(pos==DIM)g.write(buffer,DIM),pos=0;
}
return (*this);
}
inline void flush()
{
g.write(buffer,pos);
pos=0;
}
~Writer()
{
g.write(buffer,pos);
g.close();
}
private:
ofstream g;
int pos,aux,x;
char buffer[DIM],nr[21];
};
Reader f("algsort.in");
Writer g("algsort.out");
const int SZ = 1<<RADS;
int v[MAXN],n;
queue<int> q[SZ],aux;
void countSort(int byte) {
for(int i=0;i<SZ;i++)
q[i]=aux;
int m=SZ,M=0,t;
for(int i=0;i<n;i++)
{
t = get_byte(v[i]);
q[t].push(v[i]);
if(t<m)m=t;
if(t>M)M=t;
}
int cnt=0;
for(int i=m;i<=M;i++)
{
while(!q[i].empty())
{
v[cnt++] = q[i].front();
q[i].pop();
}
}
}
void radixsort() {
for(int i=0;i<4;i++)
countSort(i);
}
int main()
{
ios_base::sync_with_stdio(false);
f>>n;
for(int i=0;i<n;i++)f>>v[i];
radixsort();
for(int i=0;i<n;i++)g<<v[i]<<" ";
return 0;
}