zoukankan      html  css  js  c++  java
  • [思维]Finite Encyclopedia of Integer Sequences

    题目描述

    In Finite Encyclopedia of Integer Sequences (FEIS), all integer sequences of lengths between 1 and N (inclusive) consisting of integers between 1 and K (inclusive) are listed.
    Let the total number of sequences listed in FEIS be X. Among those sequences, find the (X⁄2)-th (rounded up to the nearest integer) lexicographically smallest one.

    Constraints
    1≤N,K≤3×105
    N and K are integers.

    输入

    Input is given from Standard Input in the following format:
    K N

    输出

    Print the (X⁄2)-th (rounded up to the nearest integer) lexicographically smallest sequence listed in FEIS, with spaces in between, where X is the total number of sequences listed in FEIS.

    样例输入

    3 2
    

    样例输出

    2 1 
    

    提示

    There are 12 sequences listed in FEIS: (1),(1,1),(1,2),(1,3),(2),(2,1),(2,2),(2,3),(3),(3,1),(3,2),(3,3). The (12⁄2=6)-th lexicographically smallest one among them is (2,1).

    思路:1.k为偶时:因为序列总数x=,那(X⁄2)-th (rounded up to the nearest integer) lexicographically smallest sequence一定是{k/2,k,k,k...}

    2.k为奇时:可以证明(不贴了),(X⁄2)-th (rounded up to the nearest integer) lexicographically smallest sequence是{k/2,k/2,k/2,...}的前[n/2](取下整)个序列,(即{k/2,k/2,k/2,...}再向前挪[n/2]就是答案);

    AC代码:

    #include<cstdio>
    #include<algorithm>
    using namespace std;
    
    int ans[300010];
    
    int main()
    {
       int k,n;scanf("%d%d",&k,&n);
       if(k%2==0){
          printf("%d",k/2);
          for(int i=2;i<=n;i++){
            printf(" %d",k);
          }
          printf("
    ");
       }
       else{
          int t;
          if(n%2==1) t=(n-1)/2;
          else t=(n-1)/2+1;
          for(int i=1;i<=n;i++) ans[i]=k/2+1;
          int len=n;
          while(t--){
            if(ans[len]==1) len--;
            else{
                ans[len]--;
                for(int i=len+1;i<=n;i++) ans[i]=k;
                len=n;
            }
          }
          for(int i=1;i<=len;i++) {
            if(i!=1) printf(" ");
            printf("%d",ans[i]);
          }
          printf("
    ");
       }
    }
    转载请注明出处:https://www.cnblogs.com/lllxq/
  • 相关阅读:
    sql mdf文件被破坏,ldf完好的情况下恢复数据库
    网站开发人员应该知道的61件事
    Myeclipse6.5中安装maven
    Java常用的一些正则表达式验证
    jQuery中$.ajax()方法中参数详解
    Pro Silverlight 3 in C# Layout
    Pro Silverlight 3 in C# XAML
    bugtracker.net 3.4.0 简易汉化手记+汉化文件下载
    IBatisNet In 参数配置方法
    Pro Silverlight 3 in C# XAML Resources
  • 原文地址:https://www.cnblogs.com/lllxq/p/9416349.html
Copyright © 2011-2022 走看看