Array and Peaks
题目描述
A sequence of n n n integers is called a permutation if it contains all integers from 1 1 1 to n n n exactly once.
Given two integers n n n and k k k , construct a permutation a a a of numbers from 1 1 1 to n n n which has exactly k k k peaks. An index i i i of an array a a a of size n n n is said to be a peak if 1 < i < n 1 < i < n 1<i<n and a i > a i − 1 a_i \gt a_{i-1} ai>ai−1 and a i > a i + 1 a_i \gt a_{i+1} ai>ai+1 . If such permutation is not possible, then print − 1 -1 −1 .
输入格式
The first line contains an integer t t t ( 1 ≤ t ≤ 100 1 \leq t \leq 100 1≤t≤100 ) — the number of test cases.
Then $ t $ lines follow, each containing two space-separated integers $ n $ ( 1 ≤ n ≤ 100 1 \leq n \leq 100 1≤n≤100 ) and k k k ( 0 ≤ k ≤ n 0 \leq k \leq n 0≤k≤n ) — the length of an array and the required number of peaks.
输出格式
Output t t t lines. For each test case, if there is no permutation with given length and number of peaks, then print − 1 -1 −1 . Otherwise print a line containing n n n space-separated integers which forms a permutation of numbers from 1 1 1 to n n n and contains exactly k k k peaks.
If there are multiple answers, print any.
样例 #1
样例输入 #1
5
1 0
5 2
6 6
2 1
6 1
样例输出 #1
1
2 4 1 5 3
-1
-1
1 3 6 5 4 2
提示
In the second test case of the example, we have array a = [ 2 , 4 , 1 , 5 , 3 ] a = [2,4,1,5,3] a=[2,4,1,5,3] . Here, indices i = 2 i=2 i=2 and i = 4 i=4 i=4 are the peaks of the array. This is because $(a_{2} \gt a_{1} $ , a 2 > a 3 ) a_{2} \gt a_{3}) a2>a3) and ( a 4 > a 3 (a_{4} \gt a_{3} (a4>a3 , a 4 > a 5 ) a_{4} \gt a_{5}) a4>a5) .
题面翻译
题目描述
对于一列 n n n 个整数组成的数列,如果这个序列包括 1 1 1 到 n n n 的每个数字(即每个数字只出现一次),那么这个数列就叫做一个“排列”。
给您两个整数 n n n 和 k k k,请您构造一个排列 a a a,要求这个排列符合上述要求并且要有 k k k 个峰值。(如果在这个排列 a a a 中的一个元素 i i i 满足 1 < i < n 1 < i < n 1<i<n , a i > a i − 1 a_i > a_{i - 1} ai>ai−1 且 a i > a i + 1 a_i > a_{i + 1} ai>ai+1,那么这个 i i i 就叫峰值)。如果给定条件构造不出符合要求的排列,输出 − 1 -1 −1。
输入格式
第一行是一个整数 t ( 1 ≤ t ≤ 100 ) t (1 \leq t \leq 100) t(1≤t≤100) 代表数据测试组数。
从第二行到第 t + 1 t + 1 t+1 行,每行包括两个数字 n ( 1 ≤ n ≤ 100 ) n (1 \leq n \leq 100) n(1≤n≤100) 和 k ( 0 ≤ k ≤ n ) k (0 \leq k \leq n) k(0≤k≤n)。
输出格式
输出 t t t 行,对于每行,如果没有符合要求的排列,那么输出 − 1 -1 −1,否则输出一行 n n n 个数字——符合要求的排列。
如果有多组答案,随意输出一组。
说明/提示
在样例的第二组测试数据中,我们可以构造一个排列 a = [ 2 , 4 , 1 , 5 , 3 ] a = [2, 4, 1, 5, 3] a=[2,4,1,5,3]。 i = 2 i = 2 i=2 和 i = 4 i = 4 i=4 是这个排列的峰值,因为 a 2 > a 1 , a 2 > a 3 a_2 > a_1, a_2 > a_3 a2>a1,a2>a3 并且 a 4 > a 3 , a 4 > a 5 a_4 > a_3, a_4 > a_5 a4>a3,a4>a5。
思路:
初始化数组 a[n]
,a[i] = i
,保证
1
1
1 到
n
n
n 出现,如果
n
n
n 是偶数,那么最多有
n
÷
2
−
1
n \div 2 - 1
n÷2−1 个峰,奇数有
n
÷
2
n \div 2
n÷2 个峰,
k
k
k 大于最大峰,直接输出 -1
,否则就输出序列。
序列的输出:每次从取最后一个数(也是最大的数,用 sort
保证),插到前面两个小的数字中间。
举例:
n = 5 n=5 n=5, k = 2 k=2 k=2, m a x n = n ÷ 2 = 2 ≤ k maxn = n \div 2 = 2 \le k maxn=n÷2=2≤k,所以应该构造序列。
初始化
12345
1 2 3 4 5
12345 第一次交换
2
2
2 和
5
5
5,变成
15342
1 5 3 4 2
15342,再 sort
排序后边变成
15234
1 5 2 3 4
15234。
这样就完成了把最大的数插到两个小数之间。
代码:
#include<bits/stdc++.h>
using namespace std;
int t,n,m,a[1000001];
int main(){
cin>>t;
while(t--){
cin>>n>>m;
if(m>(n-1)/2){
cout<<-1<<endl;
}else{
for(int i=1;i<=m;i++){
cout<<i<<" "<<(n-i+1)<<" ";
}
for(int i=m+1;i<=n-m;i++){
cout<<i<<" ";
}
cout<<endl;
}
}
return 0;
}