Submission #1088584

# Submission time Handle Problem Language Result Execution time Memory
1088584 2024-09-14T15:52:50 Z Pacybwoah Sorting (IOI15_sorting) C++17
100 / 100
209 ms 29816 KB
#include "sorting.h"
#include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#include<cassert>
using namespace std;
namespace{
	struct DSU{
		vector<int> dsu, sz;
		DSU(int n){
			dsu.resize(n + 1);
			sz.resize(n + 1);
			for(int i = 0; i <= n; i++) dsu[i] = i, sz[i] = 1;
		}
		int query(int x){
			if(x == dsu[x]) return x;
			dsu[x] = query(dsu[x]);
			return dsu[x];
		}
		void unite(int a, int b){
			a = query(a);
			b = query(b);
			if(a == b) return;
			if(sz[a] < sz[b]) swap(a, b);
			dsu[b] = a;
			sz[a] += sz[b];
		}
	};
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
	int n = N;
	vector<int> pos(n);
	for(int i = 0; i < n; i++) pos[S[i]] = i;
    int l = 0, r = M;
	while(l < r){
		int mid = (l + r) >> 1;
		DSU dsu(n);
		vector<int> vec(n);
		for(int i = 0; i < n; i++) vec[i] = S[i];
		for(int i = 0; i < mid; i++) swap(vec[X[i]], vec[Y[i]]);
		for(int i = 0; i < n; i++) dsu.unite(vec[i], i);
		vector<bool> vis(n);
		int cc = 0;
		for(int i = 0; i < n; i++){
			if(!vis[dsu.query(i)]){
				vis[dsu.query(i)] = 1;
				cc++;
			}
		}
		if(n - cc <= mid) r = mid;
		else l = mid + 1;
	}
	DSU dsu(n);
	vector<int> vec(n), s(n), finpos(n);
	for(int i = 0; i < n; i++) vec[i] = S[i], pos[S[i]] = i, s[i] = S[i];
	for(int i = 0; i < l; i++) swap(s[X[i]], s[Y[i]]);
	for(int i = 0; i < n; i++) dsu.unite(i, s[i]);
	for(int i = 0; i < n; i++) finpos[s[i]] = i;
	vector<vector<int>> children(n);
	for(int i = 0; i < n; i++) children[dsu.query(i)].push_back(i);
	vector<int> todo;
	for(int i = 0; i < n; i++){
		int sz = children[i].size();
		for(int j = 0; j < sz - 1; j++) todo.push_back(children[i][j]);
	}
	int tsz = todo.size();
	for(int i = 0; i < l; i++){
		if(i >= tsz){
			P[i] = 0;
			Q[i] = 0;
			continue;
		}
		swap(vec[X[i]], vec[Y[i]]);
		swap(pos[vec[X[i]]], pos[vec[Y[i]]]);
		P[i] = pos[todo[i]];
		Q[i] = pos[s[todo[i]]];
		swap(vec[P[i]], vec[Q[i]]);
		swap(pos[vec[P[i]]], pos[vec[Q[i]]]);
		swap(s[finpos[vec[P[i]]]], s[finpos[vec[Q[i]]]]);
		swap(finpos[vec[P[i]]], finpos[vec[Q[i]]]);
	}
	return l;
}
// g++ -std=c++17 -Wall -Wextra -fsanitize=undefined -fsanitize=address -Wshadow -o run sorting.cpp grader.cpp

Compilation message

sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:65:28: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   65 |   int sz = children[i].size();
      |            ~~~~~~~~~~~~~~~~^~
sorting.cpp:68:21: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   68 |  int tsz = todo.size();
      |            ~~~~~~~~~^~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 600 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 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 600 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 344 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 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory 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 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 600 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 344 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 0 ms 348 KB Output is correct
12 Correct 1 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 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 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 604 KB Output is correct
25 Correct 1 ms 600 KB Output is correct
26 Correct 1 ms 704 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 856 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 1 ms 592 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 2 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 1 ms 592 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 856 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
5 Correct 1 ms 592 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 2 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 1 ms 592 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 520 KB Output is correct
15 Correct 162 ms 25240 KB Output is correct
16 Correct 181 ms 25684 KB Output is correct
17 Correct 186 ms 27032 KB Output is correct
18 Correct 92 ms 28028 KB Output is correct
19 Correct 183 ms 28620 KB Output is correct
20 Correct 191 ms 29280 KB Output is correct
21 Correct 185 ms 29052 KB Output is correct
22 Correct 175 ms 22480 KB Output is correct
23 Correct 193 ms 27776 KB Output is correct
24 Correct 195 ms 27572 KB Output is correct
25 Correct 193 ms 27296 KB Output is correct
26 Correct 188 ms 29236 KB Output is correct
27 Correct 166 ms 28828 KB Output is correct
28 Correct 202 ms 27516 KB Output is correct
29 Correct 207 ms 27580 KB Output is correct
30 Correct 138 ms 29124 KB Output is correct
31 Correct 209 ms 28328 KB Output is correct
32 Correct 181 ms 27104 KB Output is correct
33 Correct 191 ms 27604 KB Output is correct
34 Correct 201 ms 27676 KB Output is correct
35 Correct 190 ms 28088 KB Output is correct
36 Correct 71 ms 29816 KB Output is correct
37 Correct 194 ms 28344 KB Output is correct
38 Correct 186 ms 27168 KB Output is correct
39 Correct 195 ms 27244 KB Output is correct