B. Fedya and Array

B. Fedya and Array

For his birthday recently Fedya was given an array $a$ of $n$ integers arranged in a circle, For each pair of neighboring numbers ($a_1$ and $a_2$, $a_2$ and $a_3$, $\ldots$, $a_{n - 1}$ and $a_n$, $a_n$ and $a_1$) the absolute difference between them is equal to $1$.

Let's call a local maximum an element, which is greater than both of its neighboring elements. Also call a local minimum an element, which is less than both of its neighboring elements. Note, that elements $a_1$ and $a_n$ are neighboring elements.

Unfortunately, Fedya lost an array, but he remembered in it the sum of local maximums $x$ and the sum of local minimums $y$.

Given $x$ and $y$, help Fedya find any matching array of minimum length.

Input

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). Description of the test cases follows.

Each line of each test case contain two integers $x$ and $y$ ($-10^{9} \le y < x \le 10^{9}$) — the sum of local maximums and the sum of local minimums, respectively.

Output

For each test case, in the first line print one integer $n$ — the minimum length of matching arrays.

In the second line print $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^{9} \leqslant a_i \leqslant 10^{9}$) — the array elements such that the the absolute difference between each pair of neighboring is equal to $1$.

If there are multiple solutions, print any of them.

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^{5}$.

Example

input

4
3 -2
4 -4
2 -1
5 -3

output

10
0 1 2 1 0 -1 0 -1 0 1
16
-2 -1 -2 -1 0 1 2 3 4 5 4 3 2 1 0 -1 
6
1 0 -1 0 1 0
16
2 3 2 1 0 -1 0 -1 0 -1 0 1 2 1 0 1

Note

In the first test case, the local maximums are the numbers at $3, 7$ and $10$ positions, and the local minimums are the numbers at $1, 6$ and $8$ positions. $x = a_3 + a_7 + a_{10} = 2 + 0 + 1 = 3$, $y = a_1 + a_6 + a_8 = 0 + (-1) + (-1) = -2$.

In the second test case, the local maximums are the numbers at $2$ and $10$ positions, and the local minimums are the numbers at $1$ and $3$ positions. $x = a_2 + a_{10} = -1 + 5 = 4$, $y = a_1 + a_3 = -2 + (-2) = -4$.

In the third test case, the local maximums are the numbers at $1$ and $5$ positions, and the local minimums are the numbers at $3$ and $6$ positions.

 

解题思路

  首先要发现在循环序列中,局部最大值与局部最小值是交替出现这个性质,并且序列中局部最大值的数量等于局部最小值的数量。

  令$a_i$表示第$i$个局部最大值,$b_i$表示第$i$个局部最小值,并且均有$m$个。不失一般性的,假设$a_i$在$b_i$的前面(局部最大值与最小值交替出现),由于序列中相邻两个数的差值恰好为$1$,因此为了通过$a_i$得到$b_i$,那么至少需要添加$a_i - b_i$个数字,即$a_i, a_i - 1, \ldots, b_i$。同理为了通过$b_i$得到$a_{i+1}$,至少需要添加$a_{i+1} - b_i$个数字。

  每次都取最小的长度,因此整个序列的最小长度就是

\begin{align*}
& \ \ \ \ \ (a_1 - b_1) + (a_2 - b_1) + (a_2 - b_2) + \ldots + (a_m - b_m) + (a_1 - b_m) \\
&= 2 \cdot (a_1 + a_2 + \ldots + a_k) - 2 \cdot (b_1 + b_2 + \ldots + b_k) \\
&= 2 \cdot (x - y) = n
\end{align*}

  以$m=5$举例:

  然后可以发现$[x, x - 1, x - 2, \ldots, y - 1, y, y + 1, y + 2, \ldots, x - 2, x - 1]$是满足条件的一组构造方案。

  AC代码如下:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef long long LL;
 5 
 6 void solve() {
 7     int x, y;
 8     scanf("%d %d", &x, &y);
 9     printf("%d\n", x - y << 1);
10     for (int i = x; i >= y; i--) {
11         printf("%d ", i);
12     }
13     for (int i = y + 1; i < x; i++) {
14         printf("%d ", i);
15     }
16     printf("\n");
17 }
18 
19 int main() {
20     int t;
21     scanf("%d", &t);
22     while (t--) {
23         solve();
24     }
25     
26     return 0;
27 }

 

 参考资料

  Codeforces Round #852 Editorial:https://codeforces.com/blog/entry/112723

posted @ 2023-02-13 16:22  onlyblues  阅读(42)  评论(0编辑  收藏  举报
Web Analytics