CodeForces - 482A Diverse Permutation

题目描述:

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 ≤ 10^{5}).

Output

Print n integers forming the permutation. If there are multiple answers, print any of them.

Examples

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 number x.

题目大意:

给你一个n和k。构造一个序列满足这个条件:{   |p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn|   } 存在k个不同的数。

解题报告:

1:列举几个样例,可以发现规律。1 k+1 2 k 3 k-1 4 k-2.....像这样的有k+1项。

2:从这样构造就已经满足题意,那么接下来的n-(k+1)项直接从k+2开始+1递增就行了。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 1e5+10;
ll ans[N];
int main(){
    ll n, k, cnt = 0;
    scanf("%lld%lld", &n, &k);
    ll s = 1, e = k+1;
    while(cnt < k+1){
        ans[cnt++] = s++;
        if(cnt >= k+1)break;
        ans[cnt++] = e--;
    }
    for(ll i=0; cnt<n; ++i){
        ans[cnt++] = k+2+i;
    }
    for(ll i=0; i<cnt; ++i){
        printf("%lld%s", ans[i], i == n-1 ? "\n" : " ");
    }
    return 0;
}
发布了164 篇原创文章 · 获赞 4 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/jun_____/article/details/104089130