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 nthe 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 nk (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个数1~n,每一个数仅仅能用一次。组成差值的绝对值有k个数。为1~k。

输出任一个方案。

构造题,我是这样构造的,取前k+1个数。第一个数取1,先+k。后一个数-(k-1),在后一个数+k-2.......这样从两头往

中间靠拢。既取完了k+1个数。又构造了1~k的差值绝对值,至于k+1后的嘛,每次+1即可了。

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn=100000+1000;
int ans[maxn];
int main()
{
int n,k;
ans[1]=1;
scanf("%d%d",&n,&k);
if(k==1)
{
for(int i=1;i<=n;i++)
ans[i]=i;
}
else
{
for(int i=2;i<=k+1;i++)
{
if(i%2)
ans[i]=ans[i-1]-(k-i+2);
else
ans[i]=ans[i-1]+(k-i+2);
}
int cur=1;
for(int i=k+2;i<=n;i++)
{
ans[i]=k+1+cur;
cur++;
}
}
for(int i=1;i<n;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[n]);
return 0;
}

C. Diverse Permutation(Codeforces Round #275(div2)的更多相关文章

  1. B. Friends and Presents(Codeforces Round #275(div2)

    B. Friends and Presents time limit per test 1 second memory limit per test 256 megabytes input stand ...

  2. A. Counterexample (Codeforces Round #275(div2)

    A. Counterexample time limit per test 1 second memory limit per test 256 megabytes input standard in ...

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

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

  4. 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 ...

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

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

  6. Codeforces Round #539 div2

    Codeforces Round #539 div2 abstract I 离散化三连 sort(pos.begin(), pos.end()); pos.erase(unique(pos.begin ...

  7. 【前行】◇第3站◇ Codeforces Round #512 Div2

    [第3站]Codeforces Round #512 Div2 第三题莫名卡半天……一堆细节没处理,改一个发现还有一个……然后就炸了,罚了一啪啦时间 Rating又掉了……但是没什么,比上一次好多了: ...

  8. Codeforces Round#320 Div2 解题报告

    Codeforces Round#320 Div2 先做个标题党,骗骗访问量,结束后再来写咯. codeforces 579A Raising Bacteria codeforces 579B Fin ...

  9. Codeforces Round #564(div2)

    Codeforces Round #564(div2) 本来以为是送分场,结果成了送命场. 菜是原罪 A SB题,上来读不懂题就交WA了一发,代码就不粘了 B 简单构造 很明显,\(n*n\)的矩阵可 ...

随机推荐

  1. Django——Model

    一. ORM 在 MVC 或者说 MTV 设计模式中,模型(M)代表对数据库的操作.那么如何操作数据库呢? 我们可以在 Python 代码中嵌入 SQL 语句. 但是问题又来了,Python 怎么连接 ...

  2. NOIp模拟赛三十四(yxq供题)

    毒瘤yxq! 毒瘤yxq! 毒瘤yxq! 据yxq自己说,林导让他出题的时候要求是“代码量少”,“思维难度高”,“不涉及太复杂的算法”,而且“最好要让myh有一题做不出来”(狙击myh).于是今天的题 ...

  3. vue mint-ui swipe 不显示或显示空白

    vue mint-ui swipe 不显示或显示空白? 解决需要在mt-swipe上层元素设置高度 <div> <div> <mt-header title=" ...

  4. HDU-1024 Max Sum Plus Plus 动态规划 滚动数组和转移优化

    题目链接:https://cn.vjudge.net/problem/HDU-1024 题意 给n, m和一个序列,找m个不重叠子串,使这几个子串内元素和的和最大. n<=1e6 例:1 3 1 ...

  5. [LeetCode] 455. 分发饼干 assign-cookies(贪心算法)

    思路: 尽量先将小饼干分配给胃口小的孩子,故而饼干和孩子胃口都应该先排序. python中,a.sort()只能用于a为list, sort()是可变对象的方法,无参数,无返回值,但会影响改变对象. ...

  6. GenIcam标准(一)

    1.概述 如今的数码摄相机包含了很多的功能,而不仅仅是采集图像.对于机器视觉相机来说,处理图像并把结果附加到图像数据流上,控制附加的硬件,代替应用程序作实时的处理等都是很平常的事情.这也导致了相机的编 ...

  7. cogs 141. [USACO Jan08] 奶牛的选举

    141. [USACO Jan08] 奶牛的选举 ★   输入文件:elect.in   输出文件:elect.out   简单对比时间限制:1 s   内存限制:16 MB 在推翻了Farmer J ...

  8. CI框架源代码阅读笔记2 一切的入口 index.php

    上一节(CI框架源代码阅读笔记1 - 环境准备.基本术语和框架流程)中,我们提到了CI框架的基本流程.这里再次贴出流程图.以备參考: 作为CI框架的入口文件.源代码阅读,自然由此開始. 在源代码阅读的 ...

  9. Linux多线程实践(六)使用Posix条件变量解决生产者消费者问题

    前面的一片文章我们已经讲过使用信号量解决生产者消费者问题.那么什么情况下我们须要引入条件变量呢? 这里借用  http://www.cnblogs.com/ngnetboy/p/3521547.htm ...

  10. nyoj--108--士兵杀敌(一)(区间求和&&树状数组)

    士兵杀敌(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 南将军手下有N个士兵,分别编号1到N,这些士兵的杀敌数都是已知的. 小工是南将军手下的军师,南将军现在想知 ...