Cf 275 (div.1) a diverse Permutation

Source: Internet
Author: User

Diverse Permutation

Question: 1 ~ N is a non-repeating integer sequence, and one of them needs to be printed, so that the absolute values of the two adjacent values of the new sequence are exactly K after deduplication.

(1 <= k <n <= 10 ^ 5)

Input: N, K

Output: new sequence

Thought about the sequence arrangement when k is the largest and K is the smallest value

1) K maximum time: K = n-1 sequence: 1, n, 2, n-1, 3, N-2 .... two Adjacent absolute values difference: n-1, N-2, n-3... 1

2) K hour: K = 1 sequence: 1, 2, 3... n | N, n-1, N-2... 1

If K is set to [1, n-1], then the values in the middle are combined for the two cases. The first section uses 1), and the remaining number uses 2)

When K is an odd number, 2) use ascending order; when K is an even number, 2) use descending order

Okay, Refer code

1 package golao.org. cf275; 2 3 Import Java. io. bufferedreader; 4 Import Java. io. ioexception; 5 import Java. io. inputstreamreader; 6 Import Java. util. arrays; 7 Import Java. util. stringtokenizer; 8 9 public class diversepermutation {10 11 public static void main (string [] ARGs) throws ioexception {12 bufferedreader BR = new bufferedreader (New inputstreamreader (system. in); 13 stringtokenizer STK = new stringtokenizer (BR. readline (); 14 int N, K; 15 N = integer. parseint (STK. nexttoken (); 16 k = integer. parseint (STK. nexttoken (); 17 system. out. println (print (findpermutation (n, k); 18 19} 20 public static int [] findpermutation (int n, int K) 21 {22 int [] ary = new int [N]; 23 for (INT I = 1; I <= N; I ++) {24 ary [I-1] = I; 25} 26 int [] aryresult = new int [N]; 27 int COUNT = n-1; 28 // previous section 29 for (Int J = 1, I = 0; j <= K; j + = 2, I ++) {30 aryresult [J-1] = ary [I]; 31 aryresult [J] = ary [count --]; 32} 33 // 34 if (K % 2 = 0) {35 for (INT Index = K; index <n; index ++) {36 aryresult [Index] = ary [count --]; 37} 38} else {39 for (INT Index = K; index <n; index ++) {40 aryresult [Index] = aryresult [index-1] + 1; 41} 42} 43 return aryresult; 44} 45 public static string print (INT [] ary) {46 stringbuffer sb = new stringbuffer (""); 47 for (INT I = 0; I <ary. length; I ++) {48 sb. append (ary [I]); 49 sb. append (""); 50} 51 return sb. tostring (); 52} 53 54}

Submitted to CF, run successfully, 77 Ms

Cf 275 (div.1) a diverse Permutation

Contact Us

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.