题解: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 a a of length n n n, where each element is initially − 1 -1 1.

Misuki has two typewriters where the first one writes letters from left to right, with a pointer initially pointing to 1 1 1, and another writes letters from right to left with a pointer initially pointing to n n n.

Misuki would choose one of the typewriters and use it to perform the following operations until a a a becomes a permutation of [ 1 , 2 , … , n ] [1, 2, \ldots, n] [1,2,,n]

  • write number: write the minimum positive integer that isn’t present in the array a a a to the element a i a_i ai, i i i is the position where the pointer points at. Such operation can be performed only when a i = − 1 a_i = -1 ai=1.
  • carriage return: return the pointer to its initial position (i.e. 1 1 1 for the first typewriter, n n n for the second)
  • move pointer: move the pointer to the next position, let i i i be the position the pointer points at before this operation, if Misuki is using the first typewriter, i : = i + 1 i := i + 1 i:=i+1 would happen, and i : = i − 1 i := i - 1 i:=i1 otherwise. Such operation can be performed only if after the operation, 1 ≤ i ≤ n 1 \le i \le n 1in holds.

Your task is to construct any permutation p p p of length n n n, such that the minimum number of carriage return operations needed to make a = p a = p a=p is the same no matter which typewriter Misuki is using.

有一个长度为 n n n 的整数序列 a a a ,其中每个元素的初始值为 − 1 -1 1

美雪有两台打字机,第一台打字机从左往右写字母,指针最初指向 1 1 1 ,另一台打字机从右往左写字母,指针最初指向 n n n

美雪会选择其中一台打字机进行以下操作,直到 a a a 变成 [ 1 , 2 , … , n ] [1, 2, \ldots, n] [1,2,,n] 的排列。

  • 写数:将 a a a 中未出现的最小整数写入 a i a_i ai i i i 是指针指向的位置。这种操作只能在 a i = − 1 a_i = -1 ai=1 时执行。
  • 回车:将指针返回到初始位置(第一台打字机为 1 1 1 ,第二台打字机为 n n n )。
  • 移动指针:将指针移动到下一个位置,假设 i i i 是指针在执行此操作前所指向的位置,如果美雪使用的是第一台打字机,则为 i : = i + 1 i := i + 1 i:=i+1 ,否则为 i : = i − 1 i := i - 1 i:=i1 。只有在操作之后, 1 ≤ i ≤ n 1 \le i \le n 1in 成立时,才能执行该操作。

你的任务是构造长度为 n n n 的任意排列 p p p ,使得无论美雪使用哪台打字机, a = p a = p a=p 所需的最小回车操作次数都相同。

Input

Each test contains multiple test cases. The first line of input contains a single integer t t t ( 1 ≤ t ≤ 500 1 \le t \le 500 1t500) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 1n2105) — the length of the permutation.

It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

输入

每个测试包含多个测试用例。第一行输入包含一个整数 t t t ( 1 ≤ t ≤ 500 1 \le t \le 500 1t500 ) - 测试用例的数量。测试用例说明如下。

每个测试用例的第一行都包含一个整数 n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 1n2105 ) - 排列的长度。

保证所有测试用例的 n n n 之和不超过 2 ⋅ 1 0 5 2 \cdot 10^5 2105

Output

For each test case, output a line of n n n integers, representing the permutation p p p of length n n n such that the minimum number of carriage return operations needed to make a = p a = p a=p is the same no matter which typewriter Misuki is using, or − 1 -1 1 if it is impossible to do so.

If there are multiple valid permutations, you can output any of them.

输出

对于每个测试用例,输出一行 n n n 整数,表示长度为 n n n 的排列 p p p ,使得无论美雪使用哪台打字机,都能实现 a = p a = p a=p 所需的最少回车操作次数相同,如果无法实现,则输出 − 1 -1 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 ] a = p = [1] a=p=[1] using 0 0 0 carriage return operations.

In the second testcase, it is possible to make a = p = [ 1 , 2 ] a = p = [1, 2] a=p=[1,2] with the minimal number of carriage returns as follows:

If Misuki is using the first typewriter:

  • Write number: write 1 1 1 to a 1 a_1 a1, a a a becomes [ 1 , − 1 ] [1, -1] [1,1]
  • Move pointer: move the pointer to the next position. (i.e. 2 2 2)
  • Write number: write 2 2 2 to a 2 a_2 a2, a a a becomes [ 1 , 2 ] [1, 2] [1,2]

If Misuki is using the second typewriter:

  • Move pointer: move the pointer to the next position. (i.e. 1 1 1)
  • Write number: write 1 1 1 to a 1 a_1 a1, a a a becomes [ 1 , − 1 ] [1, -1] [1,1]
  • Carriage return: return the pointer to 2 2 2.
  • Write number: write 2 2 2 to a 2 a_2 a2, a a a becomes [ 1 , 2 ] [1, 2] [1,2]

It can be proven that the minimum number of carriage returns needed to transform a a a into p p p when using the first typewriter is 0 0 0 and it is 1 1 1 when using the second one, so this permutation is not valid.

Similarly, p = [ 2 , 1 ] p = [2, 1] p=[2,1] is also not valid, so there is no solution for n = 2 n = 2 n=2.

In the third testcase, it is possibile to make a = p = [ 3 , 1 , 2 ] a = p = [3, 1, 2] a=p=[3,1,2] with 1 1 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 p p with 0 0 0 carriage returns.

With the first typewriter it is possible to:

  • Move pointer: move the pointer to the next position. (i.e. 2 2 2)
  • Write number: write 1 1 1 to a 2 a_2 a2, a a a becomes [ − 1 , 1 , − 1 ] [-1, 1, -1] [1,1,1]
  • Move pointer: move the pointer to the next position. (i.e. 3 3 3)
  • Write number: write 2 2 2 to a 3 a_3 a3, a a a becomes [ − 1 , 1 , 2 ] [-1, 1, 2] [1,1,2]
  • Carriage return: return the pointer to 1 1 1.
  • Write number: write 3 3 3 to a 1 a_1 a1, a a a becomes [ 3 , 1 , 2 ] [3,1,2] [3,1,2]

With the second typewriter it is possible to:

  • Move pointer: move the pointer to the next position. (i.e. 2 2 2)
  • Write number: write 1 1 1 to a 2 a_2 a2, a a a becomes [ − 1 , 1 , − 1 ] [-1, 1, -1] [1,1,1]
  • Carriage return: return the pointer to 3 3 3.
  • Write number: write 2 2 2 to a 3 a_3 a3, a a a becomes [ − 1 , 1 , 2 ] [-1, 1, 2] [1,1,2]
  • Move pointer: move the pointer to the next position. (i.e. 2 2 2)
  • Move pointer: move the pointer to the next position. (i.e. 1 1 1)
  • Write number: write 3 3 3 to a 1 a_1 a1, a a a becomes [ 3 , 1 , 2 ] [3, 1, 2] [3,1,2]

在第一个测试用例中,可以使用 0 0 0 回车操作制作 a = p = [ 1 ] a = p = [1] a=p=[1]

在第二个测试用例中,可以使用 $ 0 0 0$ 回车操作制作 a = p = [ 1 , 2 ] a = p = [1, 2] a=p=[1,2] ,最小回车次数如下:

如果美树使用的是第一台打字机:

  • 写数字:将 1 1 1 写入 a 1 a_1 a1 a a a 变为 [ 1 , − 1 ] [1, -1] [1,1]
  • 移动指针:将指针移动到下一个位置。(即 2 2 2 )
  • 写入数字:将 2 2 2 写入 a 2 a_2 a2 a a a 变为 [ 1 , 2 ] [1, 2] [1,2]

如果美雪使用的是第二台打字机:

  • 移动指针:将指针移动到下一个位置。(即 1 1 1 )
  • 写入数字:将 1 1 1 写入 a 1 a_1 a1 a a a 变为 [ 1 , − 1 ] [1, -1] [1,1]
  • 回车:返回指向 2 2 2 的指针。
  • 写入数字:将 2 2 2 写入 a 2 a_2 a2 a a a 变为 [ 1 , 2 ] [1, 2] [1,2]

可以证明,使用第一台打字机时,将 a a a 转换为 p p p 所需的最少回车次数为 0 0 0 ,而使用第二台打字机时则为 1 1 1 ,因此这种排列是无效的。

同样, p = [ 2 , 1 ] p = [2, 1] p=[2,1] 也是无效的,因此 n = 2 n = 2 n=2 无解。

题意

你需要在一个水平的序列中从 1 1 1 打印到 n n n
你有两台打字机

  • 一台从最左边开始打印,每次只能打印/向右走一格/回到初始位置
  • 一台从最右边开始打印,每次只能打印/向左走一格/回到初始位置

每次打印只能打印 上一个数字 + 1 上一个数字+1 上一个数字+1
你需要给出一个序列,使得打印这个序列时
不管选择哪一台打印机,回到初始位置的操作数相同

  • 如果存在这样的序列,那就从左到右打印这个序列
  • 如果不存在这样的序列,那就输出 − 1 -1 1

题解

这题实际上考察了思维构造
怎么构造这样的一个序列呢?
其实样例给了我们提示
我们只要类似于把数字在两边交替放置,一直到中间即可
比如,当 n = 11 n = 11 n=11
序列为 1 , 3 , 5 , 7 , 9 , 11 , 10 , 8 , 6 , 4 , 2 1,3,5,7,9,11,10,8,6,4,2 1,3,5,7,9,11,10,8,6,4,2
这样的话,就可以不管从哪边开始打字,
返回初始位置次数都是一样的了

但是,如果 n n n 是偶数的话就只能输出 − 1 -1 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;
}

有什么建议或者意见的欢迎在评论区留言!

转载自博客https://www.cnblogs.com/jiejiejiang2004/p/18372009
博主已同意,我就是博主

  • 7
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,根据提供的引用内容,我无法理解你具体想要问什么问题。请提供更清晰明确的问题,我将竭诚为你解答。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Codeforces Round 860 (Div. 2)题解](https://blog.csdn.net/qq_60653991/article/details/129802687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [【CodeforcesCodeforces Round 865 (Div. 2) (补赛)](https://blog.csdn.net/t_mod/article/details/130104033)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Codeforces Round 872 (Div. 2)(前三道](https://blog.csdn.net/qq_68286180/article/details/130570952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值