Codeforces Round #275 (Div. 1)A. Diverse Permutation 构造

Codeforces Round #275 (Div. 1)A. Diverse Permutation

Time Limit: 1 Sec  Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/482/problem/A

Description

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.

Sample Input

Input
3 2
 
Input
3 1
 
Input
5 2

Sample Output

Output
1 3 2
Output
1 2 3
Output
1 3 2 4 5

HINT

By |x| we denote the absolute value of number x.

题意

从1-n的数,让你选择一些数来构造,要求每个相邻的数之间的绝对值之差有k种

题解:

按照1,n,2,n-1,3,n-2……这样子构造k-1组,然后把剩下没有遍历的都输出一下就好了

然后还有一种构造是n,1,2,n-1,3,n-2 这样子构造k-1组

这两种构造方法相差1,分别是对应k为奇数和偶数的情况

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
//const int inf=0x7fffffff;   //无限大
const int inf=0x3f3f3f3f;
/*

*/
//**************************************************************************************

inline ll read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int main()
{
    int flag[maxn];
    int n,k;
    int ans=0;
    cin>>n>>k;
    if(k%2==0)
    {
        for(int i=0;i<k-1;i++)
        {
            if(i%2)
            {
                cout<<ans<<" ";
                flag[ans]=1;
            }
            else
            {
                cout<<n-ans<<" ";
                flag[n-ans]=1;
                ans++;
            }
        }
    }
    else
    {
        for(int i=0;i<k-1;i++)
        {
            if(i%2==0)
            {
                cout<<ans+1<<" ";
                flag[ans+1]=1;
            }
            else
            {
                cout<<n-ans<<" ";
                flag[n-ans]=1;
                ans++;
            }
        }
    }

    for(int i=1;i<=n;i++)
    {
        if(flag[i]==0)
            cout<<i<<" ";
    }
    return 0;

}
原文地址:https://www.cnblogs.com/qscqesze/p/4385652.html