题解: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 \(a\) of length \(n\), where each element is initially \(-1\).
Misuki has two typewriters where the first one writes letters from left to right, with a pointer initially pointing to \(1\), and another writes letters from right to left with a pointer initially pointing to \(n\).
Misuki would choose one of the typewriters and use it to perform the following operations until \(a\) becomes a permutation of \([1, 2, \ldots, n]\)
- write number: write the minimum positive integer that isn't present in the array \(a\) to the element \(a_i\), \(i\) is the position where the pointer points at. Such operation can be performed only when \(a_i = -1\).
- carriage return: return the pointer to its initial position (i.e. \(1\) for the first typewriter, \(n\) for the second)
- move pointer: move the pointer to the next position, let \(i\) be the position the pointer points at before this operation, if Misuki is using the first typewriter, \(i := i + 1\) would happen, and \(i := i - 1\) otherwise. Such operation can be performed only if after the operation, \(1 \le i \le n\) holds.
Your task is to construct any permutation \(p\) of length \(n\), such that the minimum number of carriage return operations needed to make \(a = p\) is the same no matter which typewriter Misuki is using.
有一个长度为 \(n\) 的整数序列 \(a\) ,其中每个元素的初始值为 \(-1\) 。
美雪有两台打字机,第一台打字机从左往右写字母,指针最初指向 \(1\) ,另一台打字机从右往左写字母,指针最初指向 \(n\) 。
美雪会选择其中一台打字机进行以下操作,直到 \(a\) 变成 \([1, 2, \ldots, n]\) 的排列。
- 写数:将 \(a\) 中未出现的最小正整数写入 \(a_i\) , \(i\) 是指针指向的位置。这种操作只能在 \(a_i = -1\) 时执行。
- 回车:将指针返回到初始位置(第一台打字机为 \(1\) ,第二台打字机为 \(n\) )。
- 移动指针:将指针移动到下一个位置,假设 \(i\) 是指针在执行此操作前所指向的位置,如果美雪使用的是第一台打字机,则为 \(i := i + 1\) ,否则为 \(i := i - 1\) 。只有在操作之后, \(1 \le i \le n\) 成立时,才能执行该操作。
你的任务是构造长度为 \(n\) 的任意排列 \(p\) ,使得无论美雪使用哪台打字机, \(a = p\) 所需的最小回车操作次数都相同。
Input
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 500\)) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) — the length of the permutation.
It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
输入
每个测试包含多个测试用例。第一行输入包含一个整数 \(t\) ( \(1 \le t \le 500\) ) - 测试用例的数量。测试用例说明如下。
每个测试用例的第一行都包含一个整数 \(n\) ( \(1 \le n \le 2 \cdot 10^5\) ) - 排列的长度。
保证所有测试用例的 \(n\) 之和不超过 \(2 \cdot 10^5\) 。
Output
For each test case, output a line of \(n\) integers, representing the permutation \(p\) of length \(n\) such that the minimum number of carriage return operations needed to make \(a = p\) is the same no matter which typewriter Misuki is using, or \(-1\) if it is impossible to do so.
If there are multiple valid permutations, you can output any of them.
输出
对于每个测试用例,输出一行 \(n\) 整数,表示长度为 \(n\) 的排列 \(p\) ,使得无论美雪使用哪台打字机,都能实现 \(a = p\) 所需的最少回车操作次数相同,如果无法实现,则输出 \(-1\) 。
如果存在多种有效的排列组合,则可以输出其中任何一种。
Example
Input
3
1
2
3
Output
1
-1
3 1 2
Note
In the first testcase, it's possible to make \(a = p = [1]\) using \(0\) carriage return operations.
In the second testcase, it is possible to make \(a = p = [1, 2]\) with the minimal number of carriage returns as follows:
If Misuki is using the first typewriter:
- Write number: write \(1\) to \(a_1\), \(a\) becomes \([1, -1]\)
- Move pointer: move the pointer to the next position. (i.e. \(2\))
- Write number: write \(2\) to \(a_2\), \(a\) becomes \([1, 2]\)
If Misuki is using the second typewriter:
- Move pointer: move the pointer to the next position. (i.e. \(1\))
- Write number: write \(1\) to \(a_1\), \(a\) becomes \([1, -1]\)
- Carriage return: return the pointer to \(2\).
- Write number: write \(2\) to \(a_2\), \(a\) becomes \([1, 2]\)
It can be proven that the minimum number of carriage returns needed to transform \(a\) into \(p\) when using the first typewriter is \(0\) and it is \(1\) when using the second one, so this permutation is not valid.
Similarly, \(p = [2, 1]\) is also not valid, so there is no solution for \(n = 2\).
In the third testcase, it is possibile to make \(a = p = [3, 1, 2]\) with \(1\) carriage return with both the first and the second typewriter. It can be proven that, for both typewriters, it is impossible to write \(p\) with \(0\) carriage returns.
With the first typewriter it is possible to:
- Move pointer: move the pointer to the next position. (i.e. \(2\))
- Write number: write \(1\) to \(a_2\), \(a\) becomes \([-1, 1, -1]\)
- Move pointer: move the pointer to the next position. (i.e. \(3\))
- Write number: write \(2\) to \(a_3\), \(a\) becomes \([-1, 1, 2]\)
- Carriage return: return the pointer to \(1\).
- Write number: write \(3\) to \(a_1\), \(a\) becomes \([3,1,2]\)
With the second typewriter it is possible to:
- Move pointer: move the pointer to the next position. (i.e. \(2\))
- Write number: write \(1\) to \(a_2\), \(a\) becomes \([-1, 1, -1]\)
- Carriage return: return the pointer to \(3\).
- Write number: write \(2\) to \(a_3\), \(a\) becomes \([-1, 1, 2]\)
- Move pointer: move the pointer to the next position. (i.e. \(2\))
- Move pointer: move the pointer to the next position. (i.e. \(1\))
- Write number: write \(3\) to \(a_1\), \(a\) becomes \([3, 1, 2]\)
注
在第一个测试用例中,可以使用 \(0\) 回车操作制作 \(a = p = [1]\) 。
在第二个测试用例中,可以使用 $$\(0\)$$ 回车操作制作 \(a = p = [1, 2]\) ,最小回车次数如下:
如果美树使用的是第一台打字机:
- 写数字:将 \(1\) 写入 \(a_1\) , \(a\) 变为 \([1, -1]\)
- 移动指针:将指针移动到下一个位置。(即 \(2\) )
- 写入数字:将 \(2\) 写入 \(a_2\) , \(a\) 变为 \([1, 2]\) 。
如果美雪使用的是第二台打字机:
- 移动指针:将指针移动到下一个位置。(即 \(1\) )
- 写入数字:将 \(1\) 写入 \(a_1\) , \(a\) 变为 \([1, -1]\) 。
- 回车:返回指向 \(2\) 的指针。
- 写入数字:将 \(2\) 写入 \(a_2\) , \(a\) 变为 \([1, 2]\) 。
可以证明,使用第一台打字机时,将 \(a\) 转换为 \(p\) 所需的最少回车次数为 \(0\) ,而使用第二台打字机时则为 \(1\) ,因此这种排列是无效的。
同样, \(p = [2, 1]\) 也是无效的,因此 \(n = 2\) 无解。
题意
你需要在一个水平的序列中从 \(1\) 打印到 \(n\)
你有两台打字机
- 一台从最左边开始打印,每次只能打印/向右走一格/回到初始位置
- 一台从最右边开始打印,每次只能打印/向左走一格/回到初始位置
每次打印只能打印 \(上一个数字+1\)
你需要给出一个序列,使得打印这个序列时
不管选择哪一台打印机,回到初始位置的操作数相同
- 如果存在这样的序列,那就从左到右打印这个序列
- 如果不存在这样的序列,那就输出 \(-1\)
题解
这题实际上考察了思维和构造
怎么构造这样的一个序列呢?
其实样例给了我们提示
我们只要类似于把数字在两边交替放置,一直到中间即可
比如,当 \(n = 11\) 时
序列为 \(1,3,5,7,9,11,10,8,6,4,2\)
这样的话,就可以不管从哪边开始打字,
返回初始位置次数都是一样的了
但是,如果 \(n\) 是偶数的话就只能输出 \(-1\)
构造不了序列
代码
#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 阅读(7) 评论(0) 编辑 收藏 举报