# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
552026 | Theo830 | 정렬하기 (IOI15_sorting) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e9+7;
const ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ll,ii>
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
///I hope I will get uprating and don't make mistakes
///I will never stop programming
///sqrt(-1) Love C++
///Please don't hack me
///@TheofanisOrfanou Theo830
///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst)
///Stay Calm
///Look for special cases
///Beware of overflow and array bounds
///Think the problem backwards
///Training
#include "sorting.h"
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
int n = N;
if(is_sorted(S,S+N)){
return 0;
}
ll l = 1,r = m;
ll ans = m;
while(l <= r){
ll mid = (l+r)/2;
int cur[n];
int fin[n];
int pos[n],post[n];
f(i,0,n){
pos[S[i]] = i;
}
f(i,0,n){
cur[i] = S[i];
fin[i] = S[i];
}
f(j,0,mid){
swap(fin[X[j]],fin[Y[j]]);
}
f(i,0,n){
post[fin[i]] = i;
}
ll po = 0;
f(j,0,mid){
swap(pos[cur[X[j]]],pos[cur[Y[j]]]);
swap(cur[X[j]],cur[Y[j]]);
while(po < n && fin[po] == po){
po++;
}
if(po < n){
ll xx = pos[po],yy = pos[fin[po]];
swap(pos[cur[xx]],pos[cur[yy]]);
swap(cur[xx],cur[yy]);
xx = post[po],yy = post[fin[po]];
swap(post[fin[xx]],post[fin[yy]]);
swap(fin[xx],fin[yy]);
}
}
while(po < n && fin[po] == po){
po++;
}
if(pos == n){
r = mid - 1;
ans = min(ans,mid);
}
else{
l = mid + 1;
}
}
int cur[n];
int fin[n];
int pos[n],post[n];
f(i,0,n){
pos[S[i]] = i;
}
f(i,0,n){
cur[i] = S[i];
fin[i] = S[i];
}
f(j,0,ans){
swap(fin[X[j]],fin[Y[j]]);
}
f(i,0,n){
post[fin[i]] = i;
}
ll po = 0;
f(j,0,ans){
swap(pos[cur[X[j]]],pos[cur[Y[j]]]);
swap(cur[X[j]],cur[Y[j]]);
while(po < n && fin[po] == po){
po++;
}
if(po < n){
ll xx = pos[po],yy = pos[fin[po]];
P[j] = xx,Q[j] = yy;
swap(pos[cur[xx]],pos[cur[yy]]);
swap(cur[xx],cur[yy]);
xx = post[po],yy = post[fin[po]];
swap(post[fin[xx]],post[fin[yy]]);
swap(fin[xx],fin[yy]);
}
else{
P[j] = Q[j] = 0;
}
}
return ans;
}