# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
647814 | Oepeling | 중앙값 배열 (balkan11_medians) | C++14 | 51 ms | 5024 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
struct list {
vector<bool> taken;
vector<int> next;
vector<int> prev;
int min;
int max;
explicit list(int n) {
next.resize(2 * n);
prev.resize(2 * n);
taken.resize(2 * n);
min = 1;
max = 2 * n - 1;
}
bool exists(int x) {
return taken[x];
}
int insert_after(int where, int x) {
assert(x > 0 && x < taken.size());
assert(!taken[x]);
assert(where == 0 || taken[where]);
next[x] = next[where];
prev[x] = where;
prev[next[x]] = x;
next[where] = x;
taken[x] = true;
return x;
}
int insert_before(int where, int x) {
assert(x > 0 && x < taken.size());
assert(!taken[x]);
assert(where == 0 || taken[where]);
prev[x] = prev[where];
next[x] = where;
next[prev[x]] = x;
prev[where] = x;
taken[x] = true;
return x;
}
int insert_min() {
while (min < taken.size() && taken[min]) { min++; }
return insert_after(min - 1, min);
}
int insert_max() {
while (max > 0 && taken[max]) { max--; }
// cerr << max << endl;
return insert_before((max + 1) % taken.size(), max);
}
};
int main() {
int n; cin >> n;
vector<int> input(n);
for (int i = 0; i < n; i++) {
cin >> input[i];
}
list list(n);
int median = input[0];
vector<int> ans;
ans.push_back(list.insert_after(0, median));
for (int i = 1; i < n; i++) {
if (input[i] == median) {
ans.push_back(list.insert_min());
ans.push_back(list.insert_max());
} else if (input[i] > median) {
if (list.exists(input[i])) {
ans.push_back(list.insert_max());
} else {
ans.push_back(list.insert_after(median, input[i]));
}
ans.push_back(list.insert_max());
// cerr << input[i] << " " << ans.back() << endl;
} else {
if (list.exists(input[i])) {
ans.push_back(list.insert_min());
} else {
ans.push_back(list.insert_before(median, input[i]));
}
ans.push_back(list.insert_min());
}
median = input[i];
}
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
cout << "\n";
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |