题解:Codeforces Round 967 (Div. 2) B [思维/构造]
B. Generate Permutation
time limit per test: 1.5 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output
There is an integer sequence
Misuki has two typewriters where the first one writes letters from left to right, with a pointer initially pointing to
Misuki would choose one of the typewriters and use it to perform the following operations until
- write number: write the minimum positive integer that isn't present in the array
to the element , is the position where the pointer points at. Such operation can be performed only when . - carriage return: return the pointer to its initial position (i.e.
for the first typewriter, for the second) - move pointer: move the pointer to the next position, let
be the position the pointer points at before this operation, if Misuki is using the first typewriter, would happen, and otherwise. Such operation can be performed only if after the operation, holds.
Your task is to construct any permutation
有一个长度为
美雪有两台打字机,第一台打字机从左往右写字母,指针最初指向
美雪会选择其中一台打字机进行以下操作,直到
- 写数:将
中未出现的最小正整数写入 , 是指针指向的位置。这种操作只能在 时执行。 - 回车:将指针返回到初始位置(第一台打字机为
,第二台打字机为 )。 - 移动指针:将指针移动到下一个位置,假设
是指针在执行此操作前所指向的位置,如果美雪使用的是第一台打字机,则为 ,否则为 。只有在操作之后, 成立时,才能执行该操作。
你的任务是构造长度为
Input
Each test contains multiple test cases. The first line of input contains a single integer
The first line of each test case contains a single integer
It is guaranteed that the sum of
输入
每个测试包含多个测试用例。第一行输入包含一个整数
每个测试用例的第一行都包含一个整数
保证所有测试用例的
Output
For each test case, output a line of
If there are multiple valid permutations, you can output any of them.
输出
对于每个测试用例,输出一行
如果存在多种有效的排列组合,则可以输出其中任何一种。
Example
Input
3 1 2 3
Output
1 -1 3 1 2
Note
In the first testcase, it's possible to make
In the second testcase, it is possible to make
If Misuki is using the first typewriter:
- Write number: write
to , becomes - Move pointer: move the pointer to the next position. (i.e.
) - Write number: write
to , becomes
If Misuki is using the second typewriter:
- Move pointer: move the pointer to the next position. (i.e.
) - Write number: write
to , becomes - Carriage return: return the pointer to
. - Write number: write
to , becomes
It can be proven that the minimum number of carriage returns needed to transform
Similarly,
In the third testcase, it is possibile to make
With the first typewriter it is possible to:
- Move pointer: move the pointer to the next position. (i.e.
) - Write number: write
to , becomes - Move pointer: move the pointer to the next position. (i.e.
) - Write number: write
to , becomes - Carriage return: return the pointer to
. - Write number: write
to , becomes
With the second typewriter it is possible to:
- Move pointer: move the pointer to the next position. (i.e.
) - Write number: write
to , becomes - Carriage return: return the pointer to
. - Write number: write
to , becomes - Move pointer: move the pointer to the next position. (i.e.
) - Move pointer: move the pointer to the next position. (i.e.
) - Write number: write
to , becomes
注
在第一个测试用例中,可以使用
在第二个测试用例中,可以使用 $$
如果美树使用的是第一台打字机:
- 写数字:将
写入 , 变为 - 移动指针:将指针移动到下一个位置。(即
) - 写入数字:将
写入 , 变为 。
如果美雪使用的是第二台打字机:
- 移动指针:将指针移动到下一个位置。(即
) - 写入数字:将
写入 , 变为 。 - 回车:返回指向
的指针。 - 写入数字:将
写入 , 变为 。
可以证明,使用第一台打字机时,将
同样,
题意
你需要在一个水平的序列中从
你有两台打字机
- 一台从最左边开始打印,每次只能打印/向右走一格/回到初始位置
- 一台从最右边开始打印,每次只能打印/向左走一格/回到初始位置
每次打印只能打印
你需要给出一个序列,使得打印这个序列时
不管选择哪一台打印机,回到初始位置的操作数相同
- 如果存在这样的序列,那就从左到右打印这个序列
- 如果不存在这样的序列,那就输出
题解
这题实际上考察了思维和构造
怎么构造这样的一个序列呢?
其实样例给了我们提示
我们只要类似于把数字在两边交替放置,一直到中间即可
比如,当
序列为
这样的话,就可以不管从哪边开始打字,
返回初始位置次数都是一样的了
但是,如果
构造不了序列
代码
#include <bits/stdc++.h> #define int long long #define INF 0x3f3f3f3f #define all(x) x.begin(),x.end() using i64 = long long; const int N = 2e5 + 10; int t = 1; int a[N]; void solve() { int n; std::cin >> n; if(n%2 == 1) { int st = 1; while(st <= n) { std::cout << st << " "; st += 2; } st = n-1; while(st) { std::cout << st << " "; st -= 2; } std::cout << "\n"; } else std::cout << -1 << "\n"; } signed main(){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cin >> t; while(t--){ solve(); } return 0; }
有什么建议或者意见的欢迎在评论区留言!
posted on 2024-08-21 16:49 Jiejiejiang 阅读(75) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」