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

本文介绍了一个Codeforces竞赛中的数学算法问题,目标是在给定长度和差值数量的情况下,构造一个排列使得相邻元素间的差值恰好有指定数量的种类。详细阐述了解题思路并提供了实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值