Pagini recente » Arhiva de probleme | Infoarena Monthly 2014 - Clasament Runda 2 | Monitorul de evaluare | Cod sursa (job #1131256) | Cod sursa (job #1567269)
#include <fstream>
#include <iostream>
using namespace std;
const int MAX_N = 5e5 + 10;
int v[MAX_N];
int aux[MAX_N];
int n;
void __attribute__((always_inline)) merge(int i, const int step) {
const int fst = i;
int j = i + step;
const int ui = min(i + step - 1, n);
const int uj = min(j + step - 1, n);
int st = 0;
while(i <= ui && j <= uj) {
if(v[i] < v[j])
aux[++st] = v[i++];
else
aux[++st] = v[j++];
}
if(i <= ui) {
while(i <= ui)
aux[++st] = v[i++];
} else {
while(j <= uj)
aux[++st] = v[j++];
}
for(int k = 1; k <= st; ++k)
v[fst + k - 1] = aux[k];
}
void msort() {
for(int step = 1; step < n; step *= 2) {
for(int i = 1; i + step <= n; i += 2 * step) {
merge(i, step);
}
}
}
int main() {
ifstream in("algsort.in");
in >> n;
for(int i = 1; i <= n; ++i)
in >> v[i];
in.close();
msort();
ofstream out("algsort.out");
for(int i = 1; i <= n; ++i)
out << v[i] << " ";
out << "\n";
}