Submission #432288

# Submission time Handle Problem Language Result Execution time Memory
432288 2021-06-18T07:24:56 Z snasibov05 Sorting (IOI15_sorting) C++14
0 / 100
1000 ms 76956 KB
#include "sorting.h"
#include <vector>
#include <cassert>
#include <algorithm>

using namespace std;

#define pii pair<int, int>
#define f first
#define s second
#define pb push_back

int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[]) {

    vector<vector<int>> v(m+1, vector<int>(n));
    vector<vector<int>> idx(m+1, vector<int>(n));
    for (int i = 0; i < n; ++i) {
        v[0][i] = s[i];
        idx[0][s[i]] = i;
    }


    for (int i = 1; i <= m; ++i) {
        p[i-1] = q[i-1] = 0;
        v[i] = v[i-1];
        idx[i] = idx[i-1];
        swap(idx[i][v[i][x[i-1]]], idx[i][v[i][y[i-1]]]);
        swap(v[i][x[i-1]], v[i][y[i-1]]);
    }

    vector<int> final = idx[m];
    vector<int> arr = v[m];
    vector<pii> swaps;

    for (int i = 0; i < n; ++i) {
        if (final[i] == i) continue;
        int k = final[i];
        swaps.pb({i, k});
        final[arr[i]] = k;
        arr[k] = arr[i];
        arr[i] = i;
    }

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

    arr = v[m];

    int k = swaps.size();
    for (int i = 1; i <= k; ++i){
        int a = idx[i][arr[swaps[i-1].f]];
        int b = idx[i][arr[swaps[i-1].s]];
        p[i-1] = a, q[i-1] = b;
        swap(idx[i][arr[swaps[i-1].f]], idx[i][arr[swaps[i-1].s]]);
        swap(v[i][a], v[i][b]);
        swap(arr[swaps[i-1].f], arr[swaps[i-1].s]);

        for (int j = i+1; j <= m; ++j) {
            v[j] = v[j-1];
            idx[j] = idx[j-1];
            swap(idx[j][v[j][x[j-1]]], idx[j][v[j][y[j-1]]]);
            swap(v[j][x[j-1]], v[j][y[j-1]]);
        }
    }

    assert(is_sorted(v[m].begin(), v[m].end()));

    return m;
}


Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:48:23: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   48 |     int k = swaps.size();
      |             ~~~~~~~~~~^~
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1084 ms 76956 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1084 ms 76956 KB Time limit exceeded
2 Halted 0 ms 0 KB -