Submission #670876

# Submission time Handle Problem Language Result Execution time Memory
670876 2022-12-11T02:28:31 Z LittleCube Sorting (IOI15_sorting) C++17
100 / 100
213 ms 24164 KB
#include "sorting.h"
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;

// 0 1 2    0 1 2
//          0   2
// 0 1      0 1
//   1 2
// 1 2 0    1 2 0

vector<pii> ans;

void dfs(int u, vector<int> &p, vector<int> &vis)
{
	vis[u] = 1;
	if (!vis[p[u]])
	{
		ans.emplace_back(pii(u, p[u]));
		dfs(p[u], p, vis);
	}
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[])
{
	auto check = [&](int K)
	{
		vector<int> v(N), vis(N, 0), pos(N);
		ans.clear();
		for (int i = 0; i < N; i++)
			v[i] = S[i];
		for (int i = 0; i < K; i++)
			swap(v[X[i]], v[Y[i]]);
		for (int i = 0; i < N; i++)
			if (!vis[i])
				dfs(i, v, vis);
		if(ans.size() <= K)
		{
			reverse(ans.begin(), ans.end());
			while(ans.size() <= K)
				ans.emplace_back(pii(0, 0));
			for(int i = 0; i < N; i++)
				pos[i] = i, v[i] = i;
			for(int i = K - 1; i >= 0; i--)
			{
				P[i] = pos[ans[i].F];
				Q[i] = pos[ans[i].S];
				tie(v[X[i]], v[Y[i]], pos[v[X[i]]], pos[v[Y[i]]]) = make_tuple(v[Y[i]], v[X[i]], pos[v[Y[i]]], pos[v[X[i]]]);
			}
			return true;
		}
		else 
			return false;
	};
	int L = 0, R = M;
	while(L < R)
	{
		int mid = (L + R) / 2;
		if(check(mid))
			R = mid;
		else 
			L = mid + 1;
	}
	check(L);
	return L;
}

Compilation message

sorting.cpp: In lambda function:
sorting.cpp:41:17: 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 |   if(ans.size() <= K)
      |      ~~~~~~~~~~~^~~~
sorting.cpp:44: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]
   44 |    while(ans.size() <= K)
      |          ~~~~~~~~~~~^~~~
# 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 0 ms 300 KB Output is correct
4 Correct 0 ms 304 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 300 KB Output is correct
7 Correct 0 ms 296 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 0 ms 300 KB Output is correct
4 Correct 0 ms 304 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 300 KB Output is correct
7 Correct 0 ms 296 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 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 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 1 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 0 ms 300 KB Output is correct
4 Correct 0 ms 304 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 300 KB Output is correct
7 Correct 0 ms 296 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 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 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 308 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 2 ms 596 KB Output is correct
22 Correct 2 ms 724 KB Output is correct
23 Correct 2 ms 724 KB Output is correct
24 Correct 1 ms 688 KB Output is correct
25 Correct 2 ms 600 KB Output is correct
26 Correct 1 ms 596 KB Output is correct
27 Correct 1 ms 688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 500 KB Output is correct
8 Correct 2 ms 504 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 2 ms 440 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 500 KB Output is correct
8 Correct 2 ms 504 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 2 ms 440 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 158 ms 21204 KB Output is correct
16 Correct 177 ms 21876 KB Output is correct
17 Correct 205 ms 22948 KB Output is correct
18 Correct 92 ms 19980 KB Output is correct
19 Correct 175 ms 22304 KB Output is correct
20 Correct 166 ms 22912 KB Output is correct
21 Correct 167 ms 22916 KB Output is correct
22 Correct 143 ms 18312 KB Output is correct
23 Correct 194 ms 23760 KB Output is correct
24 Correct 197 ms 23496 KB Output is correct
25 Correct 201 ms 23136 KB Output is correct
26 Correct 173 ms 23016 KB Output is correct
27 Correct 154 ms 22244 KB Output is correct
28 Correct 185 ms 23320 KB Output is correct
29 Correct 199 ms 22924 KB Output is correct
30 Correct 139 ms 21876 KB Output is correct
31 Correct 212 ms 23544 KB Output is correct
32 Correct 164 ms 23012 KB Output is correct
33 Correct 212 ms 23516 KB Output is correct
34 Correct 194 ms 23380 KB Output is correct
35 Correct 174 ms 21852 KB Output is correct
36 Correct 73 ms 21372 KB Output is correct
37 Correct 213 ms 24164 KB Output is correct
38 Correct 196 ms 23048 KB Output is correct
39 Correct 205 ms 23380 KB Output is correct