# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
292034 | mat_v | 정렬하기 (IOI15_sorting) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "soring.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
#define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
#define mod 1000000007
#define xx first
#define yy second
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define ll long long
#define pii pair<int,int>
#define maxn 200005
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>,rb_tree_tag, tree_order_statistics_node_update> ordered_set;/// find_by_order(x)(x+1th) , order_of_key() (strictly less)
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
int n;
int m;
int a[maxn];
int x[maxn];
int y[maxn];
int p[maxn];
int q[maxn];
int tmp[maxn];
int poz[maxn];
bool check(int t){
vector<pii> sols;
ff(i,0,n - 1)tmp[i] = a[i];
ff(i,0,t - 1){
swap(tmp[x[i]], tmp[y[i]]);
}
ff(i,0,n - 1)poz[tmp[i]] = i;
ff(i,0,n - 1){
if(poz[i] == i)continue;
sols.pb({tmp[i], i});
int fr1 = tmp[i];
int fr2 = i;
int t1 = poz[fr1];
int t2 = poz[fr2];
poz[fr2] = t1;
poz[fr1] = t2;
swap(tmp[t1], tmp[t2]);
}
ff(i,0,n - 1)assert(tmp[i] == i);
int kol = sols.size();
//for(auto c:sols)cout << c.xx << " " << c.yy << "\n";
if(kol > t)return 0;
ff(i,kol,t)sols.pb({0,0});
//cout << "ok\n";
ff(i,0,n - 1)tmp[i] = a[i];
ff(i,0,n - 1)poz[tmp[i]] = i;
ff(i,0,t - 1){
/// prvi
int fr1 = tmp[x[i]];
int fr2 = tmp[y[i]];
int t1 = poz[fr1];
int t2 = poz[fr2];
poz[fr2] = t1;
poz[fr1] = t2;
swap(tmp[t1], tmp[t2]);
fr1 = sols[i].xx;
fr2 = sols[i].yy;
//cout << i << " " << sols[i].xx << " " << sols[i].yy << "\n";
t1 = poz[fr1];
t2 = poz[fr2];
p[i] = t1;
q[i] = t2;
poz[fr2] = t1;
poz[fr1] = t2;
swap(tmp[t1], tmp[t2]);
}
//ff(i,0,n - 2)assert(tmp[i] < tmp[i + 1]);
return 1;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
n = N;
m = M;
ff(i,0,m - 1){
p[i] = q[i] = 0;
}
ff(i,0,n - 1){
a[i] = S[i];
}
ff(i,0,m - 1){
x[i] = X[i];
y[i] = Y[i];
}
bool da = 1;
ff(i,0,n - 2){
if(a[i] >= a[i + 1])da = 0;
}
if(da)return 0;
int l = 1;
int r = m;
// return check(3);
//return 1;
while(l < r){
int mid = (l + r) / 2;
if(check(mid)){
r = mid;
}
else l = mid + 1;
}
check(l);
ff(i,0,m - 1){
P[i] = p[i];
Q[i] = q[i];
}
ff(i,0,l - 1){
swap(S[X[i]], S[Y[i]]);
swap(S[P[i]], S[Q[i]]);
}
//ff(i,0,n - 2)assert(S[i] < S[i + 1]);
//assert(l <= m);
return l;
}