# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1015319 |
2024-07-06T08:42:16 Z |
우민규(#10889) |
Sorting (IOI15_sorting) |
C++14 |
|
1 ms |
860 KB |
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
// can swap -> (valid swapping)
// can't -> {}
vector<pair<int, int>> can_sort(vector<int> perm, int m, int* x, int* y) {
for (int i = 0; i < m; ++i) swap(perm[x[i]], perm[y[i]]);
// sort perm
vector<pair<int, int>> sorting;
for (int i = 0; i < perm.size(); ++i) {
while (perm[i] != i) {
sorting.push_back({i, perm[i]});
swap(perm[i], perm[perm[i]]);
}
}
if (sorting.size() > m) {
return {};
}
while (sorting.size() < m) sorting.push_back({0, 0});
vector<int> swap_perm(perm.size());
iota(swap_perm.begin(), swap_perm.end(), 0);
for (int i = m - 1; i >= 0; --i) {
swap(swap_perm[x[i]], swap_perm[y[i]]);
}
for (int i = 0; i < m; ++i) {
swap(swap_perm[x[i]], swap_perm[y[i]]);
sorting[i].first = swap_perm[sorting[i].first];
sorting[i].second = swap_perm[sorting[i].second];
}
return sorting;
}
int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[]) {
bool is_zero = true;
for (int i = 0; i < n; ++i)
if (s[i] != i) is_zero = false;
if (is_zero) return 0;
// rl(0) -> not possible
// rh(m) -> possible
int rl = 0, rh = m;
vector<pair<int, int>> pos_sorting = can_sort(vector<int>(s, s + n), m, x, y);
while (rl + 1 < rh) {
int rm = (rl + rh) / 2;
vector<pair<int, int>> sorting = can_sort(vector<int>(s, s + n), rm, x, y);
// not possible
if (sorting.size() == 0) {
rl = rm;
} else {
rh = rm;
pos_sorting = sorting;
}
}
for (int i = 0; i < rh; ++i) {
p[i] = pos_sorting[i].first;
q[i] = pos_sorting[i].second;
}
return rh;
}
#ifndef EVAL
#include "grader.cpp"
#endif
Compilation message
sorting.cpp: In function 'std::vector<std::pair<int, int> > can_sort(std::vector<int>, int, int*, int*)':
sorting.cpp:12:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
12 | for (int i = 0; i < perm.size(); ++i) {
| ~~^~~~~~~~~~~~~
sorting.cpp:18:22: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
18 | if (sorting.size() > m) {
| ~~~~~~~~~~~~~~~^~~
sorting.cpp:21:25: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
21 | while (sorting.size() < m) sorting.push_back({0, 0});
| ~~~~~~~~~~~~~~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 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 |
440 KB |
Output is correct |
5 |
Correct |
1 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 |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
440 KB |
Output is correct |
5 |
Correct |
1 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 |
344 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 |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
500 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 |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
440 KB |
Output is correct |
5 |
Correct |
1 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 |
344 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 |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
500 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Incorrect |
1 ms |
860 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |