zoukankan      html  css  js  c++  java
  • CodeForces 483C Diverse Permutation

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

    Description

    Permutationp is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive integers not larger than n. We'll denote asn 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 nk (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
    Output
    1 3 2
    Input
    3 1
    Output
    1 2 3
    Input
    5 2
    Output
    1 3 2 4 5

    Hint

    By |x| we denote the absolute value of number x.

     1 #include <stdio.h>
     2 #include <string.h>
     3 int main()
     4 {
     5     int n,k;
     6     int i,j;
     7     int a[100005];
     8     while(scanf("%d %d",&n,&k)!=EOF)
     9     {
    10         memset(a,0,sizeof(a));
    11         int j=k+2,num=1;
    12         a[1]=1;
    13         printf("1");
    14         while(k)
    15         {
    16             if(num-k>0 && a[num-k]==0)
    17             {
    18                 printf(" %d",num-k);
    19                 num=num-k;
    20                 a[num]=1;
    21             }
    22             else
    23             {
    24                 printf(" %d",num+k);
    25                 num=num+k;
    26                 a[num]=1;
    27             }
    28             k--;
    29         }
    30         for(i=j;i<=n;i++)
    31         {
    32             printf(" %d",i);
    33         }
    34         printf("
    ");
    35     }    
    36     return 0;
    37 }
    View Code
  • 相关阅读:
    用UILocalNotification实现一个闹钟(Swift)
    Swift
    iOS判断一些权限是否被禁止
    ofbiz学习笔记
    POJ1062 昂贵的聘礼 【DFS】
    echarts 应用数个样例
    java 中缀转后缀(逆波兰)
    开放是否能让苹果成为智能家居的标准制定者?
    2015阿里校招前端在线题目
    hql中不能写count(1)能够写count(a.id)
  • 原文地址:https://www.cnblogs.com/cyd308/p/4771536.html
Copyright © 2011-2022 走看看