[Codeforces Round #155 (Div. 2)]A. Cards with Numbers

地址:http://codeforces.com/contest/254/problem/A

这题昨天就在想,想复杂了一些

一个5000的数组用来存数字的位数,一个400000x2的二维数组用来存答案(400000稍大了些)

读每个数第一次读入时,先将其位置存到5000数组里,第二次读入时,一起转到二维数组中,然后5000数组相应位置清零

所有数据读完后,检查5000数组,全为零则可以输出答案,否则输出-1

 1 #include<stdio.h>
 2 
 3 int n,a[5001],ans[400000][2];
 4 
 5 int main()
 6 {
 7     int i,in,j=0,flag=0;
 8     freopen("input.txt","r",stdin);
 9     freopen("output.txt","w",stdout);
10     scanf("%d",&n);
11     for(i=1;i<=2*n;i++)
12     {
13         scanf("%d",&in);
14         if(0==a[in]) a[in]=i;
15         else
16         {
17             ans[j][0]=a[in];
18             ans[j][1]=i;
19             j++;
20             a[in]=0;
21         }
22     }
23     for(i=1;i<=5000;i++)
24     {
25         if(a[i]!=0) {flag=1;break;}
26     }
27     if(flag) printf("-1\n");
28     else
29     {
30         while(j>0)
31         {
32             printf("%d %d\n",ans[j-1][0],ans[j-1][1]);
33             j--;
34         }
35     }
36     return 0;
37 }

 

posted @ 2013-01-18 21:22  tjsuhst  阅读(169)  评论(0)    收藏  举报