QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#262006 | #1646. Disk Sort | ucup-team133# | TL | 1ms | 3752kb | C++17 | 3.5kb | 2023-11-23 14:41:41 | 2023-11-23 14:41:42 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<pair<int, int>> ans;
vector<vector<int>> a(n + 1, vector<int>(3));
auto Move = [&](int x, int y) {
if (x == y) {
return;
}
ans.emplace_back(x + 1, y + 1);
assert((int) ans.size() <= 6 * n);
assert(a[y][0] == 0);
for (int i = 0; i < 3; i++) {
if (a[x][i] != 0) {
swap(a[x][i], a[y][0]);
break;
} else if (i == 2) {
assert(false);
}
}
for (int i = 0; i < 2; i++) {
if (a[y][i + 1] == 0) {
swap(a[y][i], a[y][i + 1]);
}
}
};
auto Top = [&](int x) {
for (int i = 0; i < 3; i++) {
if (a[x][i] != 0) {
return a[x][i];
}
}
return 0;
};
for (int j = 0; j < 3; j++) {
for (int i = 0; i < n; i++) {
cin >> a[i][j];
}
}
while (true) {
vector<vector<int>> t(n + 1);
int z = -1;
for (int i = 0; i < n + 1; i++) {
if (a[i] == vector<int>(3, a[i][0])) {
if (a[i][0] == 0) {
z = i;
}
continue;
}
for (int j = 0; j < 3; j++) {
t[a[i][j]].emplace_back(j);
}
}
int c = -1;
for (int i = 0; i < n + 1; i++) {
if (t[i].empty()) {
continue;
}
sort(t[i].begin(), t[i].end());
if (c == -1 || t[c] > t[i]) {
c = i;
}
}
if (c == -1) {
break;
}
assert(t[c][0] == 0);
assert(t[c][1] <= 1);
vector<pair<int, int>> s;
for (int i = 0; i < n + 1; i++) {
for (int j = 0; j < 3; j++) {
if (a[i][j] == c) {
s.emplace_back(j, i);
}
}
}
sort(s.begin(), s.end());
// for (int i = 0; i < 3; i++) {
// cerr << s[i].first << " " << s[i].second << endl;
// }
for (int i = 0; i < 3; i++) {
// cerr << Top(s[i].second) << " " << c << endl;
while (Top(s[i].second) != c) {
assert(i != 0);
for (int j = i - 1; j >= 0; j--) {
if (a[s[j].second][0] == 0) {
Move(s[i].second, s[j].second);
break;
} else {
assert(j != 0);
}
}
}
Move(s[i].second, z);
}
while (a[s[2].second][2] != 0) {
for (int j = 1; j >= 0; j--) {
if (a[s[j].second][0] == 0) {
Move(s[2].second, s[j].second);
break;
} else {
assert(j != 0);
}
}
}
}
if (a[n][0] != 0) {
for (int i = 0; i < n; i++) {
if (a[i][0] == 0) {
Move(n, i);
Move(n, i);
Move(n, i);
break;
}
}
}
// for (int i = 0; i < n + 1; i++) {
// for (int j = 0; j < 3; j++) {
// cerr << a[i][j] << " \n"[j == 2];
// }
// }
cout << ans.size() << '\n';
for (auto [x, y]: ans) {
cout << x << " " << y << '\n';
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3520kb
input:
4 2 3 1 4 2 1 1 4 2 3 3 4
output:
9 3 5 2 3 2 5 3 2 3 5 3 2 5 3 5 3 5 3
result:
ok n=4
Test #2:
score: 0
Accepted
time: 0ms
memory: 3752kb
input:
2 1 2 1 2 1 2
output:
0
result:
ok n=2
Test #3:
score: -100
Time Limit Exceeded
input:
3 2 2 1 1 3 3 1 2 3