Pagini recente » Cod sursa (job #1761281) | Cod sursa (job #2176147) | Cod sursa (job #16300) | Cod sursa (job #996718) | Cod sursa (job #1731495)
#include <cstdio>
#include <queue>
using namespace std;
struct Point {
int lower, upper;
};
int a[2][500012];
int main()
{
FILE * inFile, *outFile;
inFile = fopen ("algsort.in","r");
outFile = fopen ("algsort.out", "w+");
int n, x, y, previous;
fscanf(inFile, "%d", &n);
queue <Point> seq_queue;
Point seq_bounds;
seq_bounds.lower = 0;
seq_bounds.upper = 0;
bool poz, ch = false;
fscanf(inFile, "%d %d", &x, &y);
a[0][0] = x;
a[0][1] = y;
poz = true;
if (x > y)
poz = false;
previous = y;
seq_bounds.upper = 1;
for (int i = 2; i < n; i++)
{
fscanf(inFile, "%d", &x);
a[0][i] = x;
if (ch == true)
{
poz = false;
if (previous < x)
poz = true;
ch = false;
}
if ((previous < x) == poz)
{
++seq_bounds.upper;
}
else
{
seq_queue.push(seq_bounds);
if (poz == false)
{
int mid = (seq_bounds.lower + (seq_bounds.upper - seq_bounds.lower + 1) / 2);
for (int j = seq_bounds.lower; j < mid; ++j)
{
swap(a[0][j], a[0][seq_bounds.lower + seq_bounds.upper - j]);
}
}
ch = true;
seq_bounds.lower = i;
seq_bounds.upper = i;
}
previous = x;
}
seq_queue.push(seq_bounds);
if (poz == false)
{
for (int j = seq_bounds.lower; j < (n + seq_bounds.lower) / 2; ++j)
{
swap(a[0][j], a[0][n + seq_bounds.lower - j - 1]);
}
}
bool even = true;
int len = seq_queue.size();
if (len % 2 != 0)
even = false;
// Merging part
int take = (len + 1) / 2;
int times = 0;
while (len != 1)
{
if (even == false)
--take;
for (int i = 0; i < take; ++i)
{
// first-bounds - the bounds of the first seq
// to be merged with the second seq (for which
// we use second_bounds)
Point first_bounds, second_bounds;
first_bounds = seq_queue.front();
seq_queue.pop();
second_bounds = seq_queue.front();
seq_queue.pop();
Point new_seq;
new_seq.lower = first_bounds.lower;
new_seq.upper = second_bounds.upper;
seq_queue.push(new_seq);
int first_lower = first_bounds.lower;
int first_upper = first_bounds.upper;
int second_lower = second_bounds.lower;
int second_upper = second_bounds.upper;
int new_lower = first_lower;
int new_upper = second_upper;
for (int l = new_lower; l <= new_upper; ++l)
{
if (first_lower <= first_upper && second_lower <= second_upper)
{
if (a[times % 2][first_lower] < a[times % 2][second_lower])
{
a[(times + 1) % 2][l] = a[times % 2][first_lower];
++first_lower;
}
else
{
a[(times + 1) % 2][l] = a[times % 2][second_lower];
++second_lower;
}
}
else
{
if (first_lower > first_upper)
{
while (l <= new_upper)
{
a[(times + 1) % 2][l] = a[times % 2][second_lower];
++second_lower;
++l;
}
}
else
{
while (l <= new_upper)
{
a[(times + 1) % 2][l] = a[times % 2][first_lower];
++first_lower;
++l;
}
}
}
}
}
if (even == false)
{
Point remaining_seq = seq_queue.front();
int lower_bound, upper_bound;
lower_bound = remaining_seq.lower;
upper_bound = remaining_seq.upper;
for (lower_bound; lower_bound <= upper_bound; ++lower_bound)
a[(times + 1) % 2][lower_bound] = a[times % 2][lower_bound];
seq_queue.pop();
seq_queue.push(remaining_seq);
}
len = seq_queue.size();
take = (len + 1) / 2;
even = true;
if (len % 2 != 0)
even = false;
++times;
}
for (int i = 0; i < n; ++i)
fprintf(outFile, "%d ", a[times % 2][i]);
fprintf(outFile, "\n");
return 0;
}