#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 100010;
struct node
{
int to, id;
}tong[N];
int n, a[N], cnt;
bool cmp (node x, node y)
{
if(x.to == y.to) return x.id < y.id;
return x.to > y.to;
}
int main ()
{
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
if(!tong[a[i]].to)
{
cnt++;
tong[a[i]].id = a[i];
}
tong[a[i]].to++;
}
if(cnt % 2 == 1)
{
printf("-1");
return 0;
}
sort(tong + 1, tong + 1 + n, cmp);
for(int i = 1; i <= cnt; i++)
{
while(tong[i].to-- && tong[i + 1].to--)
{
printf("%d %d ", tong[i].id, tong[i + 1].id);
}
}
return 0;
}
这是零分的
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 100010;
struct node
{
int to, id = 0x3f3f3f3f;
}tong[N];
int n, a[N], cnt;
bool cmp (node x, node y)
{
if(x.to == y.to) return x.id < y.id;
return x.to > y.to;
}
int main ()
{
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
if(tong[a[i]].to == 0)
{
cnt++;
tong[a[i]].id = a[i];
}
tong[a[i]].to++;
}
if(cnt % 2 == 1)
{
printf("-1");
return 0;
}
for(int i = 1; i <= n; i++)
{
if(tong[i].to) printf("%d ", tong[i].id), tong[i].to--;
}
for(int i = 1; i <= n; i++) while(tong[i].to) printf("%d ", tong[i].id), tong[i].to--;
return 0;
}
这是满分的
感谢大佬