zoukankan      html  css  js  c++  java
  • hust新人赛模拟20150407 C

    C - C
    Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
    Submit Status

    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.

    题意是说找到找到一组n个由1..n组成的数列,且每个数字只出现一次

    满足每相邻的两项的差的绝对值一共有k种。

    找规律即可。不好描述,直接上代码吧。

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <cmath>
     6 using namespace std;
     7      int n,k;
     8      int tmp,p;
     9      const int N=1E5+7;
    10      int a[N];
    11 
    12 int main()
    13 {
    14 
    15      scanf("%d %d",&n,&k);
    16      for ( int i = 1; i <= n ; i++ )
    17         a[i] = i;
    18         tmp = k;
    19         p = 1;
    20      for ( int i = 2 ; i <= k+1  ; i++)
    21      {
    22          a[i] = a[i-1] + tmp*p;
    23          tmp--;
    24          p=p*-1;
    25      }
    26      for ( int i = 1 ; i < n ; i++ )
    27         printf("%d ",a[i]);
    28     printf("%d",a[n]);
    29 
    30     return 0;
    31 }
  • 相关阅读:
    【LeetCode】152. 乘积最大子数组(DP)
    【剑指Offer】49. 丑数(三指针)
    [P1979][NOIP2013] 华容道 (BFS建图+SPFA)
    [P1850][NOIP2016] 换教室 (期望+DP+Floyd)
    差分约束学习笔记
    [P1291][SHOI2002] 百事世界杯之旅 (期望)
    [P4342][IOI1998] Polygon (区间DP)
    [P3802] 小魔女帕琪 (期望)
    [P1273] 有线电视网 (树形DP+分组背包)
    树链剖分学习
  • 原文地址:https://www.cnblogs.com/111qqz/p/4403431.html
Copyright © 2011-2022 走看看