Cod sursa(job #1522890)

Utilizator DanFodorFODOR Dan Horatiu DanFodor Data 12 noiembrie 2015 02:17:38
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 5.05 kb
#include <cstdio>
#include <queue>

using namespace std;

struct Point {
    int start, fin;
};

int a[2][500012];

int main()
{
    FILE * inFile, *outFile;

    inFile = fopen ("algsort.in","r");
    outFile = fopen ("algsort.out", "w+");

    int n, x, y, prev;
    fscanf(inFile, "%d", &n);
    if (n <= 2)
    {
        if (n == 1)
        {
            fscanf(inFile, "%d", &x);
            fprintf(outFile, "%d\n", x);
        }
        if (n == 2)
        {
            fscanf(inFile, "%d %d", &x, &y);
            fprintf(outFile, "%d %d", min(x, y), max(x, y));
        }
    }
    else
    {
        queue <Point> q;

        Point p;
        p.start = 0;
        p.fin = 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;
        prev = y;
        p.fin = 1;
        for (int i = 2; i < n; i++)
        {
            fscanf(inFile, "%d", &x);
            a[0][i] = x;
            if (ch == true)
            {
                poz = false;
                if (prev < x)
                    poz = true;
                ch = false;
            }

            if ((prev < x) == poz)
            {
                ++p.fin;
            }
            else
            {
                q.push(p);
                if (poz == false)
                {
                    int mid = (p.start + (p.fin  - p.start + 1) / 2);
                    for (int j = p.start; j < mid; ++j)
                    {
                        swap(a[0][j], a[0][p.start + p.fin - j]);
                    }
                }
                ch = true;
                p.start = i;
                p.fin = i;
            }

            prev = x;
        }
        q.push(p);
        if (poz == false)
        {
            for (int j = p.start; j < (n + p.start) / 2; ++j)
            {
                swap(a[0][j], a[0][n + p.start - j - 1]);
            }
        }

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

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

        int take = (len + 1) / 2;
        int times = 0;
        //for (int l = 0; l < n; ++l)
        //    a[1][l] = a[0][l];


        while (len != 1)
        {
            if (even == false)
                --take;
            for (int i = 0; i < take; ++i)
            {
                Point p1, p2;
                p1 = q.front();
                q.pop();
                p2 = q.front();
                q.pop();

                Point newP;
                newP.start = p1.start;
                newP.fin = p2.fin;
                q.push(newP);

                int lb1 = p1.start;
                int lb2 = p2.start;
                int up1 = p1.fin;
                int up2 = p2.fin;
                int mini = lb1;
                int maxi = up2;

                for (int l = mini; l <= maxi; ++l)
                {
                    if (lb1 <= up1 && lb2 <= up2)
                    {
                        if (a[times % 2][lb1] < a[times % 2][lb2])
                        {
                            a[(times + 1) % 2][l] = a[times % 2][lb1];
                            ++lb1;
                        }
                        else
                        {
                            a[(times + 1) % 2][l] = a[times % 2][lb2];
                            ++lb2;
                        }
                    }
                    else
                    {
                        if (lb1 > up1)
                        {
                            while (l <= maxi)
                            {
                                a[(times + 1) % 2][l] = a[times % 2][lb2];
                                ++lb2;
                                ++l;
                            }
                        }
                        else
                        {
                            while (l <= maxi)
                            {
                                a[(times + 1) % 2][l] = a[times % 2][lb1];
                                ++lb1;
                                ++l;
                            }
                        }
                    }
                }
                //for (int l = mini; l <= maxi; ++l)
                //    a[0][l] = b[l];
            }
            if (even == false)
            {
                Point z = q.front();
                int in, stop;
                in = z.start;
                stop = z.fin;
                for (in; in <= stop; ++in)
                    a[(times + 1) % 2][in] = a[times % 2][in];
                q.pop();
                q.push(z);
            }

            len = q.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;
}