http://codeforces.com/contest/483/problem/C

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.

解题思路:形如1,n,2,n-1,3,n-2.....这样肯定是最优的,但题目要求你正好有k个不同的,那么可以构造出k-1个不同的之后根据k是否为偶数进行单调输出

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 
 5 int main(){
 6     int i, k, n, left, right;
 7     while(scanf("%d %d", &n, &k) != EOF){
 8         left = ; right = n;
 9         for(i =  ; i <= k; i++){
             if(i&){
                 printf("%d ", left++);
             }
             else{
                 printf("%d ", right--);
             }
         }
         if(k&){
             while(left < right){
                 printf("%d ", left++);
             }
             printf("%d\n", left);
         }
         else{
             while(left < right){
                 printf("%d ", right--);
             }
             printf("%d\n", right);
         }
     }
     return ;

31 }

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

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

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

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

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

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

  4. Codeforces Round #486 (Div. 3) A. Diverse Team

    Codeforces Round #486 (Div. 3) A. Diverse Team 题目连接: http://codeforces.com/contest/988/problem/A Des ...

  5. Codeforces Round #275 (Div. 2)

    A. Counterexample 题意:给出l,r,找出使得满足l<a<b<c<r,同时满足a,b的最大公约数为1,b,c的最大公约数为1,且a,b的最大公约数不为1 因为题 ...

  6. Codeforces Round #275 (Div. 2) A,B,C,D

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

  7. [Codeforces Round #275 (Div. 2)]B - Friends and Presents

    最近一直在做 codeforces ,总觉得已经刷不动 BZOJ 了? ——真是弱喵 你看连 Div.2 的 B 题都要谢谢题解,不是闲就是傻 显然我没那么闲 ╮(╯_╰)╭ 我觉得这题的想法挺妙的~ ...

  8. (原创)Codeforces Round #550 (Div. 3) A Diverse Strings

    A. Diverse Strings time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  9. Codeforces Round #275 (Div. 2) C

    题目传送门:http://codeforces.com/contest/483/problem/C 题意分析:题目意思没啥好说的. 去搞排列列举必须TLE.那么就想到构造. 1.n.2.n-1.3.n ...

随机推荐

  1. https协议(4)

    架构层次 HTTPS(全称:Hypertext Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版.即HTT ...

  2. 算法学习--Day9

    继上一次完成最小生成树后,这次我开始准备最短路径的程序. 最短路分为两种算法,第一个为Floyd算法,第二个为Dijkstra. 简单来说,Floyd是以点为参照对象,它使用三层循环求解当前图中所有点 ...

  3. CodeForces717C 【数学】

    题意: 给你n个数既表示a类的值也表示b类的值,然后计算a和b类两两搭配相乘相加,使得答案最小: 思路: 显而易见的方案是最小乘最大,次小乘次大,然后依次下去.. 可以那个特例证明这个是对的 #inc ...

  4. idea | 命名空间改过后重新导入项目方法

    file菜单-->Open-->找到项目中pom文件-->会提示as project 和as file-->选择as project,会自动生成idea所有相关的文件

  5. VLAN-5-802.1Q-in-Q隧道

    Q-in-Q允许SP在跨越WAN服务时,保留802.1Q VLAN标签.由此,VLAN可以被拓展到多个地理分散的站点上.     入向SP交换机收到802.1Q数据帧,使用额外的802.1Q头部来标记 ...

  6. [題解]hdu_6412公共子序列

    https://blog.csdn.net/nka_kun/article/details/81902421 #include<bits/stdc++.h> #define ll long ...

  7. 自己写的Grid组件,第二版

    大体没什么变化,主要是添加了一个方法,getSelectedItems(),返回当前选中的数据项. (function ($) { $.fn.GridView = function (setting) ...

  8. 接口测试02 - 无法绕过的json解析

    概述: 先瞧一下什么是json.JSON(JavaScript Object Notation,JS对象标记)是一种轻量级的数据交换格式. 它基于ECMAScript(w3c定制的js规范)的一个子集 ...

  9. 530 Minimum Absolute Difference in BST 二叉搜索树的最小绝对差

    给定一个所有节点为非负值的二叉搜索树,求树中任意两节点的差的绝对值的最小值.示例 :输入:   1    \     3    /   2输出:1解释:最小绝对差为1,其中 2 和 1 的差的绝对值为 ...

  10. Linux 批量杀进程的命令

    使用awk批量杀进程的命令: ps -ef | grep firefox | grep -v grep | awk '{print "kill -9 "$2}'|sh #列出了当前 ...