Submission #1042007

# Submission time Handle Problem Language Result Execution time Memory
1042007 2024-08-02T12:04:53 Z c2zi6 Sorting (IOI15_sorting) C++14
100 / 100
212 ms 23848 KB
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define replr(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define reprl(i, a, b) for (int i = int(a); i >= int(b); --i)
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define mkp(a, b) make_pair(a, b)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VPI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<VPI> VVPI;
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;
typedef vector<PLL> VPL;
typedef vector<VL> VVL;
typedef vector<VVL> VVVL;
typedef vector<VPL> VVPL;
template<class T> T setmax(T& a, T b) {if (a < b) return a = b; return a;}
template<class T> T setmin(T& a, T b) {if (a < b) return a; return a = b;}
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<typename T>
using indset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#include "sorting.h"

#define ANSWER {\
        rep(i, ans.size()) {\
            P[i] = ans[i].ff;\
            Q[i] = ans[i].ss;\
        }\
        return ans.size();\
    }

struct PERM {
    int n;
    VI a;
    VI ind;
    PERM(int N_ARG) : n(N_ARG) {
        a = ind = VI(n);
        rep(i, n) a[i] = ind[i] = i;
    }
    PERM(VI A_ARG) : a(A_ARG) {
        n = a.size();
        ind = VI(n);
        rep(i, n) ind[a[i]] = i;
    }
    int size() {
        return n;
    }
    int& operator[](const int i) {
        return a[i];
    }
};

PERM operator*(const PERM& perm, const PII p) {
    auto[x, y] = p;
    PERM ret = perm;
    swap(ret.ind[ret[x]], ret.ind[ret[y]]);
    swap(ret[x], ret[y]);
    return ret;
}
void operator*=(PERM& perm, const PII p) {
    auto[x, y] = p;
    swap(perm.ind[perm[x]], perm.ind[perm[y]]);
    swap(perm[x], perm[y]);
}

VPI ans;
bool f(const VI& A_ARG, const VPI& op) {
    ans.clear();
    PERM a(A_ARG);
    int n = a.size();
    int m = op.size();
    PERM cur(n);
    reprl(i, m-1, 0) cur *= op[i];
    int i = 0;
    for (const PII& p : op) {
        cur *= p;
        a *= p;
        while (i < n && a.ind[i] == cur.ind[i]) i++;
        int x, y;
        if (i == n) {
            x = y = 0;
        } else {
            x = a.ind[i];
            y = cur.ind[i];
        }
        ans.pb({x, y});
        a *= mkp(x, y);
    }
    rep(i, n) if (a[i] != i) return false;
    return true;
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    int n = N;
    int m = min(M, N);
    VI a(n);
    rep(i, n) a[i] = S[i];
    VPI op(m);
    rep(i, m) op[i] = mkp(X[i], Y[i]);

    auto func = [&](int x) {
        VPI curop;
        rep(i, x) curop.pb(op[i]);
        return f(a, curop);
    };

    int l = -1, r = m;
    while (l + 1 < r) {
        int m = (l + r) / 2;
        if (func(m)) r = m;
        else l = m;
    }
    func(r);
    ANSWER

    /*rep(i, m+1) {*/
    /*    if (f(a, curop)) {*/
    /*        ANSWER*/
    /*    }*/
    /*    if (i == m) break;*/
    /*    curop.pb(op[i]);*/
    /*}*/
    /*ANSWER;*/
}


Compilation message

sorting.cpp: In constructor 'PERM::PERM(VI)':
sorting.cpp:51:19: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   51 |         n = a.size();
      |             ~~~~~~^~
sorting.cpp: In function 'PERM operator*(const PERM&, PII)':
sorting.cpp:64:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   64 |     auto[x, y] = p;
      |         ^
sorting.cpp: In function 'void operator*=(PERM&, PII)':
sorting.cpp:71:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   71 |     auto[x, y] = p;
      |         ^
sorting.cpp: In function 'bool f(const VI&, const VPI&)':
sorting.cpp:81:20: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   81 |     int m = op.size();
      |             ~~~~~~~^~
sorting.cpp:99:9: warning: declaration of 'i' shadows a previous local [-Wshadow]
   99 |     rep(i, n) if (a[i] != i) return false;
      |         ^
sorting.cpp:9:28: note: in definition of macro 'rep'
    9 | #define rep(i, n) for (int i = 0; i < int(n); ++i)
      |                            ^
sorting.cpp:84:9: note: shadowed declaration is here
   84 |     int i = 0;
      |         ^
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:119:13: warning: declaration of 'm' shadows a previous local [-Wshadow]
  119 |         int m = (l + r) / 2;
      |             ^
sorting.cpp:105:9: note: shadowed declaration is here
  105 |     int m = min(M, N);
      |         ^
sorting.cpp:39:24: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   39 |         return ans.size();\
      |                ~~~~~~~~^~
sorting.cpp:124:5: note: in expansion of macro 'ANSWER'
  124 |     ANSWER
      |     ^~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 600 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 600 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 432 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 440 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 440 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 134 ms 13812 KB Output is correct
16 Correct 169 ms 21680 KB Output is correct
17 Correct 201 ms 22792 KB Output is correct
18 Correct 62 ms 20988 KB Output is correct
19 Correct 133 ms 22100 KB Output is correct
20 Correct 172 ms 23220 KB Output is correct
21 Correct 158 ms 23436 KB Output is correct
22 Correct 138 ms 18108 KB Output is correct
23 Correct 154 ms 23544 KB Output is correct
24 Correct 206 ms 23224 KB Output is correct
25 Correct 203 ms 22984 KB Output is correct
26 Correct 144 ms 22968 KB Output is correct
27 Correct 132 ms 22372 KB Output is correct
28 Correct 196 ms 23104 KB Output is correct
29 Correct 184 ms 22676 KB Output is correct
30 Correct 101 ms 22468 KB Output is correct
31 Correct 188 ms 23148 KB Output is correct
32 Correct 150 ms 22764 KB Output is correct
33 Correct 201 ms 23208 KB Output is correct
34 Correct 183 ms 23140 KB Output is correct
35 Correct 135 ms 21716 KB Output is correct
36 Correct 56 ms 22464 KB Output is correct
37 Correct 212 ms 23848 KB Output is correct
38 Correct 193 ms 23000 KB Output is correct
39 Correct 211 ms 23084 KB Output is correct