Submission #316647

#TimeUsernameProblemLanguageResultExecution timeMemory
316647jainbot27Sorting (IOI15_sorting)C++17
100 / 100
236 ms16876 KiB
/*
Observation:
if we can do it in N turns, we can do it in N + 1

*/

#include <bits/stdc++.h>
using namespace std;

#define f first
#define s second
#define pb push_back
#define ar array
#define all(x) x.begin(), x.end()
#define siz(x) (int)x.size()

#define FOR(x, y, z) for(int x = (y); x < (z); x++)
#define ROF(x, z, y) for(int x = (y-1); x >= (z); x--)
#define F0R(x, z) FOR(x, 0, z)
#define R0F(x, z) ROF(x, 0, z)
#define trav(x, y) for(auto&x:y)

using ll = long long;
using vi = vector<int>;
using vl = vector<long long>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;

template<class T> inline bool ckmin(T&a, T b) {return b < a ? a = b, 1 : 0;}
template<class T> inline bool ckmax(T&a, T b) {return b > a ? a = b, 1 : 0;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const char nl = '\n';
const int mxN = 2e5 + 10;
const int MOD = 1e9 + 7;
const long long infLL = 1e18;

int findSwapPairs(int n, int* s, int M, int* x, int *y, int *p, int *q){
    int l = 0, r = M + 1;
    vpii ans;
    while(l < r){
        int m = l + r; m = m / 2;
        vi pos(n);
        vi cur(n);
        vpii ops;
        F0R(i, n){
            cur[i] = s[i];
        }
        F0R(i, m){
            swap(cur[x[i]], cur[y[i]]);
        }
        //cout << m << endl;
        //F0R(i, n){
        //    cout << cur[i] << ' ';
        //}
        //cout << endl;
        F0R(i, n){
            pos[cur[i]] = i;
        }
        F0R(i, n){
            if(cur[i] != i){
                int x = cur[i];
                // whats currently at the spot we want
                int z = pos[i];
                // the pos of our current element
                ops.pb({i, x});
                cur[z] = x;
                cur[i] = i;
                pos[i] = i;
                pos[x] = z;

            }
        }
        if(siz(ops) <= m){
            ans = ops;
            r = m;
        }
        else{
            l = m + 1;
        }
    }
    vi pos(n);
    vi cur(n);
    vpii res;
    F0R(i, n){
        pos[s[i]] = i;
        cur[i] = s[i];
    }
    F0R(i, siz(ans)){
        //F0R(i, n){ 
        //    cout<<cur[i]<<' ';
        //}
        //cout << nl;
        swap(pos[cur[x[i]]], pos[cur[y[i]]]);
        swap(cur[x[i]], cur[y[i]]);
        res.pb({pos[ans[i].f], pos[ans[i].s]});
        //cout << ans[i].f << ' ' << ans[i].s << nl;
        //cout << pos[ans[i].f] << ' ' << pos[ans[i].s] << nl;
        swap(pos[ans[i].f], pos[ans[i].s]);
        //assert(pos[cur[ans[i].f]]==ans[i].s);
        swap(cur[pos[ans[i].f]], cur[pos[ans[i].s]]);
    }
    F0R(i, l){
        if(i < res.size()){
            p[i] = res[i].f; q[i] = res[i].s;
        }
        else{
            p[i] = 0; q[i] = 0;
        }
    }
    return l;
}
//int32_t main(){
//    ios_base::sync_with_stdio(0); cin.tie(0);
//    int N;
//    cin >> N;
//    int S[N];
//    F0R(i, N){
//        cin >> S[i];
//    }
//    int M;
//    cin >> M;
//    int X[M], Y[M];
//    F0R(i, M){
//        cin >> X[i] >> Y[i];
//    }
//    int P[M], Q[M];
//    int res = findSwapPairs(N, S, M, X, Y, P, Q);
//    cout << res << nl; 
//    F0R(i, res){
//        cout << P[i] << ' ' << Q[i] << nl;
//    }
//    return 0;
//}

Compilation message (stderr)

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:62:21: warning: declaration of 'int x' shadows a parameter [-Wshadow]
   62 |                 int x = cur[i];
      |                     ^
sorting.cpp:38:46: note: shadowed declaration is here
   38 | int findSwapPairs(int n, int* s, int M, int* x, int *y, int *p, int *q){
      |                                         ~~~~~^
sorting.cpp:104:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  104 |         if(i < res.size()){
      |            ~~^~~~~~~~~~~~
#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...