[Topic link]
Https://codeforces.com/contest/482/problem/A
Algorithm
First, a sequence of (k + 1) numbers is constructed to satisfy their difference of 1-k
For i > k + 1, make AI = i
Time complexity: O (N)
Code
#include <bits/stdc++.h>using namespacestd;template<typename t> InlinevoidChkmax (T &x,t y) {x =Max (x, y);} Template<typename t> InlinevoidChkmin (T &x,t y) {x =min (x, y);} Template<typename t> InlinevoidRead (T &x) {T F=1; x =0; Charc =GetChar (); for(;!isdigit (c); c = GetChar ())if(c = ='-') F =-F; for(; IsDigit (c); c = GetChar ()) x = (x <<3) + (x <<1) + C-'0'; X*=F;}intMain () {intN, K; Read (n); Read (k); printf ("1"); intValue = k, now =1; for(inti =1; I <= K; i++) { if(I &1) Now + =value; ElseNow-=value; printf ("%d", now); Value--; } for(inti = k +2; I <= N; i++) printf ("%d", i); printf ("\ n"); return 0; }
[Codeforces 482A] Diverse permutation