当前位置: 首页 > 工具软件 > card-one > 使用案例 >

A. Cards

袁成化
2023-12-01

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n cards (n is even) in the deck. Each card has a positive integer written on it. n / 2 people will play new card game. At the beginning of the game each player gets two cards, each card is given to exactly one player.

Find the way to distribute cards such that the sum of values written of the cards will be equal for each player. It is guaranteed that it is always possible.

Input

The first line of the input contains integer n (2 ≤ n ≤ 100) — the number of cards in the deck. It is guaranteed that n is even.

The second line contains the sequence of n positive integers a1, a2, ..., an (1 ≤ ai ≤ 100), where ai is equal to the number written on the i-th card.

Output

Print n / 2 pairs of integers, the i-th pair denote the cards that should be given to the i-th player. Each card should be given to exactly one player. Cards are numbered in the order they appear in the input.

It is guaranteed that solution exists. If there are several correct answers, you are allowed to print any of them.

Examples
input
6
1 5 7 4 4 3
output
1 3
6 2
4 5
input
4
10 10 10 10
output
1 2
3 4
Note

In the first sample, cards are distributed in such a way that each player has the sum of numbers written on his cards equal to 8.

In the second sample, all values ai are equal. Thus, any distribution is acceptable.


解题说明:此题的意思是把一组数两两配对,让每对数之和相等。由于题目保证一定有解,先计算每对数的和,然后遍历找到对应的配对数即可,完成配对后做一个标记确保不会重复。


#include<cstdio>
#include <cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include <map>
using namespace std;

int main()
{
	int i,j,n,sum=0;
	int a[100];
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
	}
	for(i=1;i<=n;i++)
	{
		sum=sum+a[i];
	}
	sum=sum/(n/2);
	for(i=1;i<=n;i++)
	{
		for(j=i+1;j<=n;j++)
		{
			if((a[i]+a[j])==sum)
			{
				printf("%d %d\n",i,j);
				a[i]=0;
				a[j]=0;
				break;
			}
		}
	}
	return 0;
}


 类似资料:

相关阅读

相关文章

相关问答