Submission #594590

# Submission time Handle Problem Language Result Execution time Memory
594590 2022-07-12T17:25:23 Z davi_bart Sorting (IOI15_sorting) C++14
56 / 100
1000 ms 8052 KB
#pragma GCC optimize("O3")
#include <bits/stdc++.h>

#include "sorting.h"
using namespace std;
#define ll long long
// #define int ll
#define fi first
#define se second
#define ld long double
#define pb push_back
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int N;
struct Dsu {
    vector<int> par, dim;
    Dsu(vector<int> &k) {
        par.resize(N);
        dim = vector<int>(N, 1);
        iota(par.begin(), par.end(), 0);
        for (int i = 0; i < k.size(); i++) {
            unite(i, k[i]);
        }
    }
    int find(int pos) {
        return par[pos] = par[pos] == pos ? pos : find(par[pos]);
    }
    void unite(int a, int b) {
        a = find(a);
        b = find(b);
        if (a == b) return;
        if (dim[a] < dim[b]) swap(a, b);
        par[b] = a;
        dim[a] += dim[b];
    }
    int conta() {
        int tot = 0;
        for (int i = 0; i < N; i++) {
            if (i == par[i]) tot += dim[i] - 1;
        }
        return tot;
    }
};
vector<int> v;
vector<int> ini;
int findSwapPairs(int N, int S[], int M, int P[], int Q[], int X[], int Y[]) {
    ::N = N;
    for (int i = 0; i < N; i++) v.pb(S[i]);
    ini = v;
    int ans = 0;
    for (int i = 0; i < M; i++) {
        Dsu dsu(v);
        // cout << dsu.conta() << " ";
        if (dsu.conta() <= i) {
            ans = i;
            break;
        }
        swap(v[P[i]], v[Q[i]]);
    }
    // cout << endl;
    // int o = 0;
    vector<pair<int, int> > scambi;
    while (1) {
        int k = scambi.size();
        for (int i = 0; i < N; i++) {
            if (v[i] != i) {
                scambi.pb({v[i], i});
                swap(v[find(v.begin(), v.end(), i) - v.begin()], v[i]);
            }
        }
        if (k == scambi.size()) break;
    }
    assert(scambi.size() <= ans);
    v = ini;
    for (int i = 0; i < ans; i++) {
        swap(v[P[i]], v[Q[i]]);
        for (int j = 0; j < N; j++) {
            if (v[j] == scambi[i].fi) X[i] = j;
        }
        for (int j = 0; j < N; j++) {
            if (v[j] == scambi[i].se) Y[i] = j;
        }
        swap(v[X[i]], v[Y[i]]);
    }
    // v = ini;
    // for (int i = 0; i < ans; i++) {
    //     swap(v[P[i]], v[Q[i]]);
    //     swap(v[X[i]], v[Y[i]]);
    // }
    // for (int i = 0; i < N; i++) assert(v[i] == i);
    return ans;
}

Compilation message

sorting.cpp: In constructor 'Dsu::Dsu(std::vector<int>&)':
sorting.cpp:20:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |         for (int i = 0; i < k.size(); i++) {
      |                         ~~^~~~~~~~~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:45:23: warning: declaration of 'N' shadows a global declaration [-Wshadow]
   45 | int findSwapPairs(int N, int S[], int M, int P[], int Q[], int X[], int Y[]) {
      |                   ~~~~^
sorting.cpp:13:5: note: shadowed declaration is here
   13 | int N;
      |     ^
sorting.cpp:63:28: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   63 |         int k = scambi.size();
      |                 ~~~~~~~~~~~^~
sorting.cpp:70:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |         if (k == scambi.size()) break;
      |             ~~^~~~~~~~~~~~~~~~
In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from sorting.cpp:2:
sorting.cpp:72:26: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   72 |     assert(scambi.size() <= ans);
      |            ~~~~~~~~~~~~~~^~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 5 ms 340 KB Output is correct
22 Correct 5 ms 340 KB Output is correct
23 Correct 6 ms 444 KB Output is correct
24 Correct 5 ms 432 KB Output is correct
25 Incorrect 2 ms 340 KB Output isn't correct
26 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 49 ms 392 KB Output is correct
2 Correct 55 ms 380 KB Output is correct
3 Correct 52 ms 376 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 4 ms 404 KB Output is correct
6 Correct 9 ms 396 KB Output is correct
7 Correct 30 ms 400 KB Output is correct
8 Correct 58 ms 396 KB Output is correct
9 Correct 58 ms 388 KB Output is correct
10 Correct 55 ms 384 KB Output is correct
11 Correct 56 ms 388 KB Output is correct
12 Correct 28 ms 408 KB Output is correct
13 Correct 56 ms 396 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 392 KB Output is correct
2 Correct 55 ms 380 KB Output is correct
3 Correct 52 ms 376 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 4 ms 404 KB Output is correct
6 Correct 9 ms 396 KB Output is correct
7 Correct 30 ms 400 KB Output is correct
8 Correct 58 ms 396 KB Output is correct
9 Correct 58 ms 388 KB Output is correct
10 Correct 55 ms 384 KB Output is correct
11 Correct 56 ms 388 KB Output is correct
12 Correct 28 ms 408 KB Output is correct
13 Correct 56 ms 396 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Execution timed out 1080 ms 8052 KB Time limit exceeded
16 Halted 0 ms 0 KB -