CF:Cards

Cards

题面翻译

题目描述

现在有 n n n张卡牌( n n n为偶数),每张卡牌上都写着一个正整数。有 n / 2 n/2 n/2个人要玩这个卡牌游戏,在游戏开始时每个人会得到两张卡牌,每张卡牌只能给一个玩家。
现在让你发卡牌,使每一个人手上的卡片上所写的数字的总和相等。数据保证有发牌的方案符合题目要求。

输入输出格式

输入格式:

第一行包括一个整数 n ( 2 < = n < = 100 ) n(2<=n<=100) n(2<=n<=100),表示一共有的卡牌数。
第二行包括 n n n个正整数 a 1 , a 2 , . . . , a n a_1,a_2,...,a_n a1,a2,...,an ( 1 < = a i < = 100 ) (1<=ai<=100) (1<=ai<=100), a i a_i ai表示第 i i i张卡牌上的数字。

输出格式:

输出 n / 2 n/2 n/2对整数,第 i i i对数表示应给第 i i i个玩家的卡牌。每张卡牌只能给一个人,卡牌的编号按照输入的顺序编号。
数据保证有发牌的方案符合题目要求。如果有多种方案,可任意输出一个答案。

题目描述

There are n n n cards ( n n n is even) in the deck. Each card has a positive integer written on it. n / 2 n/2 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.

输入格式

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

The second line contains the sequence of n n n positive integers a 1 a_{1} a1, a 2 a_{2} a2,…, a n a_{n} an( 1<= a i a_{i} ai<=100), where a i a_{i} ai is equal to the number written on the i i i -th card.

输出格式

Print n / 2 n/2 n/2 pairs of integers, the i i i -th pair denote the cards that should be given to the i i 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.

样例 #1

样例输入 #1

6
1 5 7 4 4 3

样例输出 #1

1 3
6 2
4 5

样例 #2

样例输入 #2

4
10 10 10 10

样例输出 #2

1 2
3 4

提示

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 8 8 .

In the second sample, all values a i a_{i} ai are equal. Thus, any distribution is acceptable.

思路

使用pair数组,第一个first记录卡牌的数字,第二个second记录卡牌的编号,因为题目保证有解且只有输出一种,只要进行sort排序,然后从头尾两边开始遍历,输出每个人的卡牌编号就能得到答案。

#include <bits/stdc++.h>
using namespace std;
int main() {
	int n;
	cin >> n;
	int t;
	vector<pair<int,int>> v(n);
	for(int i=0;i<n;i++){
		cin >> t;
		v[i]={t, i + 1};//pair元素的赋值方式
	}
	sort(v.begin(), v.end());//pair数组的排序
	for (int l = 0, r = n - 1; l < r; l++, r--) {
		cout << v[l].second << " " << v[r].second << "\n";
	}
}

学到的东西

  1. pair的赋值方式
    最简单的又常用的:如pair<int,int> p = {1, 1};
    正式的:pair<double,double> p1=make_pair(1.2,2.2);
  2. 使用sort对pair数组排序
    默认对 先first 进行升序排列 ,如果first相同时, 那么再对second 进行升序排列。当然也可以自己写一个cmp 实现对其别的要求的排序 。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值