题目描述
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).
题意:
对于k是偶数时,答案为 k/2 k k k …. k,共n个数。
k是奇数时,应该很靠近中间,即(k+1)/2 (k+1)/2 (k+1)/2…..正好相差n/2个字典序。让这个序列按照树的先序遍历往前退n/2步即为正确答案。
#include<bits/stdc++.h>
//int k,a[100010];
using namespace std;
const int INF=0x3f3f3f3f;
const int N=3e5+10;
int a[N];
int Search(int *a,int key)
{ //在顺序表中折半查找key的数据元素。若找到,则函数值为
int low=0,mid; //该元素的数组下标;否则为0。
int high=14;
while(low <=high)
{
mid=(low+high)/2;
if(key==a[mid]) return mid; //找到待查元素
else if(key <a[mid]) high=mid-1; //继续在前半区间进行查找
else low=mid+1; //继续在后半区间进行查找
}}
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);
}
else {
for(int i=1;i<=n;i++)
a[i] = (k+1)/2;
int t = n;
for(int i=1;i<=n/2;i++)
if(a[t]==1){
t--;
}
else{
a[t]--;
while(t<n)a[++t]=k;
}
for(int i = 1; i <=t; i++)
printf("%d ",a[i]);
}
return 0;
}