6617: Finite Encyclopedia of Integer Sequences

时间限制: 1 Sec  内存限制: 128 MB
提交: 239  解决: 42
[提交] [状态] [讨论版] [命题人:admin]

题目描述

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

【小结】

本来用dfs直接模拟树,几乎20以内的数我都和暴力程序对比了,没有错,可为什么就是一直WA+WA+WA?赛后跟彬子哥的代码对拍,拍了十几秒吧,我都要说这个他妈对拍都拍不出来。啪,一个错误数据蹦出来了,3 88。这是一组杀人的数据,我的输出和答案正好差1。又多拍了几组,都是差1~   无语,几乎90%的数据都对。   或许罪魁祸首是那个n/2向上取整,某一个瞬间,我的代码没取上去吧。。。。。自闭了。

【分析】

对于k是偶数时,很容易发现,答案为  k/2 k k k .... k,共n个数。

k是奇数时,应该很靠近中间,即(k+1)/2  (k+1)/2  (k+1)/2.....而多写几组会发现,真是答案会和这个序列在题意要求下,正好相差n/2个字典序。让这个序列按照树的先序遍历往前退n/2步即为正确答案。

【代码】

/****
***author: winter2121
****/
#include<bits/stdc++.h>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define SI(i) scanf("%d",&i)
#define PI(i) printf("%d\n",i)
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int MAX=3e5+5;
const int INF=0x3f3f3f3f;
const double eps=1e-8;
int dir[9][2]={0,1,0,-1,1,0,-1,0, -1,-1,-1,1,1,-1,1,1};
template<class T>bool gmax(T &a,T b){return a<b?a=b,1:0;}
template<class T>bool gmin(T &a,T b){return a>b?a=b,1:0;}
template<class T>void gmod(T &a,T b){a=(a%mod+b)%mod;}ll gcd(ll a,ll b){ while(b) b^=a^=b^=a%=b; return a;}
ll inv(ll b){return b==1?1:(mod-mod/b)*inv(mod%b)%mod;}
ll qpow(ll n,ll m)
{n%=mod;ll ans=1;while(m){if(m%2)ans=(ans*n)%mod;m>>=1; n=(n*n)%mod;}return ans;
}int a[MAX];
int main()
{int n,k,rem,p;while(cin>>k>>n){p=n;if(k%2==0){rep(i,2,n)a[i]=k;a[1]=k/2;}else{rep(i,1,n)a[i]=(k+1)/2;rem=n/2,p=n;while(rem--) //按字典序往后退{if(a[p]==1)a[p--]--; //向上退else{a[p]--;while(p<n)a[++p]=k; //回退到了上一课子树,并直接到底}}}rep(i,1,p)printf("%d%c",a[i],i<p?' ':'\n');}return 0;
}

upc 6617: Finite Encyclopedia of Integer Sequences(树的先序遍历第n/2个结点)相关推荐

  1. UPC 6617 Finite Encyclopedia of Integer Sequences(找规律)

    题目描述 In Finite Encyclopedia of Integer Sequences (FEIS), all integer sequences of lengths between 1 ...

  2. 6617: Finite Encyclopedia of Integer Sequences

    6617: Finite Encyclopedia of Integer Sequences 时间限制: 1 Sec  内存限制: 128 MB 提交: 341  解决: 75 [提交] [状态] [ ...

  3. UPC6617: Finite Encyclopedia of Integer Sequences

    6617: Finite Encyclopedia of Integer Sequences 时间限制: 1 Sec  内存限制: 128 MB 提交: 239  解决: 42 [提交] [状态] [ ...

  4. [ARC084]E - Finite Encyclopedia of Integer Sequences 乱搞

    题面 首先若k是偶数,答案显然是k/2,k,k,k-- 若k是奇数,我们考虑这样一个序列B:k/2,k/2,k/2--的排名. 构造一个映射f(X)->X',X是一个序列,X'是每个Xi替换成k ...

  5. 程序员面试题精选100题(06)-二元查找树的后序遍历结果[数据结构]

    题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果.如果是返回true,否则返回false. 例如输入5.7.6.9.11.10.8,由于这一整数序列是如下树的后序遍历结果: 8   ...

  6. 6.二元查找树的后序遍历结果[PostOrderOfBST]

    [题目] 输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果.如果是返回true,否则返回false. 例如输入5.7.6.9.11.10.8,由于这一整数序列是如下树的后序遍历结果: 8 ...

  7. 树的先序遍历的栈实现

    树的先序遍历的栈实现 先把根节点访问了,右子树入栈,去访问左子树. 1 void preorder(tree bt) //先序遍历bt所指的二叉树 2 { 3 tree stack[n]; //栈 4 ...

  8. 判断整数序列是不是二元查找树的后序遍历结果

    题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果. 如果是返回true,否则返回false. 例如输入5.7.6.9.11.10.8,由于这一整数序列是如下树的后序遍历结果: 8 ...

  9. 二元查找树的后序遍历结果

    题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果.如果是返回 true ,否则返回 false . 例如输入5.7.6.9.11.10.8,由于这一整数序列是如下树的后序遍历结果: ...

最新文章

  1. MySIAM与Innodb引擎
  2. 直击!10万阿里小二的复工生活
  3. Juniper ex4200 端口镜像问题
  4. Mac本如何运营php框架,1、Mac系统下搭建thinkPHP框架环境
  5. JDBC实现学生信息管理系统(仅增删改查)
  6. Linux Watchdog Test Program
  7. ThingJS图表整合
  8. 销售服务器账务处理,​销售货物和提供技术服务怎么做账
  9. html使用对话框接收密码,Excel黑科技 vba中用Inputbox对话框接受输入密码时显示为*...
  10. 人工智能区块链智能合约_通过业务规则使您的区块链智能合约更智能
  11. php 获取月初月末时间,php一个获取下月月初和月末的函数
  12. FFA 2021 专场解读 - 平台建设
  13. js 实现筋斗云效果(点击tab栏里面的某个地方,会有图片移动到此地方)
  14. it人成功的六大步骤
  15. 条件概率的本质是样本空间的缩减
  16. .hpp文件和.h文件的区别
  17. 【爬虫小白】各种请求使用代理的方法
  18. matlab 路由表,闭关修炼之zigbee路由
  19. 干货 | 免费GIS数据网站推荐
  20. [JS权威指南笔记] 第1章-第3章

热门文章

  1. 为Halide安装opencl支持
  2. 全栈修炼:如何从Web前端迈向全栈开发
  3. java52-抽象类
  4. Matlab之绘制三维曲面图
  5. C语言一维/二维数组解引用难理解点以及一道难题
  6. Java实现split字符串分割方法
  7. os.listdir(path)
  8. 2017算法实习生应聘经验总结
  9. python:shape和reshape()函数
  10. Access数据库的加密与解密