# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
785328 | NothingXD | 정렬하기 (IOI15_sorting) | C++17 | 158 ms | 17076 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef complex<ld> point;
void debug_out(){cerr << endl;}
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T){
cout << H << " ";
debug_out(T...);
}
#define debug(...) cerr << "(" << #__VA_ARGS__ << "): ", debug_out(__VA_ARGS__)
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define MP(x, y) make_pair(x, y)
const int maxn = 2e5 + 10;
int n, a[maxn], b[maxn], idx[maxn], x[3*maxn], y[3*maxn];
bool vis[maxn];
bool check(int k){
for (int i = 0; i < n; i++) b[i] = a[i];
for (int i = 0; i < k; i++){
swap(b[x[i]], b[y[i]]);
}
memset(vis, false, sizeof vis);
int cnt = 0;
for (int i = 0; i < n; i++){
if (!vis[i]) cnt++;
int x = i;
while(!vis[x]){
vis[x] = true;
x = b[x];
}
}
return n - cnt <= k;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
n = N;
for (int i = 0; i < n; i++){
a[i] = S[i];
idx[a[i]] = i;
}
for (int i = 0; i < M; i++){
x[i] = X[i];
y[i] = Y[i];
}
int l = -1, r = M;
while(l + 1 < r){
int mid = (l+r) >> 1;
if (check(mid)) r = mid;
else l = mid;
}
for (int i = 0; i < n; i++){
b[i] = a[i];
}
for (int i = 0; i < r; i++){
swap(b[x[i]], b[y[i]]);
}
vector<pii> swp;
memset(vis, false, sizeof vis);
for (int i = 0; i < n; i++){
int x = i;
vector<int> ver;
while(!vis[x]){
ver.push_back(x);
vis[x] = true;
x = b[x];
}
for (int j = 1; j < ver.size(); j++){
swp.push_back(MP(ver[j-1], ver[j]));
}
}
for (int i = 0; i < r; i++){
swap(a[x[i]], a[y[i]]);
swap(idx[a[x[i]]], idx[a[y[i]]]);
if (i < swp.size()){
P[i] = idx[swp[i].F];
Q[i] = idx[swp[i].S];
}
else{
P[i] = Q[i] = 0;
}
swap(a[P[i]], a[Q[i]]);
swap(idx[a[P[i]]], idx[a[Q[i]]]);
}
return r;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |