# |
제출 시각 |
아이디 |
문제 |
언어 |
결과 |
실행 시간 |
메모리 |
1015316 |
2024-07-06T08:40:30 Z |
우민규(#10889) |
정렬하기 (IOI15_sorting) |
C++14 |
|
1 ms |
532 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) {
swap(perm[i], perm[perm[i]]);
sorting.push_back({i, 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});
| ~~~~~~~~~~~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
532 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
532 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |