# Codeforces: diverse permutation (query rule)

Source: Internet
Author: User

**** ***************************

Original works, from the "Xiaofeng residual month XJ" blog, welcome to reprint, reprint please be sure to indicate the source (http://blog.csdn.net/xiaofengcanyuexj ).

For various reasons, there may be many shortcomings. Thank you!

**************************************** ***************************

Time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

PermutationPIs an ordered set of IntegersP1 ,???P2 ,???...,???PN, ConsistingNDistinct positive integers not largerN. We'll denoteNThe length of permutationP1 ,???P2 ,???...,???PN.

Your task is to find such PermutationPOf LengthN, That the group of numbers |P1? -?P2 | ,? |P2? -?P3 | ,?...,? |PN? -? 1? -?PN| Has exactlyKDistinct elements.

Input

The single line of the input contains two space-separated Positive IntegersN,K(1? ≤?K? <?N? ≤? 105 ).

Output

PrintNIntegers forming the permutation. If there are multiple answers, print any of them.

Sample test (s) Input
`3 2`
Output
`1 3 2`
Input
`3 1`
Output
`1 2 3`
Input
`5 2`
Output
`1 3 2 4 5`
Note

By |X| We denote the absolute value of numberX.

Question:

This question requires that a sequence of 1 to n be given, and the number of absolute values that meet the difference between two adjacent items is K. Given 1? ≤?K? <?N? ≤? 10Because the five fields are large, you can only search for algorithms with the time complexity of O (n. We can think of this sequence with a maximum of N-1 adjacent differences (absolute values), one of which meets the condition of the sequence is: N, 1, n-1, 2, n-3, 3 ............. You can try to construct a front K-1 that meets the conditions, and then fill in the order that follows.

Java source code:

`import java.util.Scanner;public class Main {private int k, n;public Main(int tn, int tk) {n = tn;k = tk;}public void printMain() {boolean flag = true;int count = k - 1;int i = 1;while (count > 0) {if (count != k - 1) {--count;if (count <= 0) {flag = false;break;}System.out.print(" ");}System.out.print(i);System.out.print(" " + (n - i + 1));++i;--count;}int j = n - i + 1;if (count != k - 1 && i <= j)System.out.print(" ");if (flag) {while (i <= j) {System.out.print(j);--j;if (i <= j)System.out.print(" ");}} else {while (i <= j) {System.out.print(i);++i;if (i <= j)System.out.print(" ");}}System.out.println();}public static void main(String[] args) {Scanner input = new Scanner(System.in);while (input.hasNext()) {int tn = input.nextInt();int tk = input.nextInt();Main AC = new Main(tn, tk);AC.printMain();}input.close();}}`

Due to time and level, it is inevitable that there are deficiencies. Thank you!

Codeforces: diverse permutation (query rule)

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.