B. Fedya and Array
B. Fedya and Array
For his birthday recently Fedya was given an array of integers arranged in a circle, For each pair of neighboring numbers ( and , and , , and , and ) the absolute difference between them is equal to .
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 and are neighboring elements.
Unfortunately, Fedya lost an array, but he remembered in it the sum of local maximums and the sum of local minimums .
Given and , 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 (). Description of the test cases follows.
Each line of each test case contain two integers and () — the sum of local maximums and the sum of local minimums, respectively.
Output
For each test case, in the first line print one integer — the minimum length of matching arrays.
In the second line print integers () — the array elements such that the the absolute difference between each pair of neighboring is equal to .
If there are multiple solutions, print any of them.
It is guaranteed that the sum of over all test cases does not exceed .
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 and positions, and the local minimums are the numbers at and positions. , .
In the second test case, the local maximums are the numbers at and positions, and the local minimums are the numbers at and positions. , .
In the third test case, the local maximums are the numbers at and positions, and the local minimums are the numbers at and positions.
解题思路
首先要发现在循环序列中,局部最大值与局部最小值是交替出现这个性质,并且序列中局部最大值的数量等于局部最小值的数量。
令表示第个局部最大值,表示第个局部最小值,并且均有个。不失一般性的,假设在的前面(局部最大值与最小值交替出现),由于序列中相邻两个数的差值恰好为,因此为了通过得到,那么至少需要添加个数字,即。同理为了通过得到,至少需要添加个数字。
每次都取最小的长度,因此整个序列的最小长度就是
以举例:
然后可以发现是满足条件的一组构造方案。
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
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/17116798.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
2022-02-13 飞行员兄弟