Codeforces: Diverse Permutation (find rule), find rule
**** ***************************
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, too many connections,P2, please wait..., please wait again ,...,PN, ConsistingNDistinct positive integers not largerN. We'll denoteNThe length of permutationP1, too many connections,P2, please wait..., please wait again ,...,PN.
Your task is to find such permutationPOf lengthN, That the group of numbers |P1 bytes-elapsed-P2 |, bytes |P2 bytes-elapsed-P3 |, large..., large |PNAccept-ranges 1 accept-ranges-PN| Has exactlyKDistinct elements.
Input
The single line of the input contains two space-separated positive integersN,K(1 digit ≤ DigitKLatency <latencyNLimit ≤ limit 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. Because the given 1 limit ≤ limitKLatency <latencyNLimit ≤ limit 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 question: How can I attack others in codeforces?
For a question, you must first pass TEST. Then return to the PROBLEM list of the game, lock the lock behind the question (the lock won't be submitted again, so don't lock it if you're unsure), and then go to the ROOM, you can check other people's Code. There is a HACK button below. Click it and enter the example that you think is wrong.
What is codeforces?
.. Thank you!