Description
Permutationp is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote asn the length of permutation p1, p2, ..., pn.
Your task is to find such permutation p of length n, that the group of numbers |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
Input
The single line of the input contains two space-separated positive integers n, k (1 ≤ k < n ≤ 105).
Output
Print n integers forming the permutation. If there are multiple answers, print any of them.
Sample Input
Input
3 2
Output
1 3 2
Input
3 1
Output
1 2 3
Input
5 2
Output
1 3 2 4 5
Hint
By |x| we denote the absolute value of number x.
题意是说找到找到一组n个由1..n组成的数列,且每个数字只出现一次
满足每相邻的两项的差的绝对值一共有k种。
找规律即可。不好描述,直接上代码吧。
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 #include <cstdio> 5 #include <cmath> 6 using namespace std; 7 int n,k; 8 int tmp,p; 9 const int N=1E5+7; 10 int a[N]; 11 12 int main() 13 { 14 15 scanf("%d %d",&n,&k); 16 for ( int i = 1; i <= n ; i++ ) 17 a[i] = i; 18 tmp = k; 19 p = 1; 20 for ( int i = 2 ; i <= k+1 ; i++) 21 { 22 a[i] = a[i-1] + tmp*p; 23 tmp--; 24 p=p*-1; 25 } 26 for ( int i = 1 ; i < n ; i++ ) 27 printf("%d ",a[i]); 28 printf("%d",a[n]); 29 30 return 0; 31 }