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=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int main()
{
int flag[maxn];
int n,k;
int ans=;
cin>>n>>k;
if(k%==)
{
for(int i=;i<k-;i++)
{
if(i%)
{
cout<<ans<<" ";
flag[ans]=;
}
else
{
cout<<n-ans<<" ";
flag[n-ans]=;
ans++;
}
}
}
else
{
for(int i=;i<k-;i++)
{
if(i%==)
{
cout<<ans+<<" ";
flag[ans+]=;
}
else
{
cout<<n-ans<<" ";
flag[n-ans]=;
ans++;
}
}
} for(int i=;i<=n;i++)
{
if(flag[i]==)
cout<<i<<" ";
}
return ; }

最新文章

  1. iOS 字符串删除 DOM
  2. Improve Your Study Habits
  3. bash 中的case语法
  4. Redis学习手册(Hashes数据类型)
  5. iOS: 布局可视化语法 Visual Format Syntax
  6. (四)Angularjs - 小实例(2)
  7. 一些方便的bash命令
  8. laravel 汇总数据
  9. postgres 11 单实例最大支持多少个database?
  10. react 表单受控和非受控
  11. hibernate主配置文件的配置
  12. hadoop程序实例
  13. ant design的一些坑
  14. Activity优化几个结束的方法
  15. 本地项目通过github客户端上传到github网站上
  16. Texas Instruments matrix-gui-2.0 hacking -- helper_functions.php
  17. java并发编程(5)并发程序测试
  18. 无源码情况下直接修改jar里内容思路
  19. ExtJs学习-搭建开发环境
  20. [转] AS3地图拼接与战争迷雾的实现

热门文章

  1. 64_t7
  2. 2018 ICPC 徐州网络赛
  3. java基础36 双例集合Map下的HashMap和TreeMap集合
  4. 洛谷P1342请柬
  5. manacher模板
  6. yum安装Mysql-5.6
  7. NFS服务简介
  8. SQL语句资料
  9. Ubuntu 搭建etcd
  10. 20155225 2006-2007-2 《Java程序设计》第四周学习总结