Submission #302041

#TimeUsernameProblemLanguageResultExecution timeMemory
302041BertedSorting (IOI15_sorting)C++14
100 / 100
298 ms36484 KiB
#include "sorting.h"
#include <vector>
#include <iostream>
#define pii pair<int, int>
#define fst first
#define snd second

using namespace std;

int N, A[200001], T[200001], pos[200001]; 
pii S[600001];
bool vis[200001];
vector<int> check;

void DFS(int u)
{
	if (!vis[u])
	{
		vis[u] = 1;
		check.push_back(u);
		DFS(T[u]);
	}
}

vector<pii> getSwaps(int x)
{
	vector<pii> ret;
	for (int i = 0; i < N; i++) {T[i] = A[i]; vis[i] = 0;}
	for (int i = 0; i < x; i++) {swap(T[S[i].fst], T[S[i].snd]);}
	for (int i = 0; i < N; i++)
	{
		DFS(i);
		for (int i = 1; i < check.size(); i++) {ret.push_back({check[i - 1], check[i]});}
		check.clear();
	}
	return ret;
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) 
{
	::N = N;
	for (int i = 0; i < N; i++) {A[i] = S[i]; pos[A[i]] = i;}
	for (int i = 0; i < M; i++) {::S[i] = {X[i], Y[i]};}

	int L = 0, R = M;
	while (L < R)
	{
		int M = L + R >> 1;
		if (getSwaps(M).size() <= M) {R = M;}
		else {L = M + 1;}
	}	

	vector<pii> V = getSwaps(L);
	//for (auto &x : V) {cerr << x.fst << ", " << x.snd << "\n";}

	for (int i = 0; i < L; i++)
	{
		swap(pos[A[X[i]]], pos[A[Y[i]]]);
		swap(A[X[i]], A[Y[i]]);
		if (i < V.size())
		{
			P[i] = pos[V[i].fst]; Q[i] = pos[V[i].snd];
			swap(A[pos[V[i].fst]], A[pos[V[i].snd]]);
			swap(pos[V[i].fst], pos[V[i].snd]);
		}
		else {P[i] = Q[i] = 0;}
	}

	return L;
}


Compilation message (stderr)

sorting.cpp: In function 'std::vector<std::pair<int, int> > getSwaps(int)':
sorting.cpp:33:12: warning: declaration of 'i' shadows a previous local [-Wshadow]
   33 |   for (int i = 1; i < check.size(); i++) {ret.push_back({check[i - 1], check[i]});}
      |            ^
sorting.cpp:30:11: note: shadowed declaration is here
   30 |  for (int i = 0; i < N; i++)
      |           ^
sorting.cpp:33:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |   for (int i = 1; i < check.size(); i++) {ret.push_back({check[i - 1], check[i]});}
      |                   ~~^~~~~~~~~~~~~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:39:76: warning: declaration of 'S' shadows a global declaration [-Wshadow]
   39 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[])
      |                                                                            ^
sorting.cpp:11:5: note: shadowed declaration is here
   11 | pii S[600001];
      |     ^
sorting.cpp:39:76: warning: declaration of 'N' shadows a global declaration [-Wshadow]
   39 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[])
      |                                                                            ^
sorting.cpp:10:5: note: shadowed declaration is here
   10 | int N, A[200001], T[200001], pos[200001];
      |     ^
sorting.cpp:48:7: warning: declaration of 'int M' shadows a parameter [-Wshadow]
   48 |   int M = L + R >> 1;
      |       ^
sorting.cpp:39:39: note: shadowed declaration is here
   39 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[])
      |                                   ~~~~^
sorting.cpp:48:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   48 |   int M = L + R >> 1;
      |           ~~^~~
sorting.cpp:49:26: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   49 |   if (getSwaps(M).size() <= M) {R = M;}
      |       ~~~~~~~~~~~~~~~~~~~^~~~
sorting.cpp:60:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |   if (i < V.size())
      |       ~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...