Submission #590901

# Submission time Handle Problem Language Result Execution time Memory
590901 2022-07-06T13:50:07 Z AlperenT Sorting (IOI15_sorting) C++17
0 / 100
2 ms 468 KB
#include <bits/stdc++.h>
#include "sorting.h"

using namespace std;

int swapcnt(vector<int> v){
    int cnt = 0, n = v.size();

    vector<bool> vis(n, false);

    for(int i = 0; i < n; i++){
        if(!vis[i]){
            int x = i;

            while(!vis[x]){
                cnt++;

                vis[x] = true;
                x = v[x];
            }

            cnt--;
        }
    }

    return cnt;
}

int findSwapPairs(int n, int S[], int M, int X[], int Y[], int P[], int Q[]){
	vector<int> v, v2;

	v.assign(n, 0);

	copy(S, S + n, v.begin());

	int l = -1, r = M;

	while(r - l > 1){
		int m = l + (r - l) / 2;

		v2 = v;

		for(int i = 0; i < m; i++) swap(v2[X[i]], v2[Y[i]]);

		if(swapcnt(v2) <= m) r = m;
		else l = m;
	}	

	vector<pair<int, int>> swaps;

	vector<bool> vis(n, false);

	for(int i = 0; i < n; i++){
		if(!vis[i]){
			vis[i] = true;

			int x = i;

			while(!vis[v2[x]]){
				vis[x] = true;

				swaps.push_back({v2[x], v2[v2[x]]});

				x = v2[x];
			}
		}
	}

	while(swaps.size() < r) swaps.push_back({0, 0});

	vector<int> where(n);

	for(int i = 0; i < n; i++) where[v[i]] = i;

	for(int i = 0; i < r; i++){
		swap(where[v[X[i]]], where[v[Y[i]]]);
		swap(v[X[i]], v[Y[i]]);

		P[i] = where[swaps[i].first];
		Q[i] = where[swaps[i].second];
	}

	return r;
}

Compilation message

sorting.cpp: In function 'int swapcnt(std::vector<int>)':
sorting.cpp:7:28: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
    7 |     int cnt = 0, n = v.size();
      |                      ~~~~~~^~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:69: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]
   69 |  while(swaps.size() < r) swaps.push_back({0, 0});
      |        ~~~~~~~~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Incorrect 1 ms 212 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Incorrect 1 ms 212 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 0 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Incorrect 1 ms 212 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 468 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 468 KB Output isn't correct
2 Halted 0 ms 0 KB -