답안 #1092720

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1092720 2024-09-24T20:04:45 Z triplem5ds 정렬하기 (IOI15_sorting) C++14
100 / 100
205 ms 22288 KB
#include "sorting.h"
#include "bits/stdc++.h"

using namespace std;

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {

	vector<pair<int, int>> ans;
	auto check = [&](int prefix) {
		ans.clear();
		vector<int> vec(N), vis(N, 0), pos(N);
		copy(S, S + N, vec.begin());
		for (int i = 0; i < prefix; i++) swap(vec[X[i]], vec[Y[i]]);
		for (int i = 0; i < N; i++) {
			int cur = i;
			vis[cur] = 1;
			vector<pair<int, int>> _5ra;
			while (!vis[vec[cur]])
			{
				_5ra.push_back({ cur,vec[cur] });
				cur = vec[cur];
				vis[cur] = 1;
			}
			reverse(_5ra.begin(), _5ra.end());
			for (auto p : _5ra) ans.push_back(p);
		}
		return ans.size() <= prefix;

		};



	int lo = 0, hi = M;

	while (lo < hi)
	{
		int md = lo + (hi - lo) / 2;

		if (check(md))	hi = md;
		else lo = md + 1;
	}

	check(lo);
	while (ans.size() < lo) ans.push_back({ 0, 0 });
	vector<int> pos(N), vec(N);
	for (int i = 0; i < N; i++) {
		pos[i] = vec[i] = i;
	}
	for (int i = lo - 1; i >= 0; --i) {
		P[i] = pos[ans[i].first];
		Q[i] = pos[ans[i].second];
		swap(pos[vec[X[i]]], pos[vec[Y[i]]]);
		swap(vec[X[i]], vec[Y[i]]);
	}

	return lo;
}


/*
let's say I have an array of pairs(a, b)
let's do all swaps of this array
at the end of the swaps what I have is the initial position and the final position
so I know s[i] will be at the end ss[i] so ideally I want to capture the position of ss[i] and swap i with it
now what happens to this system if I do a swap
initial doesn't change but capture the position of ss[i] if it's i do swap with val1=ss[i], val2=ss[j] then the final permutation also change

so now what I want to swap is pos[ss[j]] and pos[ss[i]] so I swap those as well and just use that along the way


*/

Compilation message

sorting.cpp: In lambda function:
sorting.cpp:27:21: 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 |   return ans.size() <= prefix;
      |          ~~~~~~~~~~~^~~~~~~~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:44:20: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   44 |  while (ans.size() < lo) ans.push_back({ 0, 0 });
      |         ~~~~~~~~~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 436 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 436 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 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 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 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 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 436 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 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 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 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 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 596 KB Output is correct
25 Correct 1 ms 644 KB Output is correct
26 Correct 1 ms 568 KB Output is correct
27 Correct 1 ms 608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 856 KB Output is correct
2 Correct 1 ms 608 KB Output is correct
3 Correct 1 ms 516 KB Output is correct
4 Correct 1 ms 608 KB Output is correct
5 Correct 1 ms 524 KB Output is correct
6 Correct 1 ms 608 KB Output is correct
7 Correct 1 ms 608 KB Output is correct
8 Correct 1 ms 608 KB Output is correct
9 Correct 2 ms 608 KB Output is correct
10 Correct 1 ms 608 KB Output is correct
11 Correct 1 ms 608 KB Output is correct
12 Correct 1 ms 608 KB Output is correct
13 Correct 1 ms 516 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 856 KB Output is correct
2 Correct 1 ms 608 KB Output is correct
3 Correct 1 ms 516 KB Output is correct
4 Correct 1 ms 608 KB Output is correct
5 Correct 1 ms 524 KB Output is correct
6 Correct 1 ms 608 KB Output is correct
7 Correct 1 ms 608 KB Output is correct
8 Correct 1 ms 608 KB Output is correct
9 Correct 2 ms 608 KB Output is correct
10 Correct 1 ms 608 KB Output is correct
11 Correct 1 ms 608 KB Output is correct
12 Correct 1 ms 608 KB Output is correct
13 Correct 1 ms 516 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 153 ms 19676 KB Output is correct
16 Correct 156 ms 20052 KB Output is correct
17 Correct 191 ms 21132 KB Output is correct
18 Correct 69 ms 18596 KB Output is correct
19 Correct 138 ms 20104 KB Output is correct
20 Correct 148 ms 20156 KB Output is correct
21 Correct 163 ms 19488 KB Output is correct
22 Correct 149 ms 14712 KB Output is correct
23 Correct 166 ms 21904 KB Output is correct
24 Correct 172 ms 21580 KB Output is correct
25 Correct 173 ms 21200 KB Output is correct
26 Correct 134 ms 18176 KB Output is correct
27 Correct 129 ms 17656 KB Output is correct
28 Correct 177 ms 21400 KB Output is correct
29 Correct 205 ms 18912 KB Output is correct
30 Correct 100 ms 17804 KB Output is correct
31 Correct 178 ms 19492 KB Output is correct
32 Correct 163 ms 21032 KB Output is correct
33 Correct 168 ms 21560 KB Output is correct
34 Correct 160 ms 19672 KB Output is correct
35 Correct 139 ms 19336 KB Output is correct
36 Correct 44 ms 16792 KB Output is correct
37 Correct 172 ms 22288 KB Output is correct
38 Correct 163 ms 21464 KB Output is correct
39 Correct 164 ms 21604 KB Output is correct