CF2227C.Snowfall
入门
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Yousef has given you an array a of n positive integers.
Let f(a) denote the number of subarrays∗ of a whose product is divisible by 6.
More formally, for every pair of indices l and r such that 1≤l≤r≤n, consider the subarray al,al+1,…,ar. This subarray is counted if the product of its elements is divisible by 6.
For example, if a=[1,6,2], then the subarrays whose products are divisible by 6 are [6], [1,6], [6,2], and [1,6,2], so f(a)=4.
Your task is to reorder the elements of the array a so that f(a) is minimized. If there are multiple ways to do this, you may output any of them.
∗An array b is a subarray of an array a if b can be obtained from a by deleting several (possibly zero or all) elements from the beginning and several (possibly zero or all) elements from the end.
优素福给了你一个包含 n 个正整数的数组 a。
令 f(a) 表示数组 a 中乘积能被 6 整除的子数组∗ 的个数。
更准确地说,对每一对满足 1≤l≤r≤n 的下标 l 和 r,考虑子数组 al,al+1,…,ar。若该子数组所有元素的乘积能被 6 整除,则将其计入总数。
例如,若 a=[1,6,2],则乘积能被 6 整除的子数组为 [6]、[1,6]、[6,2] 和 [1,6,2],因此 f(a)=4。
你的任务是重新排列数组 a 的元素,使得 f(a) 尽可能小。如果存在多种最优排列方式,输出任意一种即可。
∗ 若数组 b 可通过从数组 a 的开头删除若干(可能为零或全部)元素,并从结尾删除若干(可能为零或全部)元素而得到,则称 b 是 a 的一个子数组。
输入格式
The first line of the input contains an integer t (1≤t≤104) — the number of test cases.
The first line of each test case contains an integer n (1≤n≤2⋅105) — the size of the array.
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤109) — the elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
输入的第一行包含一个整数 t(1≤t≤104)—— 表示测试用例的数量。
每个测试用例的第一行包含一个整数 n(1≤n≤2⋅105)—— 表示数组的大小。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤109)—— 表示数组的元素。
保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
For each test case, output the array after reordering it in such a way that f(a) is minimized. If there are multiple answers, you may output any of them.
对于每个测试用例,输出按某种方式重排后的数组,使得 f(a) 最小。如果存在多个满足条件的答案,你可以输出其中任意一个。
输入输出样例
输入#1
5 6 12 7 9 4 18 5 4 3 6 2 8 7 1 10 15 20 3 6 9 5 11 14 21 2 5 3 6 6 6
输出#1
12 18 4 7 5 9 2 8 3 6 6 10 20 1 15 3 9 21 5 11 2 14 6 6 6
说明/提示
In the first test case, an optimal arrangement is a=[12,18,4,7,5,9]. The subarrays whose products are divisible by 6 are:
- [12]
- [18]
- [12,18]
- [18,4]
- [12,18,4]
- [18,4,7]
- [12,18,4,7]
- [18,4,7,5]
- [4,7,5,9]
- [12,18,4,7,5]
- [18,4,7,5,9]
- [12,18,4,7,5,9]
Therefore, f(a)=12. It can be proven that no other arrangement yields a smaller value of f(a).
在第一个测试用例中,一种最优排列为 a=[12,18,4,7,5,9]。其乘积能被 6 整除的子数组有:
- [12]
- [18]
- [12,18]
- [18,4]
- [12,18,4]
- [18,4,7]
- [12,18,4,7]
- [18,4,7,5]
- [4,7,5,9]
- [12,18,4,7,5]
- [18,4,7,5,9]
- [12,18,4,7,5,9]
因此,f(a)=12。可以证明:不存在其他排列能使 f(a) 取得更小的值。
输入解题思路,AI测评打分。不知道怎么写?