Submission #796420

# Submission time Handle Problem Language Result Execution time Memory
796420 2023-07-28T11:27:15 Z Josia Sorting (IOI15_sorting) C++17
0 / 100
1 ms 852 KB
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;


int countCycles(vector<int> p) {
    vector<bool> vis(p.size());
    int count=0;

    for (int i = 0; i<(int)p.size(); i++) {
        if (!vis[i]) count++;
        int pos = i;
        while(!vis[pos]) {
            vis[pos] = 1;
            pos = p[pos];
        }
    }
    return count;
}


vector<int> doSwaps(vector<int> input, vector<pair<int, int>> swaps, int number) {
    for (int i = 0; i<number; i++) {
        swap(input[swaps[i].first], input[swaps[i].second]);
    }
    return input;
}


int findSwapPairs(int N, int _S[], int M, int X[], int Y[], int P[], int Q[]) {
	vector<int> S(N);
	for (int i = 0; i<N; i++) S[i] = _S[i];

    vector<pair<int, int>> swapsOther;
    for (int i = 0; i<M; i++) swapsOther.push_back({X[i], Y[i]});


    int l = 0, r = M;

    while (l < r) {
        int m = (l+r)/2;
        int cycles = countCycles(doSwaps(S, swapsOther, m));
        // cerr << m << " " << cycles << "\n";

        if (N-cycles <= m) {
            r = m;
        } else {
            l = m+1;
        }
    }

    vector<int> hisPerm = doSwaps(S, swapsOther, l);

    // cerr << l << "\n";
    // for (int i: hisPerm) cerr << i << " ";
    // cerr << "\n";

    vector<pair<int, int>> mySwapsValues;

    for (int i = 0; i<N; i++) {
        if (hisPerm[i] == i) continue;

        mySwapsValues.push_back({hisPerm[i], hisPerm[hisPerm[i]]});

        swap(hisPerm[i], hisPerm[hisPerm[i]]);
    }

    assert(is_sorted(hisPerm.begin(), hisPerm.end()));

    // for (auto i: mySwapsValues) {cerr << i.first << " " << i.second << "\n";}


    vector<int> SInv(N);

    for (int i = 0; i<N; i++) SInv[S[i]] = i;


    for (int i = 0; i<mySwapsValues.size(); i++) {
        swap(SInv[S[swapsOther[i].first]], SInv[S[swapsOther[i].second]]);
        swap(S[swapsOther[i].first], S[swapsOther[i].second]);

        P[i] = SInv[mySwapsValues[i].first];
        Q[i] = SInv[mySwapsValues[i].second];

        swap(SInv[S[P[i]]], SInv[S[Q[i]]]);
        swap(S[P[i]], S[Q[i]]);
    }

    return mySwapsValues.size();
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:78:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |     for (int i = 0; i<mySwapsValues.size(); i++) {
      |                     ~^~~~~~~~~~~~~~~~~~~~~
sorting.cpp:89:30: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   89 |     return mySwapsValues.size();
      |            ~~~~~~~~~~~~~~~~~~^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Runtime error 1 ms 340 KB Execution killed with signal 6
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Runtime error 1 ms 340 KB Execution killed with signal 6
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 300 KB Output is correct
3 Runtime error 1 ms 596 KB Execution killed with signal 6
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Runtime error 1 ms 340 KB Execution killed with signal 6
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 852 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 852 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -