C. Diverse Permutation
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 test(s)
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个 但详细哪k个数不限

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#define N 100009
using namespace std;
int a[N];
int vis[N]; int main()
{
int n,k;
while(~scanf("%d%d",&n,&k))
{
int t=n;
int m=1; for(int i=1;i<=k;i+=2)
{
a[i]=m;
m++;
}
for(int i=2;i<=k;i+=2)
{
a[i]=t;
t--;
} if(k%2==0)
{
for(int i=k+1;i<=n;i++)
a[i]=a[i-1]-1;
}
else
for(int i=k+1;i<=n;i++)
a[i]=a[i-1]+1; for(int i=1;i<n;i++)
cout<<a[i]<<" ";
cout<<a[n]<<endl;
}
return 0;
}

codeforces C. Diverse Permutation的更多相关文章

  1. CodeForces 483C Diverse Permutation

    Diverse Permutation Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64 ...

  2. codeforces 483C.Diverse Permutation 解题报告

    题目链接:http://codeforces.com/problemset/problem/483/C 题目意思:给出 n 和 k,要求输出一个含有 n 个数的排列 p1, p2, ...,pn,使得 ...

  3. [Codeforces 482A] Diverse Permutation

    [题目链接] https://codeforces.com/contest/482/problem/A [算法] 首先构造一个(k + 1)个数的序列 , 满足它们的差为1-k 对于i > k ...

  4. codeforces C. Diverse Permutation(构造)

    题意:1...n 的全排列中 p1, p2, p3....pn中,找到至少有k个 |p1-p2| , |p2-p3|, ...|pn-1 - pn| 互不相同的元素! 思路: 保证相邻的两个数的差值的 ...

  5. Codeforces 482 - Diverse Permutation 构造题

    这是一道蛮基础的构造题. - k         +(k - 1)      -(k - 2) 1 + k ,    1 ,         k ,             2,    ....... ...

  6. Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)

    题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation 题意:一串排列1~n.求一个序列当中相邻两项差的绝对值的个数(指绝对值不同的个数 ...

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

    Codeforces Round #275 (Div. 1)A. Diverse Permutation Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 ht ...

  8. Codeforces Round #275(Div. 2)-C. Diverse Permutation

    http://codeforces.com/contest/483/problem/C C. Diverse Permutation time limit per test 1 second memo ...

  9. 构造 Codeforces Round #275 (Div. 2) C. Diverse Permutation

    题目传送门 /* 构造:首先先选好k个不同的值,从1到k,按要求把数字放好,其余的随便放.因为是绝对差值,从n开始一下一上, 这样保证不会超出边界并且以防其余的数相邻绝对值差>k */ /*** ...

随机推荐

  1. ActiveMQ 认证(一)

    新搭建的ActiveMQ服务,在发布和读取消息时,连接的权限为ActiveMQConnection.DEFAULT_USER和ActiveMQConnection.DEFAULT_PASSWORD. ...

  2. jvm 哪些是不会被gc回收的

    韩梦飞沙 yue31313 韩亚飞 han_meng_fei_sha 313134555@qq.com

  3. jquery checkbox用法汇总

    来源:http://www.jb51.net/article/75717.htm 1.全选 ? 1 2 3 $("#btn1").click(function(){ $(" ...

  4. [转]XML中元素(Element)与节点(Node)的区别

    前言: element是特殊的node 一段纯文本即text-node也是node 但不是element w3c的原话是 A node can be an element node, an attri ...

  5. Android——Activity and Service

    参考博客:http://www.cnblogs.com/newcj/archive/2011/05/30/2061370.html   http://www.cnblogs.com/feisky/ar ...

  6. Elasticsearch 5.5.1的安装和入门教程(转)

    说明:转自老阮的文章,业界最简单的入门教程.一切的安装的运行建议不要用root权限,最好是当前用户下的权限. 作者: 阮一峰 日期: 2017年8月17日 全文搜索属于最常见的需求,开源的 Elast ...

  7. 【Tomcat MyEcplise】MyEcplise添加一个新的Server,Tomcat8.5报错

    添加新的Tomcat添加不进去,报错: The Apache Tomcat installation at this directory is version 8.5.11.  A Tomcat 8. ...

  8. React个人学习笔记

    元素渲染 通过 ReactDOM.render() 方法渲染页面, 可以使用 ES6 class 来定义一个组件: 如何解析HTMl里面的空格: 1. 使用空格的 unicod 编码 : \u0020 ...

  9. 流畅的python第二章序列构成的数组学习记录

    python内置序列类型概览 列表推导和生成器表达式 列表推导是构建列表的快捷方式,而生成器表达式可以用来创建其他任何类型的序列 列表推导的示例 >>>test = [i*2 for ...

  10. hdu 1408 acdearm &quot;Money, Money, Money&quot;

    #include<stdio.h> int main() { long long int x; while(~scanf("%lld",&x)) { if(x% ...