Submission #284502

#TimeUsernameProblemLanguageResultExecution timeMemory
284502SamAndSorting (IOI15_sorting)C++17
0 / 100
1090 ms10008 KiB
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
const int N = 200005;

int n, m;
int a[N];

vector<int> g[N];

int c[N];
int k;

void dfs(int x)
{
    c[x] = k;
    for (int i = 0; i < g[x].size(); ++i)
    {
        int h = g[x][i];
        if (!c[h])
            dfs(h);
    }
}

vector<int> v[N];

int findSwapPairs(int N_, int S[], int M_, int X[], int Y[], int P[], int Q[])
{
    n = N_;
    m = M_;

    for (int i = 0; i < n; ++i)
        a[i] = S[i];

    bool z = true;
    for (int i = 0; i < n - 1; ++i)
    {
        if (a[i] > a[i + 1])
        {
            z = false;
            break;
        }
    }

    if (z)
        return 0;

    for (int i = 0; i < m; ++i)
    {
        P[i] = Q[i] = 0;
        swap(a[X[i]], a[Y[i]]);

        for (int x = 0; x < n; ++x)
            g[x].clear();
        for (int j = i + 1; j < m; ++j)
        {
            g[X[j]].push_back(Y[j]);
            g[Y[j]].push_back(X[j]);
        }

        for (int x = 0; x < n; ++x)
            c[x] = 0;
        k = 0;
        for (int x = 0; x < n; ++x)
        {
            if (!c[x])
            {
                ++k;
                dfs(x);
            }
        }

        for (int i = 1; i <= k; ++i)
            v[i].clear();
        for (int x = 0; x < n; ++x)
            v[c[x]].push_back(a[x]);
        for (int i = 1; i <= k; ++i)
            sort(all(v[i]));
        for (int x = 0; x < n; ++x)
        {
            if (!binary_search(all(v[c[x]]), x))
            {
                vector<int> vv;
                for (int y = 0; y < n; ++y)
                {
                    if (c[y] == c[x])
                        vv.push_back(y);
                }
                int xx = -1;
                for (int i = 0; i < v[c[x]].size(); ++i)
                {
                    int u = v[c[x]][i];
                    if (!binary_search(all(vv), u))
                    {
                        for (int y = 0; y < n; ++y)
                        {
                            if (a[y] == u)
                            {
                                xx = y;
                                break;
                            }
                        }
                        break;
                    }
                }
                assert(xx != -1);
                for (int y = 0; y < n; ++y)
                {
                    if (a[y] == x)
                    {
                        swap(a[xx], a[y]);
                        P[i] = xx;
                        Q[i] = y;
                        break;
                    }
                }
                break;
            }
        }

        bool z = true;
        for (int i = 0; i < n - 1; ++i)
        {
            if (a[i] > a[i + 1])
            {
                z = false;
                break;
            }
        }
        if (z)
        {
            for (int j = 0; j <= i; ++j)
            {
                swap(S[X[j]], S[Y[j]]);
                swap(S[P[j]], S[Q[j]]);
            }
            while (1){}
            for (int i = 0; i < n; ++i)
            {
                if (S[i] != i)
                {
                    while (1)
                    {
                        assert(false);
                    }
                }
            }
            return (i + 1);
        }
    }
    while (1)
    {
        assert(false);
    }
    return -1;
}


Compilation message (stderr)

sorting.cpp: In function 'void dfs(int)':
sorting.cpp:22:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 |     for (int i = 0; i < g[x].size(); ++i)
      |                     ~~^~~~~~~~~~~~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:78:18: warning: declaration of 'i' shadows a previous local [-Wshadow]
   78 |         for (int i = 1; i <= k; ++i)
      |                  ^
sorting.cpp:53:14: note: shadowed declaration is here
   53 |     for (int i = 0; i < m; ++i)
      |              ^
sorting.cpp:82:18: warning: declaration of 'i' shadows a previous local [-Wshadow]
   82 |         for (int i = 1; i <= k; ++i)
      |                  ^
sorting.cpp:53:14: note: shadowed declaration is here
   53 |     for (int i = 0; i < m; ++i)
      |              ^
sorting.cpp:95:26: warning: declaration of 'i' shadows a previous local [-Wshadow]
   95 |                 for (int i = 0; i < v[c[x]].size(); ++i)
      |                          ^
sorting.cpp:53:14: note: shadowed declaration is here
   53 |     for (int i = 0; i < m; ++i)
      |              ^
sorting.cpp:95:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |                 for (int i = 0; i < v[c[x]].size(); ++i)
      |                                 ~~^~~~~~~~~~~~~~~~
sorting.cpp:126:14: warning: declaration of 'z' shadows a previous local [-Wshadow]
  126 |         bool z = true;
      |              ^
sorting.cpp:40:10: note: shadowed declaration is here
   40 |     bool z = true;
      |          ^
sorting.cpp:127:18: warning: declaration of 'i' shadows a previous local [-Wshadow]
  127 |         for (int i = 0; i < n - 1; ++i)
      |                  ^
sorting.cpp:53:14: note: shadowed declaration is here
   53 |     for (int i = 0; i < m; ++i)
      |              ^
sorting.cpp:143:22: warning: declaration of 'i' shadows a previous local [-Wshadow]
  143 |             for (int i = 0; i < n; ++i)
      |                      ^
sorting.cpp:53:14: note: shadowed declaration is here
   53 |     for (int i = 0; i < m; ++i)
      |              ^
#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...