Submission #432615

# Submission time Handle Problem Language Result Execution time Memory
432615 2021-06-18T11:38:15 Z 2qbingxuan Sorting (IOI15_sorting) C++17
56 / 100
1000 ms 19172 KB
#include "sorting.h"
#include <bits/stdc++.h>
#ifdef local
#define debug(a...) qqbx(#a, a)
#define pary(a...) danb(#a, a)
#define safe std::cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
template <typename ...T> void qqbx(const char *s, T ...a) {
    int cnt = sizeof...(T);
    ((std::cerr << "\e[1;32m(" << s << ") = ("), ..., (std::cerr << a << (--cnt ? ", " : ")\e[0m\n")));
}
template <typename T> void danb(const char *s, T L, T R) {
    std::cerr << "\e[1;32m[ " << s << " ] = [ ";
    for (int f = 0; L != R; ++L)
        std::cerr << (f++ ? ", " : "") << *L;
    std::cerr << " ]\e[0m\n";
}
#else
#define debug(...) ((void)0)
#define pary(...) ((void)0)
#define safe ((void)0)
#endif // local
#define all(v) begin(v),end(v)

using namespace std;

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    if (is_sorted(S, S+N)) return 0;
    vector<pair<int,int>> best(M, pair<int,int>(-1, -1));
    for (int R = 1; R <= M; R++) {
        vector<int> targ(N);
        for (int i = 0; i < N; i++) targ[i] = i;
        for (int i = R-1; i >= 0; i--)
            swap(targ[X[i]], targ[Y[i]]);

        vector<int> s(S, S+N);
        vector<int> pos(N);
        for (int i = 0; i < N; i++)
            pos[s[i]] = i;
        vector<pair<int,int>> ans;
        for (int i = 0, it = 0; i < R; i++) {
            {
                int a = X[i], b = Y[i];
                swap(targ[a], targ[b]);
                swap(pos[s[a]], pos[s[b]]);
                swap(s[a], s[b]);
            }
            {
                while (it < N && pos[it] == pos[targ[pos[it]]]) ++it;
                if (it == N) {
                    ans.emplace_back(0, 0);
                } else {
                    ;
                    int a = pos[it];
                    int b = pos[targ[a]];
                    swap(pos[s[a]], pos[s[b]]);
                    swap(s[a], s[b]);
                    ans.emplace_back(a, b);
                }
            }
        }
        // pary(all(s));
        if (is_sorted(all(s))) {
            if (best.size() > ans.size())
                best = ans;
        }
    }
    for (size_t i = 0; i < best.size(); i++)
        tie(P[i], Q[i]) = best[i];
    return best.size();
}

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:69:21: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   69 |     return best.size();
      |            ~~~~~~~~~^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 69 ms 376 KB Output is correct
11 Correct 72 ms 372 KB Output is correct
12 Correct 69 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 68 ms 360 KB Output is correct
4 Correct 72 ms 372 KB Output is correct
5 Correct 73 ms 332 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 69 ms 376 KB Output is correct
11 Correct 72 ms 372 KB Output is correct
12 Correct 69 ms 384 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 68 ms 360 KB Output is correct
16 Correct 72 ms 372 KB Output is correct
17 Correct 73 ms 332 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Execution timed out 1071 ms 744 KB Time limit exceeded
22 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 397 ms 452 KB Output is correct
2 Correct 478 ms 620 KB Output is correct
3 Correct 437 ms 552 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 508 ms 556 KB Output is correct
6 Correct 486 ms 564 KB Output is correct
7 Correct 542 ms 516 KB Output is correct
8 Correct 472 ms 600 KB Output is correct
9 Correct 479 ms 560 KB Output is correct
10 Correct 480 ms 504 KB Output is correct
11 Correct 492 ms 580 KB Output is correct
12 Correct 519 ms 500 KB Output is correct
13 Correct 535 ms 572 KB Output is correct
14 Correct 315 ms 624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 397 ms 452 KB Output is correct
2 Correct 478 ms 620 KB Output is correct
3 Correct 437 ms 552 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 508 ms 556 KB Output is correct
6 Correct 486 ms 564 KB Output is correct
7 Correct 542 ms 516 KB Output is correct
8 Correct 472 ms 600 KB Output is correct
9 Correct 479 ms 560 KB Output is correct
10 Correct 480 ms 504 KB Output is correct
11 Correct 492 ms 580 KB Output is correct
12 Correct 519 ms 500 KB Output is correct
13 Correct 535 ms 572 KB Output is correct
14 Correct 315 ms 624 KB Output is correct
15 Execution timed out 1077 ms 19172 KB Time limit exceeded
16 Halted 0 ms 0 KB -