如题.
#include <bits/stdc++.h>
using namespace std;
int n;
int a[100001];
bool mark[100001];
priority_queue<int> q;
queue<int> w;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
q.push(a[i]);
}
while (!q.empty()) {
int x = q.top();
q.pop();
if (mark[x])
continue;
int k = 0;
for (int i = 1; i < n; ++i) {
if (a[i] == x) {
for (int j = i + 1; j <= n; ++j) {
if (a[j] == 0)
continue;
if (mark[a[j]] == 0) {
k = a[j];
a[j] = 0;
break;
}
}
}
if(k!=0) break;
}
if (k != 0) {
mark[x] = 1;
mark[k] = 1;
w.push(x);
w.push(k);
}
}
while (!w.empty()) {
printf("%d ", w.front()); // queue无top,有front
w.pop();
}
return 0;
}
/*
4
3 1 4 2
*/