Diverse Permutation
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u 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.
1 #include2 #include 3 int main() 4 { 5 int n,k; 6 int i,j; 7 int a[100005]; 8 while(scanf("%d %d",&n,&k)!=EOF) 9 {10 memset(a,0,sizeof(a));11 int j=k+2,num=1;12 a[1]=1;13 printf("1");14 while(k)15 {16 if(num-k>0 && a[num-k]==0)17 {18 printf(" %d",num-k);19 num=num-k;20 a[num]=1;21 }22 else23 {24 printf(" %d",num+k);25 num=num+k;26 a[num]=1;27 }28 k--;29 }30 for(i=j;i<=n;i++)31 {32 printf(" %d",i);33 }34 printf("\n");35 } 36 return 0;37 }