Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote as n 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.
找规律咯
按照 ...3 k-1 2 k 1 k+1 k+2 ... n
的排法
#include<iostream> #include<cstring> #include<cstdio> #include <string> #include <sstream> #include <map> #include <cmath> #include <algorithm> #include <iomanip> #include <stack> #include <queue> #include <set> using namespace std; typedef long long LL; #define MOD 1000000007 int n,k; int a[100005]; int main(){ // freopen("test.in","r",stdin); cin >> n >> k; int now = 1,nowk = k,dist = k,direct = 1,total(0); while (dist >= 1){ if (direct == 1){ total ++; a[total] = now; now ++; } else { total ++; a[total] = nowk; nowk --; } direct = 1 - direct; dist --; } for (int i=total;i>=1;i--){ cout << a[i] << " "; } for (int i=k+1;i<=n;i++){ cout << i << " "; } return 0; }