제출 #393935

#제출 시각아이디문제언어결과실행 시간메모리
393935arwaeystoamnegSorting (IOI15_sorting)C++17
100 / 100
445 ms44036 KiB
// EXPLOSION!
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
#include<chrono>

using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpi;
typedef vector<pair<ll, ll>> vpll;

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)

#define pb push_back
#define mp make_pair
#define rsz resize
#define sz(x) int(x.size())
#define all(x) x.begin(),x.end()
#define f first
#define s second
#define cont continue
#define endl '\n'
//#define ednl '\n'
#define test int testc;cin>>testc;while(testc--)
#define pr(a, b) trav(x,a)cerr << x << b; cerr << endl;
#define message cout << "Hello World" << endl;
const int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 }; // for every grid problem!!
const ll linf = 4000000000000000000LL;
const ll inf = 1000000007;//998244353    

void pv(vi a) { trav(x, a)cout << x << " "; cout << endl; }void pv(vll a) { trav(x, a)cout << x << " "; cout << endl; }void pv(vector<vi>a) {
	F0R(i, sz(a)) { cout << i << endl; pv(a[i]); cout << endl; }
}void pv(vector<vll>a) { F0R(i, sz(a)) { cout << i << endl; pv(a[i]); }cout << endl; }void pv(vector<string>a) { trav(x, a)cout << x << endl; cout << endl; }
void setIO(string s) {
	ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef arwaeystoamneg
	if (sz(s))
	{
		freopen((s + ".in").c_str(), "r", stdin);
		if (s != "test1")
			freopen((s + ".out").c_str(), "w", stdout);
	}
#endif
}
#ifndef arwaeystoamneg 
#include "sorting.h"
#endif
const int MAX = 200005;
int n, m, a[MAX], x[3 * MAX], y[3 * MAX], ne[MAX], v[MAX], temp[MAX];
vpi tempo;
vpi ans;
void dfs(int i)
{
	if (v[i])return;
	v[i] = 1;
	tempo.pb({ ne[i],i });
	dfs(ne[i]);
}
bool check()
{
	F0R(i, m)swap(a[x[i]], a[y[i]]);
	F0R(i, n)ne[a[i]] = i;
	fill(v, v + n, 0);
	int c = 0;
	F0R(i, n)
	{
		if (!v[i])
		{
			tempo.clear();
			c++, dfs(i);
			tempo.pop_back();
			reverse(all(tempo));
			trav(chess, tempo)ans.pb(chess);
		}
	}
	if (n - c > m)return 0;
	while (sz(ans) < m)ans.pb({ 0,0 });
	R0F(i, m)swap(a[x[i]], a[y[i]]);
	F0R(i, n)temp[a[i]] = i;
	vpi t;
	F0R(i, m)
	{
		swap(temp[a[x[i]]], temp[a[y[i]]]);
		swap(a[x[i]], a[y[i]]);
		int j = temp[ans[i].f], k = temp[ans[i].s];
		t.pb({ j,k });
		swap(temp[a[j]], temp[a[k]]);
		swap(a[j], a[k]);
	}
	ans = t;
	return 1;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) 
{
	n = N;
	int l = 0, r = M;
	F0R(i, min(3 * MAX, M))x[i] = X[i], y[i] = Y[i];
	while (l < r)
	{
		m = (l + r) / 2;
		F0R(i, n)a[i] = S[i];
		if (check())r = m;
		else l = m + 1;
		ans.clear();
	}
	m = l;
	F0R(i, n)a[i] = S[i];
	check();
	F0R(i, sz(ans))P[i] = ans[i].f, Q[i] = ans[i].s;
	return l;
}
#ifdef arwaeystoamneg
int main()
{
	setIO("");
	int N, M;
	cin >> N;
	int* S = (int*)malloc(sizeof(int) * (unsigned int)N);
	for (int i = 0; i < N; ++i)
		cin>>S[i];
	cin >> M;
	int* X = (int*)malloc(sizeof(int) * (unsigned int)M), * Y = (int*)malloc(sizeof(int) * (unsigned int)M);
	for (int i = 0; i < M; ++i) {
		cin >> X[i] >> Y[i];
	}
	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);
	cout << ans << endl;
	for (int i = 0; i < ans; ++i)
		cout << P[i] << " " << Q[i] << endl;
	F0R(i, ans)
	{
		swap(S[X[i]], S[Y[i]]);
		swap(S[P[i]], S[Q[i]]);
	}
	F0R(i, n)cout << S[i] << " ";
	return 0;
}


#endif

컴파일 시 표준 에러 (stderr) 메시지

sorting.cpp: In function 'void setIO(std::string)':
sorting.cpp:30:11: warning: unused parameter 'second' [-Wunused-parameter]
   30 | #define s second
      |           ^
sorting.cpp:44:19: note: in expansion of macro 's'
   44 | void setIO(string s) {
      |                   ^
#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...