题面
You are given an integer . Construct a permutation of length satisfying the following condition:
- is divisible by for every .
It can be proven that such a permutation always exists under the constraints of the problem.
A permutation of length is an array consisting of distinct integers from to in arbitrary order. For example, is a permutation, but is not a permutation ( appears twice in the array), and is also not a permutation ( but there is in the array).
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The only line of each test case contains a single integer () — the length of the permutation to be constructed.
Output
For each test case, output integers (, all -s are distinct) — the permutation you constructed.
If there are multiple valid permutations, you may output any of them.
思路
这题是一道构造题,按照题意,我们需要一个排列,满足:
在条件: 下, 都可被 整除
因此,如果我们让这个排列满足: ,即可满足题目的要求
因此我们构造: 即可
在构造的时候,我们使用双指针维护两端,最终得出结果
代码
#include <bits/stdc++.h>
using namespace std;
int main() { int t; cin >> t; while (t --) { int n; cin >> n; vector<int> p(n); int l = 1, r = n; int i = n - 1; for (; i > 0;) { p[i] = l; i --; p[i] = r; i --;
l ++; r --; }
if (l == r) { p[i] = l; }
for (int i = 0; i < n; i ++) { cout << p[i] << " \n"[i == n - 1]; } }}