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

题目:http://codeforces.com/problemset/problem/254/A

   给出n个数据,如果能分为2n对数据,每对数据相同 则输出这n对数据的标号 否则输出-1

思路:这题一开始是暴力法...果然超时

   然后看到1 ≤ ai ≤ 5000 于是开了一个a[5000]的数组

    

#include <iostream>
#include <stdio.h>

using namespace std;

int a[5000];
int pairs[1000000][2];

int main()
{
    int counter=0;
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int n;
    cin >> n;
    int possible=1;
    for(int i=0;i<2*n;i++)
    {
        int temp;
        cin >> temp;
        if(a[temp]<=0) a[temp]=i+1;
        else 
        {
            pairs[counter][0]=a[temp];
            pairs[counter][1]=i+1;
            counter++;
            a[temp]=0;
        }
    }
    for(int i=0;i<5000;i++)
    {
        
        if(a[i]>0) possible=0;
    }

    if(possible)
    {
        for(int i=0;i<n;i++)
        {
            cout << pairs[i][0]<<" "<<pairs[i][1]<<endl;
        }
    }
    else cout <<"-1";
    return 0;
}

转载于:https://www.cnblogs.com/danielqiu/archive/2013/01/17/2865043.html

猜你喜欢

转载自blog.csdn.net/weixin_33713503/article/details/93795257
今日推荐