D. Super-Permutation
A permutation is a sequence $n$ integers, where each integer from $1$ to $n$ appears exactly once. For example, $[1]$, $[3,5,2,1,4]$, $[1,3,2]$ are permutations, while $[2,3,2]$, $[4,3,1]$, $[0]$ are not.
Given a permutation $a$, we construct an array $b$, where $b_i = (a_1 + a_2 +~\dots~+ a_i) \bmod n$.
A permutation of numbers $[a_1, a_2, \dots, a_n]$ is called a super-permutation if $[b_1 + 1, b_2 + 1, \dots, b_n + 1]$ is also a permutation of length $n$.
Grisha became interested whether a super-permutation of length $n$ exists. Help him solve this non-trivial problem. Output any super-permutation of length $n$, if it exists. Otherwise, output $-1$.
Input
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The description of the test cases follows.
Each test case consists of a single line containing one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the desired permutation.
The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
Output
For each test case, output in a separate line:
- $n$ integers — a super-permutation of length $n$, if it exists.
- $-1$, otherwise.
If there are several suitable permutations, output any of them.
Example
input
4 1 2 3 6
output
1 2 1 -1 6 5 2 3 4 1
解题思路
nmd思维构造题比赛结束了都没想出来怎么做,被搞到心态爆炸后面的题没都多少时间做了,后面所谓的“难题”都比这题要简单多了好吧。
以后遇到这种题直接小数据打表找规律,想推导纯纯在浪费时间。
打个表先,把$10$以内满足条件的排列都暴搜出来:
#include <bits/stdc++.h> using namespace std; const int N = 20; int p[N]; bool vis[N], st[N]; void dfs(int u, int s, int n) { if (u > n) { for (int i = 1; i <= n; i++) { printf("%d ", p[i]); } printf("\n"); return; } for (int i = 1; i <= n; i++) { int t = (s + i) % n + 1; if (!vis[i] && !st[t]) { vis[i] = st[t] = true; p[u] = i; dfs(u + 1, t - 1, n); vis[i] = st[t] = false; } } } int main() { int n = 10; for (int i = 1; i <= n; i++) { printf("%d:\n", i); dfs(1, 0, i); printf("\n"); } return 0; }打表代码
1: 1 2: 2 1 3: 4: 4 1 2 3 4 3 2 1 5: 6: 6 1 4 3 2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 5 7 4 2 1 10 9 8 7 2 6 1 5 3 4 10 9 8 7 4 3 5 6 1 2打表结果
可以发现除了$1$以外的奇数都是没有解的,因此直接特判$1$,否则如果是奇数那么直接输出$-1$。然后偶数必然有解,其中对于长度为$n$的排列第一个数必然是$p_1 = n$。继续从小数据往大数据看必然存在$p_{n/2+1} = \frac{n}{2}$的排列。继续看$n=6$的情况,发现在排列的左半边(即$[1,\frac{n}{2}]$)中的$1$都与右半边的$4$对应,左半边的$5$都与右半边的$2$对应,并且$1$和$2$不能同时出现在左半边。因此直接乱猜结论:先构造出$p = [n, n-1, \ldots, 1]$,然后$i$从$2$开始枚举到$\frac{n}{2}$,迭代步长为$2$,然后交换$p_i$与$p_{n-i+2}$。例如当$n=8$,那么构造出序列$p = [8,3,6,1,4,7,2,5]$。然后这样做发现确实可以过。
AC代码如下:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int N = 2e5 + 10; 5 6 int p[N]; 7 8 void solve() { 9 int n; 10 scanf("%d", &n); 11 if (n & 1) { 12 if (n == 1) printf("1\n"); 13 else printf("-1\n"); 14 return; 15 } 16 for (int i = 1; i <= n; i++) { 17 p[i] = n - i + 1; 18 } 19 for (int i = 2; i <= n >> 1; i += 2) { 20 swap(p[i], p[n - i + 2]); 21 } 22 for (int i = 1; i <= n; i++) { 23 printf("%d ", p[i]); 24 } 25 printf("\n"); 26 } 27 28 int main() { 29 int t; 30 scanf("%d", &t); 31 while (t--) { 32 solve(); 33 } 34 35 return 0; 36 }
直接给出官方题解的思路,这种题是真没意思。
令$k$为$n$在排列中的位置,即$p_k = n$,可以证明$k$必然等于$1$。否则如果$k>1$,那么$b_k = (b_{k-1} + p_k) \bmod n = b_{k-1}$,这就与题意矛盾了。
如果$n>1$并且为奇数,那么有$b_n = (a_1 + a_2 + \cdots + a_n) \bmod n = (1 + 2 + \cdots + n) \bmod n = n \cdot \frac{n + 1}{2} \bmod n = 0 = b_1$,因此无解。
如果$n$是偶数,一种可行的构造方案是$p = [n,~1,~n-2,~3,~n-4,~5,~\ldots,~n - 1,~2]$,这是因为$b = [0,~1,~n-1,~2,~n-2,~3,~n-3,~\dots,~\frac{n}{2}]$。
AC代码如下:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 void solve() { 5 int n; 6 scanf("%d", &n); 7 if (n & 1) { 8 if (n == 1) printf("1\n"); 9 else printf("-1\n"); 10 return; 11 } 12 for (int i = 0; i < n; i++) { 13 if (i & 1) printf("%d ", i); 14 else printf("%d ", n - 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 #867 (Div. 3) Editorial:https://codeforces.com/blog/entry/115409
标签:10,int,bmod,printf,Permutation,length,permutation,Super From: https://www.cnblogs.com/onlyblues/p/17357073.html