QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#608331 | #6838. Assumption is All You Need | Sunlight9 | WA | 1ms | 3576kb | C++20 | 3.7kb | 2024-10-03 20:43:30 | 2024-10-03 20:43:30 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve() {
int n;
cin >> n;
vector<int> a(n + 1), b(n + 1);
vector<int> posa(n + 1), posb(n + 1);
for (int i = 1; i <= n; ++i) {
cin >> a[i];
posa[a[i]] = i;
}
for (int i = 1; i <= n; ++i) {
cin >> b[i];
posb[b[i]] = i;
}
vector edge(n + 1, vector(n + 1, 0));
vector<int> vis(n + 1);
for (int i = 1; i <= n; ++i) {
int u = i;
int v = posb[a[i]];
if (a[u] > a[v]) {
edge[u][v] = 1;
edge[v][u] = -1;
}
else if (a[u] < a[v]) {
edge[v][u] = 1;
edge[u][v] = -1;
} else {
vis[a[i]] = 1;
}
}
vector<array<int, 2> > ans;
int i = n;
while (true) {
if (i == 0) break;
// cerr << i << " \n";
if (vis[i]) {
i--;
continue;
}
int u = posa[i];
// cerr << u << " u\n";
vector<int> to;
for (int j = u + 1; j <= n; ++j) {
if (edge[u][j] == 1) to.emplace_back(j);
}
if (to.empty()) {cout << "-1\n"; return;}
int v = to[0];
ans.push_back({u, v});
int lstu = 0, lstv = 0;
for (int j = 1; j <= n; ++j) {
if (edge[u][j] != 0 && j != v) {
lstu = j;
break;
}
}
for (int j = 1; j <= n; ++j) {
if (edge[v][j] != 0 && j != u) {
lstv = j;
break;
}
}
for (int j = 1; j <= n; ++j) {
edge[u][j] = edge[j][u] = 0;
edge[v][j] = edge[j][v] = 0;
}
swap(a[u], a[v]);
posa[a[u]] = u;
posa[a[v]] = v;
int lsti = i;
if (a[u] != b[u]) {
vis[a[v]] = 1;
lsti = -1;
if (a[v] == i) {
i--;
}
if (a[u] > a[lstu]) {
edge[u][lstu] = 1;
edge[lstu][u] = -1;
} else if (a[u] < a[lstu]) {
edge[lstu][u] = 1;
edge[u][lstu] = -1;
} else vis[a[u]] = 1;
if (a[u] > a[lstv]) {
edge[u][lstv] = 1;
edge[lstv][u] = -1;
} else if (a[u] < a[lstv]) {
edge[lstv][u] = 1;
edge[u][lstv] = -1;
} else vis[a[u]] = 1;
}
if (a[v] != b[v]) {
vis[a[u]] = 1;
lsti = -1;
if (a[u] == i) {
i--;
}
if (a[v] > a[lstu]) {
edge[v][lstu] = 1;
edge[lstu][v] = -1;
} else if (a[v] < a[lstu]) {
edge[lstu][v] = 1;
edge[v][lstu] = -1;
} else vis[a[v]] = 1;
if (a[v] > a[lstv]) {
edge[v][lstv] = 1;
edge[lstv][v] = -1;
} else if (a[v] < a[lstv]) {
edge[lstv][v] = 1;
edge[v][lstv] = -1;
} else vis[a[v]] = 1;
}
if (i == lsti) {
vis[a[u]] = vis[a[v]] = 1;
i--;
}
// for (int k = 1; k <= n; ++k) {
// for (int j = 1; j <= n; ++j) {
// cerr << edge[k][j] << " \n"[j == n];
// }
// }
// cerr << '\n';
}
cout << ssize(ans) << "\n";
for (i = 0; i < ssize(ans); ++i) {
cout << ans[i][0] << " " << ans[i][1] << "\n";
}
}
int main() {
cin.tie(nullptr) -> sync_with_stdio(false);
int _;
cin >> _;
while (_--) solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3568kb
input:
3 2 1 2 2 1 4 4 1 2 3 1 3 2 4 8 8 7 6 5 4 3 2 1 1 8 7 6 5 4 3 2
output:
-1 2 1 2 2 4 7 1 2 1 3 1 4 1 5 1 6 1 7 1 8
result:
ok T=3
Test #2:
score: -100
Wrong Answer
time: 1ms
memory: 3576kb
input:
315 10 8 4 6 1 2 9 7 5 10 3 6 7 8 10 5 1 3 2 9 4 10 10 8 2 9 6 5 7 4 3 1 7 1 3 5 9 8 4 10 6 2 6 4 6 5 3 1 2 1 5 4 6 2 3 12 5 9 12 8 10 6 11 4 2 3 1 7 9 2 3 1 5 12 4 7 6 10 8 11 10 4 7 3 2 8 9 6 10 5 1 1 4 8 10 3 7 9 6 2 5 7 1 2 4 5 6 7 3 4 3 5 6 7 2 1 3 1 3 2 2 1 3 7 1 5 3 7 6 4 2 6 5 2 1 3 4 7 1 1 ...
output:
-1 8 1 7 7 8 4 5 2 6 4 9 2 4 2 3 2 10 5 2 3 3 4 1 3 1 5 5 6 -1 -1 -1 -1 -1 0 -1 -1 -1 3 4 5 1 2 1 6 -1 -1 -1 0 0 -1 -1 -1 -1 -1 -1 -1 -1 -1 3 2 4 1 2 1 5 -1 -1 -1 -1 -1 -1 0 0 -1 -1 -1 -1 -1 -1 0 0 -1 -1 0 -1 -1 -1 1 1 2 4 3 6 2 5 3 4 1 3 2 1 2 2 3 -1 -1 0 -1 -1 2 2 3 3 4 -1 -1 -1 0 -1 -1 -1 -1 -1 -...
result:
wrong answer Case #180: Jury has the answer but participant has not