Submission #712545

# Submission time Handle Problem Language Result Execution time Memory
712545 2023-03-19T06:53:58 Z Nhoksocqt1 Sorting (IOI15_sorting) C++17
20 / 100
488 ms 2316 KB
#include<bits/stdc++.h>
using namespace std;

#define inf 0x3f3f3f3f
#define sz(x) int((x).size())
#define fi first
#define se second
#define round asfhuqfuqwhuqwer
typedef long long ll;
typedef pair<int, int> ii;

template<class X, class Y>
	inline bool maximize(X &x, const Y &y) {return (x < y ? x = y, 1 : 0);}
template<class X, class Y>
	inline bool minimize(X &x, const Y &y) {return (x > y ? x = y, 1 : 0);}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
    return uniform_int_distribution<int>(l, r)(rng);
}

const int MAXN = 200005;

ii round[MAXN];
int val[MAXN], P[6 * MAXN][21], Pn[6 * MAXN][21], firstPn[MAXN];
int lastIdx[MAXN], pos[MAXN], tmp[MAXN], nArr, numRound;

#ifdef Nhoksocqt1

int findSwapPairs(int _N, int _S[], int _M, int _X[], int _Y[], int _P[], int _Q[]);
int _S[MAXN], _X[MAXN], _Y[MAXN], _P[MAXN], _Q[MAXN];

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    freopen("SORTING.inp", "r", stdin);
    freopen("SORTING.out", "w", stdout);

    int _N, _M;
    cin >> _N;
    for (int i = 0; i < _N; ++i)
        cin >> _S[i];

    cin >> _M;
    for (int i = 0; i < _M; ++i)
        cin >> _X[i];

    for (int i = 0; i < _M; ++i)
        cin >> _Y[i];

    int ans = findSwapPairs(_N, _S, _M, _X, _Y, _P, _Q);
    cout << ans << '\n';
    for (int it = 0; it < ans; ++it)
        cout << _P[it] << ' ' << _Q[it] << '\n';

    return 0;
}

#endif // Nhoksocqt1

inline int getID(int x) {
    return (x > numRound) ? round[x - numRound].fi : round[x].se;
}

bool check(int nr, int _P[], int _Q[]) {
    for (int i = 1; i <= nArr; ++i) {
        tmp[i] = val[i];
        pos[val[i]] = i;
    }

    int i(1), cntNow(0);
    while(i <= nArr) {
        int to(firstPn[i]);
        for (int j = 20; j >= 0; --j) {
            int now(Pn[to][j]);
            if(now <= numRound && now <= cntNow || now > numRound && now - numRound <= cntNow)
                to = now;
        }

        if(to <= numRound && to <= cntNow || to > numRound && to - numRound <= cntNow)
            to = Pn[to][0];

        int f(to);
        for (int j = 20; j >= 0; --j) {
            int now(P[to][j]);
            if(now <= numRound && now <= nr || now > numRound && now - numRound <= nr)
                to = now;
        }

        //cout << nr << ' ' << i << ' ' << firstPn[i] << ' ' << to << ' ' << getID(to) << '\n';
        bool checkCond = (to <= numRound && to <= nr || to > numRound && to - numRound <= nr);
        if(checkCond && tmp[i] == getID(to) || !checkCond && tmp[i] == i) {
            ++i;
            continue;
        }

        ++cntNow;
        swap(pos[tmp[round[cntNow].fi]], pos[tmp[round[cntNow].se]]);
        swap(tmp[round[cntNow].fi], tmp[round[cntNow].se]);
        if(cntNow > nr)
            return false;

        int x, y;
        if(checkCond) {
            x = pos[getID(to)], y = i;
        } else {
            x = pos[i], y = i;
        }

        if(round[cntNow].fi == y) {
            y = round[cntNow].se;
        } else
            if(round[cntNow].se == y) {
                y = round[cntNow].fi;
            }

        _P[cntNow - 1] = x - 1, _Q[cntNow - 1] = i - 1;
        swap(pos[tmp[x]], pos[tmp[i]]);
        swap(tmp[x], tmp[i]);
    }

    for (int i = cntNow; i < nr; ++i)
        _P[i] = _Q[i] = 0;

    return true;
}

int findSwapPairs(int _N, int _S[], int _M, int _X[], int _Y[], int _P[], int _Q[]) {
    nArr = _N, numRound = _M;

    bool check0(1);
    for (int i = 1; i <= nArr; ++i) {
        val[i] = _S[i - 1] + 1;
        check0 &= (val[i] == i);
    }

    if(check0)
        return 0;

    for (int i = 1; i <= 2 * numRound; ++i)
        P[i][0] = Pn[i][0] = 2 * numRound + 1;

    for (int i = 1; i <= numRound; ++i) {
        round[i] = {_X[i - 1] + 1, _Y[i - 1] + 1};
        P[lastIdx[round[i].fi]][0] = i;
        P[lastIdx[round[i].se]][0] = i + numRound;
        lastIdx[round[i].se] = i;
        lastIdx[round[i].fi] = i + numRound;
    }

    for (int i = 1; i <= nArr; ++i)
        lastIdx[i] = 0;

    for (int i = 1; i <= numRound; ++i) {
        Pn[lastIdx[round[i].fi]][0] = i;
        Pn[lastIdx[round[i].se]][0] = i + numRound;
        lastIdx[round[i].fi] = i;
        lastIdx[round[i].se] = i + numRound;
        if(!firstPn[round[i].fi])
            firstPn[round[i].fi] = i;

        if(!firstPn[round[i].se])
            firstPn[round[i].se] = i + numRound;
    }

    for (int j = 0; j <= 20; ++j) {
        P[0][j] = Pn[0][j] = 2 * numRound + 1;
        P[2 * numRound + 1][j] = Pn[2 * numRound + 1][j] = 2 * numRound + 1;
        if(j > 0) {
            for (int i = 1; i <= 2 * numRound; ++i) {
                P[i][j] = P[P[i][j - 1]][j - 1];
                Pn[i][j] = Pn[Pn[i][j - 1]][j - 1];
            }
        }
    }

    for (int i = 1; i <= numRound; ++i) {
        if(check(i, _P, _Q))
            return i;
    }

    return 0;
}

Compilation message

sorting.cpp: In function 'bool check(int, int*, int*)':
sorting.cpp:77:32: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   77 |             if(now <= numRound && now <= cntNow || now > numRound && now - numRound <= cntNow)
      |                ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
sorting.cpp:81:27: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   81 |         if(to <= numRound && to <= cntNow || to > numRound && to - numRound <= cntNow)
      |            ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
sorting.cpp:87:32: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   87 |             if(now <= numRound && now <= nr || now > numRound && now - numRound <= nr)
      |                ~~~~~~~~~~~~~~~~^~~~~~~~~~~~
sorting.cpp:92:42: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   92 |         bool checkCond = (to <= numRound && to <= nr || to > numRound && to - numRound <= nr);
      |                           ~~~~~~~~~~~~~~~^~~~~~~~~~~
sorting.cpp:93:22: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   93 |         if(checkCond && tmp[i] == getID(to) || !checkCond && tmp[i] == i) {
      |            ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
sorting.cpp:84:13: warning: unused variable 'f' [-Wunused-variable]
   84 |         int f(to);
      |             ^
sorting.cpp:123:14: warning: declaration of 'i' shadows a previous local [-Wshadow]
  123 |     for (int i = cntNow; i < nr; ++i)
      |              ^
sorting.cpp:72:9: note: shadowed declaration is here
   72 |     int i(1), cntNow(0);
      |         ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 3 ms 1364 KB Output is correct
11 Correct 3 ms 1348 KB Output is correct
12 Correct 3 ms 1364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 312 KB Output is correct
2 Incorrect 3 ms 340 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 3 ms 1364 KB Output is correct
11 Correct 3 ms 1348 KB Output is correct
12 Correct 3 ms 1364 KB Output is correct
13 Correct 1 ms 312 KB Output is correct
14 Incorrect 3 ms 340 KB Output isn't correct
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 488 ms 2316 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 488 ms 2316 KB Output isn't correct
2 Halted 0 ms 0 KB -