답안 #1092719

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1092719 2024-09-24T20:02:22 Z triplem5ds 정렬하기 (IOI15_sorting) C++14
0 / 100
1 ms 604 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;
			while (!vis[vec[cur]])
			{
				ans.push_back({ cur,vec[cur] });
				cur = vec[cur];
				vis[cur] = 1;
			}
		}
		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:24: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]
   24 |   return ans.size() <= prefix;
      |          ~~~~~~~~~~~^~~~~~~~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:41: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]
   41 |  while (ans.size() < lo) ans.push_back({ 0, 0 });
      |         ~~~~~~~~~~~^~~~
# 결과 실행 시간 메모리 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 Incorrect 0 ms 348 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 Incorrect 0 ms 348 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 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 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 Incorrect 0 ms 348 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 604 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 604 KB Output isn't correct
2 Halted 0 ms 0 KB -