Submission #587514

# Submission time Handle Problem Language Result Execution time Memory
587514 2022-07-02T04:07:36 Z benson1029 Sorting (IOI15_sorting) C++14
Compilation error
0 ms 0 KB
#include "sorting.h"
#include<bits/stdc++.h>
using namespace std;

int a[200005], b[200005], s[200005];

void swapa(int x, int y) {
	b[a[x]] = y;
	b[a[y]] = x;
	if(st.find(x)==st.end() && st.find(y)!=st.end()) {
		st.erase(y);
		st.insert(x);
	}
	else if(st.find(x)!=st.end() && st.find(y)==st.end()) {
		st.erase(x);
		st.insert(y);
	}
	swap(a[x], a[y]);
}

vector< pair<int,int> > v;
set<int> st;

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
	int l = 0, r = N, mid;

	while(l <= r) {
		mid = (l+r)/2;
		
		for(int i=0; i<N; i++) {
			a[i] = i;
			s[i] = S[i];
		}
		for(int i=mid-1; i>=0; i--) {
			swap(a[X[i]], a[Y[i]]);
		}
		st.clear();
		for(int i=0; i<N; i++) {
			if(a[i] != s[i]) st.insert(i);
			b[a[i]] = i;
		}
		v.clear();

		for(int i=0; i<mid; i++) {
			swapa(X[i], Y[i]);
			swap(s[X[i]], s[Y[i]]);
			if(!st.empty()) {
				int j = *st.begin();
				int k = b[s[j]];
				swap(s[j], s[k]);
				if(s[j] == a[j]) st.erase(j);
				if(s[k] == a[k]) st.erase(k);
			}
			v.push_back({0, 0});
			end:;
		}

		if(l==r) {
			for(int i=0; i<v.size(); i++) P[i] = v[i].first, Q[i] = v[i].second;
			return v.size();
		} else {
			bool ok = true;
			for(int i=0; i<N; i++) if(s[i] != i) ok = false;

			if(ok) r = mid;
			else l = mid+1;
		}
		
	}

}


Compilation message

sorting.cpp: In function 'void swapa(int, int)':
sorting.cpp:10:5: error: 'st' was not declared in this scope; did you mean 's'?
   10 |  if(st.find(x)==st.end() && st.find(y)!=st.end()) {
      |     ^~
      |     s
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:59:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |    for(int i=0; i<v.size(); i++) P[i] = v[i].first, Q[i] = v[i].second;
      |                 ~^~~~~~~~~
sorting.cpp:60:17: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   60 |    return v.size();
      |           ~~~~~~^~
sorting.cpp:55:4: warning: label 'end' defined but not used [-Wunused-label]
   55 |    end:;
      |    ^~~
sorting.cpp:24:39: warning: unused parameter 'M' [-Wunused-parameter]
   24 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
      |                                   ~~~~^
sorting.cpp:71:1: warning: control reaches end of non-void function [-Wreturn-type]
   71 | }
      | ^