# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
257843 | shrek12357 | Sorting (IOI15_sorting) | C++14 | 912 ms | 524292 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <climits>
#include <cmath>
#include <fstream>
#include <queue>
#include "sorting.h"
using namespace std;
vector<pair<int, int>> toChange;
vector<int> xAns, yAns;
bool check(int mid, int nums[], int n, int x[], int y[]) {
for (int i = 0; i < mid; i++) {
int a = nums[x[i]];
nums[x[i]] = nums[y[i]];
nums[y[i]] = a;
}
int adjList[n + 5]; // can optimize later
for (int i = 0; i < n; i++) {
adjList[i] = nums[i];
}
int counter = 0;
bool vis[n + 5];
for (int i = 0; i < n; i++) {
vis[i] = false;
}
for (int i = 0; i < n; i++) {
if (vis[i]) {
continue;
}
//DFS because I'm too lazy to make a function
vector<int> arr;
vis[i] = true;
int cur = adjList[i];
while (cur != i) {
arr.push_back(cur);
vis[cur] = true;
cur = adjList[cur];
}
arr.push_back(cur);
//process the stuff in backwards order so change is correct
for (int j = arr.size() - 1; j > 0; j--) {
toChange.push_back(make_pair(arr[0], arr[j]));
}
counter++;
}
return (n - counter <= mid);
}
void getAns(int mid, int nums[], int n, int x[], int y[]) {
map<int, int> pos;
for (int i = 0; i < n; i++) {
pos[nums[i]] = i;
}
for (int i = 0; i < toChange.size(); i++) {
int a = x[i], b = y[i];
swap(nums[a], nums[b]);
pos[nums[a]] = a;
pos[nums[b]] = b;
int c = toChange[i].first, d = toChange[i].second;
nums[pos[c]] = d;
nums[pos[d]] = c;
xAns.push_back(pos[c]);
yAns.push_back(pos[d]);
int tmp = pos[d];
pos[d] = pos[c];
pos[c] = tmp;
}
for (int i = 0; i < mid - toChange.size(); i++) {
xAns.push_back(0);
yAns.push_back(0);
}
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
int lo = -1;
int hi = M;
while (lo + 1 < hi) {
int mid = (lo + hi) / 2;
if (check(mid,S,N,X,Y)) {
hi = mid;
}
else {
lo = mid;
}
toChange.clear();
}
check(hi, S, N, X, Y);
getAns(hi, S, N, X, Y);
for (int i = 0; i < xAns.size(); i++) {
P[i] = xAns[i];
Q[i] = yAns[i];
}
return hi;
}
컴파일 시 표준 에러 (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... |