Submission #707037

# Submission time Handle Problem Language Result Execution time Memory
707037 2023-03-08T10:15:20 Z marvinthang Sorting (IOI15_sorting) C++17
100 / 100
197 ms 12476 KB
#include "sorting.h"
#include <bits/stdc++.h>

using namespace std;

#define                  fi  first
#define                  se  second
#define           REP(i, n)  for (int i = 0, _n = (n); i < _n; ++i)

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    auto check = [&] (int r, bool trace = false) {
    	vector <int> perm(S, S + N);
    	REP(i, r) swap(perm[X[i]], perm[Y[i]]);
    	vector <int> pos(N);
    	REP(i, N) pos[perm[i]] = i;
    	vector <pair <int, int>> swaps;
    	auto __swap = [&] (int i, int j) {
			swap(perm[i], perm[j]);
			swap(pos[perm[i]], pos[perm[j]]);
    	};
    	REP(i, N) {
    		if (perm[i] != i) {
    			swaps.push_back(make_pair(perm[i], i));
    			__swap(i, pos[i]);
    		}
    	}
    	if (!trace) return swaps.size() <= r;
    	while (swaps.size() < r) swaps.push_back(make_pair(0, 0));
    	copy(S, S + N, perm.begin());
    	REP(i, N) pos[perm[i]] = i;
    	REP(i, r) {
    		__swap(X[i], Y[i]);
    		P[i] = pos[swaps[i].fi];
    		Q[i] = pos[swaps[i].se];
    		__swap(P[i], Q[i]);
    	}
    	return true;
    };
    int l = 0, r = N;
    while (l < r) {
    	int m = l + r >> 1;
    	if (check(m)) r = m;
    	else l = m + 1;
    }
    check(l, true);
    return l;
}

Compilation message

sorting.cpp: In lambda function:
sorting.cpp:27:38: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   27 |      if (!trace) return swaps.size() <= r;
      |                         ~~~~~~~~~~~~~^~~~
sorting.cpp:28: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]
   28 |      while (swaps.size() < r) swaps.push_back(make_pair(0, 0));
      |             ~~~~~~~~~~~~~^~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:41:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   41 |      int m = l + r >> 1;
      |              ~~^~~
sorting.cpp:10:39: warning: unused parameter 'M' [-Wunused-parameter]
   10 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                                   ~~~~^
# 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 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 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 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 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 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 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 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 316 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 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 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 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 316 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 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 1 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 2 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 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 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 1 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 2 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 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 165 ms 11296 KB Output is correct
16 Correct 170 ms 11584 KB Output is correct
17 Correct 195 ms 11908 KB Output is correct
18 Correct 56 ms 7876 KB Output is correct
19 Correct 131 ms 9620 KB Output is correct
20 Correct 134 ms 11356 KB Output is correct
21 Correct 131 ms 11428 KB Output is correct
22 Correct 163 ms 12084 KB Output is correct
23 Correct 188 ms 12296 KB Output is correct
24 Correct 194 ms 12192 KB Output is correct
25 Correct 192 ms 12112 KB Output is correct
26 Correct 131 ms 11416 KB Output is correct
27 Correct 136 ms 9564 KB Output is correct
28 Correct 186 ms 12128 KB Output is correct
29 Correct 183 ms 11708 KB Output is correct
30 Correct 90 ms 9200 KB Output is correct
31 Correct 177 ms 11836 KB Output is correct
32 Correct 197 ms 12016 KB Output is correct
33 Correct 197 ms 12208 KB Output is correct
34 Correct 179 ms 12128 KB Output is correct
35 Correct 135 ms 9652 KB Output is correct
36 Correct 49 ms 7280 KB Output is correct
37 Correct 196 ms 12476 KB Output is correct
38 Correct 182 ms 12056 KB Output is correct
39 Correct 192 ms 12164 KB Output is correct