CodeForces - 254A Cards with Numbers

前幾日粗略的讀了組合數學的幾個ppt,都與離散的有點關係,明日考數據庫,近期忙着複習(突擊),組合數學的題目還沒開始做,先補個昨天的題目。

題意:給你2*n個數,問你能不能平均分,且每組數據相等,可以則輸出下標,否則-1

思路:一開始是想直接開個數組暴力,注意到數據只有5000,直接記錄每個數字出現的次數就行了,一遍過下來,有奇數的直接-1,否則輸出。水的很。。

A. Cards with Numbers
time limit per test
1 second
memory limit per test
256 megabytes
input
input.txt
output
output.txt

Petya has got 2n cards, each card contains some integer. The numbers on the cards can be the same. Let's index all cards by consecutive integers from 1 to 2n. We'll denote the number that is written on a card with number i, as ai. In order to play one entertaining game with his friends, Petya needs to split the cards into pairs so that each pair had equal numbers on the cards. Help Petya do that.

Input

The first line contains integer n (1 ≤ n ≤ 3·105). The second line contains the sequence of 2n positive integers a1, a2, ..., a2n(1 ≤ ai ≤ 5000) — the numbers that are written on the cards. The numbers on the line are separated by single spaces.

Output

If it is impossible to divide the cards into pairs so that cards in each pair had the same numbers, print on a single line integer -1. But if the required partition exists, then print n pairs of integers, a pair per line — the indices of the cards that form the pairs.

Separate the numbers on the lines by spaces. You can print the pairs and the numbers in the pairs in any order. If there are multiple solutions, print any of them.

Examples
input
3
20 30 10 30 20 10
output
4 2
1 5
6 3
input
1
1 2
output
-1
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<cmath>
using namespace std;
#define ll long long
#define N 5010
#define maxn 1000010
int vis[N];
int map[maxn][2];
int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int n;
    //cout<<0%2<<endl;
    int temp;
    cin>>n;
    int flag=1;
    int num=0;
    for(int i=1;i<=2*n;i++)
    {
        scanf("%d",&temp);
        if(vis[temp]==0)
            vis[temp]=i;
        else
        {
            map[num][0]=vis[temp];
            map[num][1]=i;
            num++;
            vis[temp]=0;
        }
    }
    for(int i=0;i<5000;i++)
    {
        if(vis[i]>0)
            flag=0;
    }
    if(flag!=0)
    {
        for(int i=0;i<n;i++)
        {
            cout<<map[i][0]<<" "<<map[i][1]<<endl;
        }
    }
    else cout <<"-1"<<endl;
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章