A
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<math.h>
#include<vector>
#define INF 10000000
#define MAXN 1000
#define MAXL 1000
using namespace std;
struct T
{
int x;
int y;
} a[600005];
int cmp(T xx,T yy)
{
return xx.y<yy.y;
}
int main()
{
int n;
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
while(cin>>n)
{
for(int i=1; i<=2*n; ++i)
{
a[i].x=i;
cin>>a[i].y;
}
sort(a+1,a+2*n+1,cmp);
int t=0;
for(int i=1; i<=2*n; i+=2)
{
if(a[i].y!=a[i+1].y)
{
t=1;
break;
}
}
if(t==1)
{
cout<<-1<<endl;
}
else
{
for(int i=1; i<=2*n; i+=2)
{
cout<<a[i].x<<' '<<a[i+1].x<<endl;
}
}
}
return 0;
}
也可以还可以用vector实现,a[i]的范围比较小
vector<int>a[5001];
for(int i=1;i<=n;++i)
{
cin>>x;
a[x].push_back(i);
}
让后判断a[i].size()的奇偶性,全部为偶数则输出,否则输出-1。