제출 #1235967

#제출 시각아이디문제언어결과실행 시간메모리
1235967santi3223정렬하기 (IOI15_sorting)C++20
0 / 100
0 ms328 KiB
#include <bits/stdc++.h>
#include "sorting.h"
using namespace std;
#define ll long long
#define vb vector<bool>
#define pb push_back
#define ff(aa, bb, cc) for(ll aa = bb; aa < cc; aa++)
#define vl vector<ll>
#define pll pair<ll, ll>
#define fi first
#define se second
#define ed "\n"
#define all(aaa) aaa.begin(), aaa.end()
#define rall(aaa) aaa.rbegin(), aaa.rend()

/*
static char _buffer[1024];
static int _currentChar = 0;
static int _charsNumber = 0;
static FILE *_inputFile, *_outputFile;

static inline int _read() {
    if (_charsNumber < 0) {
        exit(1);
    }
    if (!_charsNumber || _currentChar == _charsNumber) {
        _charsNumber = (int)fread(_buffer, sizeof(_buffer[0]), sizeof(_buffer), _inputFile);
        _currentChar = 0;
    }
    if (_charsNumber <= 0) {
        return -1;
    }
    return _buffer[_currentChar++];
}*/

int findSwapPairs(int n, int S[], int M, int X[], int Y[], int P[], int Q[]){
	vl arr(n);
	ff(i, 0, n){
		arr.pb(S[i]);
	}
	map<ll, vl> mp;
	vl sorted = arr;
	sort(all(sorted));
	map<ll, vl> fin;
	ff(i, 0, n){
		if(arr[i] != sorted[i]){
			fin[sorted[i]].pb(i);
		}
	}
	ff(i, 0, n){
		if(arr[i] != sorted[i]){
			mp[arr[i]].pb(i);
		}
	}
	ll c = 0;
	vl a, b;
	ff(i, 0, n){
		if(arr[i] == sorted[i]){
			continue;
		}
		c++;
		ll sup = sorted[i];
		ll aux = 0;
		/*ff(id, 0, n){
			cout << arr[id] << " ";
		}
		cout << ed;
		cout << arr[i] << " " << sorted[i] << ed;*/
		for(auto &p : mp[sup]){
			aux = p;
			if(sorted[p] == arr[i]){
				break;
			}
		}
		//cout << "P " << i << " " << aux << ed;
		for(auto &p : mp[arr[i]]){
			//cout << p << " ";
			if(p == i){
				p = aux;
				break;
			}
		}
		//cout << ed;
		for(auto &p : mp[arr[aux]]){
			//cout << p << " ";
			if(p == aux){
				p = i;
			}
		}
		//cout << ed << ed;
		a.pb(i);
		b.pb(aux);
		swap(arr[i], arr[aux]);
		
	}
	ff(i, 0, a.size()){
		P[i] = a[i];
		Q[i] = b[i];
	}
	return c;
}
/*
static inline int _readInt() {
    int c, x, s;
    c = _read();
    while (c <= 32) c = _read();
    x = 0;
    s = 1;
    if (c == '-') {
        s = -1;
        c = _read();
    }
    while (c > 32) {
        x *= 10;
        x += c - '0';
        c = _read();
    }
    if (s < 0) x = -x;
    return x;
}


int main()
{
	int N, M;
	N = _readInt();
	int *S = (int*)malloc(sizeof(int) * (unsigned int)N);
	for (int i = 0; i < N; ++i)
		S[i] = _readInt();
	M = _readInt();
	int *X = (int*)malloc(sizeof(int) * (unsigned int)M), *Y = (int*)malloc(sizeof(int) * (unsigned int)M);
	for (int i = 0; i < M; ++i) {
	    X[i] = _readInt();
	    Y[i] = _readInt();
	}
	int *P = (int*)malloc(sizeof(int) * (unsigned int)M), *Q = (int*)malloc(sizeof(int) * (unsigned int)M);
	int ans = findSwapPairs(N, S, M, X, Y, P, Q);
	fprintf(_outputFile, "%d\n", ans);
	for (int i = 0; i < ans; ++i)
		fprintf(_outputFile, "%d %d\n", P[i], Q[i]);
	return 0;
}

*/
#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...