Submission #1088571

# Submission time Handle Problem Language Result Execution time Memory
1088571 2024-09-14T15:39:21 Z Pacybwoah Sorting (IOI15_sorting) C++17
0 / 100
1 ms 600 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);
	for(int i = 0; i < n; i++) vec[i] = S[i], pos[S[i]] = 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]);
	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]]]);
	}
	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:64:28: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   64 |   int sz = children[i].size();
      |            ~~~~~~~~~~~~~~~~^~
sorting.cpp:67:21: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   67 |  int tsz = todo.size();
      |            ~~~~~~~~~^~
# 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 Incorrect 0 ms 348 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 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 Incorrect 0 ms 348 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 1 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 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 Incorrect 0 ms 348 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 600 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 600 KB Output isn't correct
2 Halted 0 ms 0 KB -