Submission #283388

#TimeUsernameProblemLanguageResultExecution timeMemory
283388stoyan_malininSorting (IOI15_sorting)C++14
20 / 100
2 ms384 KiB
#include "sorting.h"
//#include "grader.cpp"

#include <vector>
#include <assert.h>
#include <iostream>
#include <functional>

using namespace std;

int guessSubtask(int m, int *x, int *y)
{
    bool subtask2 = true;
    for(int i = 0;i<m;i++)
    {
        if(!(x[i]==0 && y[i]==0))
        {
            subtask2 = false;
            break;
        }
    }
    if(subtask2==true) return 2;

    return -1;
}

bool checkSorted(int n, int *s)
{
    for(int i = 1;i<n;i++)
    {
        if(s[i-1]>s[i]) return false;
    }

    return true;
}

int solve2(int n, int *s, int *p, int *q)
{
    if(checkSorted(n, s)==true) return 0;

    int swapInd = 0;
    for(int i = 0;i<n;i++)
    {
        for(int j = i+1;j<n;j++)
        {
            if(s[j]==i)
            {
                swap(s[i], s[j]);
                p[swapInd] = i;
                q[swapInd] = j;
                swapInd++;

                if(checkSorted(n, s)==true) return swapInd;
                break;
            }
        }
    }

    assert(false);
}

int solve3(int n, int *s, int m, int *x, int *y, int *p, int *q)
{
    if(checkSorted(n, s)==true) return 0;

    int swapInd = 0;
    swap(s[ x[swapInd] ], s[ y[swapInd] ]);

    for(int i = 2;i<n;i++)
    {
        //cout << " -> " << i << " " << s[i] << '\n';
        function <bool(int)> isGood = [&](int x)
        {
            if(x==0 || x==1) return true;
            return false;
        };

        if(isGood(s[i])==true)
        {
            for(int j = 0;j<2;j++)
            {
                if(isGood(s[j])==false)
                {
                    p[swapInd] = j;
                    q[swapInd] = i;
                    swap(s[ p[swapInd] ], s[ q[swapInd] ]);
                    swapInd++;

                    if(checkSorted(n, s)==true) return swapInd;

                    swap(s[ x[swapInd] ], s[ y[swapInd] ]);
                }
            }
        }
    }

    for(int i = 2;i<n;i++)
    {
        for(int j = i+1;j<n;j++)
        {
            if(s[j]==i)
            {
                p[swapInd] = i;
                q[swapInd] = j;
                swap(s[ p[swapInd] ], s[ q[swapInd] ]);
                swapInd++;

                if(checkSorted(n, s)==true)
                {
                    //cout << "ok" << '\n';
                    return swapInd;
                }

                swap(s[ x[swapInd] ], s[ y[swapInd] ]);
                break;
            }
        }
    }

    //cout << "A SQ DE" << '\n';
    if(checkSorted(n, s)==false)
    {
        //cout << "hier" << '\n';

        p[swapInd] = 0;
        q[swapInd] = 1;
        swapInd++;
    }
    else
    {
        assert(false);

        p[swapInd] = 0;
        q[swapInd] = 0;
        swapInd++;
    }

    //cout << " ---- " << "END" << " --- " << '\n';
    return swapInd;
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[])
{
    int subtask = guessSubtask(M, X, Y);

    if(subtask==2) return solve2(N, S, P, Q);
    return solve3(N, S, M, X, Y, P, Q);

    assert(false);
}
/*
6
5 1 0 2 4 3
6
0 1
0 1
0 1
0 1
0 1
0 1
*/

Compilation message (stderr)

sorting.cpp: In lambda function:
sorting.cpp:72:48: warning: declaration of 'int x' shadows a parameter [-Wshadow]
   72 |         function <bool(int)> isGood = [&](int x)
      |                                                ^
sorting.cpp:62:39: note: shadowed declaration is here
   62 | int solve3(int n, int *s, int m, int *x, int *y, int *p, int *q)
      |                                  ~~~~~^
sorting.cpp: In function 'int solve3(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:62:31: warning: unused parameter 'm' [-Wunused-parameter]
   62 | int solve3(int n, int *s, int m, int *x, int *y, int *p, int *q)
      |                           ~~~~^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...