B. Absent Remainder

这是一个关于C++编程的算法问题,目标是找到一个序列中的一半数量的不同整数对,使得这对整数不相等,同时它们都存在于原始序列中,且它们的模运算结果不在序列内。题目给出了样例输入和输出,并提示至少存在一种解决方案。代码示例展示了如何遍历序列并打印符合条件的整数对。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

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--;
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值