题目
B. Absent Remainder
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a sequence 𝑎1,𝑎2,…,𝑎𝑛 consisting of 𝑛 pairwise distinct positive integers.
Find ⌊𝑛/2⌋ different pairs of integers 𝑥 and 𝑦 such that:
- 𝑥≠𝑦;
- 𝑥 and 𝑦 appear in 𝑎;
- 𝑥 𝑚𝑜𝑑 𝑦 doesn’t appear in 𝑎.
Note that some 𝑥 or 𝑦 can belong to multiple pairs.
⌊𝑥⌋ denotes the floor function — the largest integer less than or equal to 𝑥. 𝑥 𝑚𝑜𝑑 𝑦 denotes the remainder from dividing 𝑥 by 𝑦.
If there are multiple solutions, print any of them. It can be shown that at least one solution always exists.
Input
The first line contains a single integer 𝑡 (1≤𝑡≤104) — the number of testcases.
The first line of each testcase contains a single integer 𝑛 (2≤𝑛≤2⋅105) — the length of the sequence.
The second line of each testcase contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤106).
All numbers in the sequence are pairwise distinct. The sum of 𝑛 over all testcases doesn’t exceed 2⋅10^5.
Output
The answer for each testcase should contain ⌊𝑛2⌋ different pairs of integers 𝑥 and 𝑦 such that 𝑥≠𝑦, 𝑥 and 𝑦 appear in 𝑎 and 𝑥 𝑚𝑜𝑑 𝑦 doesn’t appear in 𝑎. Print the pairs one after another.
You can print the pairs in any order. However, the order of numbers in the pair should be exactly such that the first number is 𝑥 and the second number is 𝑦. All pairs should be pairwise distinct.
If there are multiple solutions, print any of them.
Example
input
4
2
1 4
4
2 8 3 4
5
3 8 5 9 7
6
2 7 5 3 4 8
output
4 1
8 2
8 4
9 5
7 5
8 7
4 3
5 2
Note
In the first testcase there are only two pairs: (1,4) and (4,1). ⌊2/2⌋=1, so we have to find one pair. 1 𝑚𝑜𝑑 4=1, and 1 appears in 𝑎, so that pair is invalid. Thus, the only possible answer is a pair (4,1).
In the second testcase, we chose pairs 8 𝑚𝑜𝑑 2=0 and 8 𝑚𝑜𝑑 4=0. 0 doesn’t appear in 𝑎, so that answer is valid. There are multiple possible answers for that testcase.
In the third testcase, the chosen pairs are 9 𝑚𝑜𝑑 5=4 and 7 𝑚𝑜𝑑 5=2. Neither 4, nor 2, appears in 𝑎, so that answer is valid.
参考代码
#include <stdio.h>
#define MAXN 1000000
int main()
{
int t, n, arr[MAXN], min, count;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
if (i == 0)
min = arr[i];
else if (min > arr[i])
min = arr[i];
}
count = n / 2;
for (int i = 0; i < n && count; i++)
{
if (arr[i] != min)
{
printf("%d %d\n", arr[i], min);
count--;
}
}
}
}