Submission #285037

#TimeUsernameProblemLanguageResultExecution timeMemory
285037SamAndSorting (IOI15_sorting)C++17
0 / 100
2 ms640 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 S[N];
int X[N], Y[N];

int a[N];
int b[N];

int uu[N];

int u[N];

bool c[N];

bool stg(int q, int P[], int Q[])
{
    for (int i = 0; i < n; ++i)
    {
        a[i] = S[i];
        b[i] = i;
        c[i] = false;
    }
    for (int i = 0; i < q; ++i)
    {
        swap(b[X[i]], b[Y[i]]);
    }
    for (int i = 0; i < n; ++i)
    {
        uu[b[i]] = i;
    }
    for (int i = 0; i < n; ++i)
    {
        u[i] = uu[a[i]];
    }

    int ans = 0;
    for (int i = 0; i < n; ++i)
    {
        int x = i;
        if (c[x])
            continue;
        vector<int> v;
        c[x] = true;
        while (1)
        {
            x = u[x];
            if (c[x])
                break;
            v.push_back(x);
            c[x] = true;
        }

        for (int i = 0; i < sz(v); ++i)
        {
            P[ans] = i;
            Q[ans] = v[i];
            ans++;
        }
    }

    return ans <= q;
}

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)
    {
        ::S[i] = S[i];
    }

    for (int i = 0; i < m; ++i)
    {
        ::X[i] = X[i];
        ::Y[i] = Y[i];
    }

    int l = 0, r = m;
    int ans;
    while (l <= r)
    {
        int mid = (l + r) / 2;
        if (stg(mid, P, Q))
        {
            ans = mid;
            r = mid - 1;
        }
        else
            l = mid + 1;
    }

    stg(ans, P, Q);
    return ans;
}


Compilation message (stderr)

sorting.cpp: In function 'bool stg(int, int*, int*)':
sorting.cpp:62:18: warning: declaration of 'i' shadows a previous local [-Wshadow]
   62 |         for (int i = 0; i < sz(v); ++i)
      |                  ^
sorting.cpp:46:14: note: shadowed declaration is here
   46 |     for (int i = 0; i < n; ++i)
      |              ^
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:73:78: warning: declaration of 'Y' shadows a global declaration [-Wshadow]
   73 | int findSwapPairs(int N_, int S[], int M_, int X[], int Y[], int P[], int Q[])
      |                                                                              ^
sorting.cpp:13:11: note: shadowed declaration is here
   13 | int X[N], Y[N];
      |           ^
sorting.cpp:73:78: warning: declaration of 'X' shadows a global declaration [-Wshadow]
   73 | int findSwapPairs(int N_, int S[], int M_, int X[], int Y[], int P[], int Q[])
      |                                                                              ^
sorting.cpp:13:5: note: shadowed declaration is here
   13 | int X[N], Y[N];
      |     ^
sorting.cpp:73:78: warning: declaration of 'S' shadows a global declaration [-Wshadow]
   73 | int findSwapPairs(int N_, int S[], int M_, int X[], int Y[], int P[], int Q[])
      |                                                                              ^
sorting.cpp:12:5: note: shadowed declaration is here
   12 | int S[N];
      |     ^
sorting.cpp:104:12: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
  104 |     return ans;
      |            ^~~
#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...