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 (a1 and a2, a2 and a3, , an1 and an, an and a1) 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 a1 and an 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 (1t1000). Description of the test cases follows.

Each line of each test case contain two integers x and y (109y<x109) — 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 a1,a2,,an (109ai109) — 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 2105.

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=a3+a7+a10=2+0+1=3, y=a1+a6+a8=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=a2+a10=1+5=4, y=a1+a3=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.

 

解题思路

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

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

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

     (a1b1)+(a2b1)+(a2b2)++(ambm)+(a1bm)=2(a1+a2++ak)2(b1+b2++bk)=2(xy)=n

  以m=5举例:

  然后可以发现[x,x1,x2,,y1,y,y+1,y+2,,x2,x1]是满足条件的一组构造方案。

  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 @   onlyblues  阅读(55)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
历史上的今天:
2022-02-13 飞行员兄弟
Web Analytics
点击右上角即可分享
微信分享提示