Cod sursa(job #1731842)

Utilizator DanFodorFODOR Dan Horatiu DanFodor Data 20 iulie 2016 02:51:07
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 5.1 kb
#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, current, next, previous;
    fscanf(inFile, "%d", &n);


    queue <Point> seq_queue;

    Point seq_bounds;
    seq_bounds.lower = 0;
    seq_bounds.upper = 0;

    bool increasing, ch = false;

    fscanf(inFile, "%d %d", &current, &next);

    a[0][0] = current;
    a[0][1] = next;

    increasing = true;

    if (current > next)
        increasing = false;

    previous = next;
    seq_bounds.upper = 1;

    for (int i = 2; i < n; i++)
    {
        fscanf(inFile, "%d", &current);
        a[0][i] = current;
        if (ch == true)
        {
            increasing = false;
            if (previous < current)
                increasing = true;
            ch = false;
        }

        if ((previous < current) == increasing)
        {
            ++seq_bounds.upper;
        }
        else
        {
            seq_queue.push(seq_bounds);
            if (increasing == 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 = current;
    }

    seq_queue.push(seq_bounds);

    if (increasing == 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]);
        }
    }



    // Merging part

    bool even = true;
    int len = seq_queue.size();

    if (len % 2 != 0)
        even = false;

    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;
}