Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote asn 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.
The single line of the input contains two space-separated positive integers n, k (1 ≤ k < n ≤ 105).
Print n integers forming the permutation. If there are multiple answers, print any of them.
3 2
1 3 2
3 1
1 2 3
5 2
1 3 2 4 5
By |x| we denote the absolute value of number x.
题解:第一个数设为a[1]=1,后一个数a[2]=a[1]+k,a[3]=a[2]-(k-1),a[4]=a[3]+(k-2),依次构造下去,剩下的数一定是连续的让他们按从小到大排下去,不会增加distinct elements的个数,
#include<cstdio>
#include<queue>
#include<string.h>
#include<algorithm>
#include<stack>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define FIN freopen("in.txt","r",stdin)
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int maxn = 1e5 + 5;
int a[maxn];
bool vis[maxn];
int main(){
int n,k;
scanf("%d%d",&n,&k);
a[0]=1;
for(int i=1;i<=k;i++){
if(i%2) a[i]=a[i-1]+k-i+1;
else a[i]=a[i-1]-(k-i+1);
}
for(int i=k+1;i<n;i++){
a[i]=i+1;
}
for(int i=0;i<n;i++) printf("%d%c",a[i],i==n-1?'\n':' ');
return 0;
}