题意:

给N和M。

输出1,2,...,N的第M大全排列。

思路:

将M逆康托,求出a1,a2,...aN。

看代码。

代码:

int const MAXM=10000;

int fac[15];
int ans[1005];
int kk;
int n,m;
vector<int> pq; int main(){ int cn=0;
fac[0]=1;
while(1){
++cn;
fac[cn]=fac[cn-1]*cn;
if(fac[cn]>MAXM){
--cn;
break;
}
} while(cin>>n>>m){ pq.clear();
rep(i,1,n) pq.push_back(i); int a;
kk=0;
--m; rep2(i,n-1,0){
if(i>cn){
a=0;
ans[++kk]=pq.at(a);
pq.erase(pq.begin()+a,pq.begin()+a+1);
}else{
a=m/fac[i];
m%=fac[i];
ans[++kk]=pq.at(a);
pq.erase(pq.begin()+a,pq.begin()+a+1);
}
} printf("%d",ans[1]);
rep(i,2,kk) printf(" %d",ans[i]); cout<<endl;
} return 0;
}

hdu 1027 Ignatius and the Princess II(正、逆康托)的更多相关文章

  1. hdoj 1027 Ignatius and the Princess II 【逆康托展开】

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  2. HDU 1027 Ignatius and the Princess II(求第m个全排列)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1027 Ignatius and the Princess II Time Limit: 2000/10 ...

  3. HDU 1027 Ignatius and the Princess II(康托逆展开)

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  4. HDU - 1027 Ignatius and the Princess II 全排列

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  5. HDU 1027 Ignatius and the Princess II[DFS/全排列函数next_permutation]

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  6. HDU 1027 Ignatius and the Princess II 选择序列题解

    直接选择序列的方法解本题,可是最坏时间效率是O(n*n),故此不能达到0MS. 使用删除优化,那么就能够达到0MS了. 删除优化就是当须要删除数组中的元素为第一个元素的时候,那么就直接移动数组的头指针 ...

  7. HDU 1027 - Ignatius and the Princess II

    第 m 大的 n 个数全排列 DFS可过 #include <iostream> using namespace std; int n,m; ]; bool flag; ]; void d ...

  8. HDU 1027 Ignatius and the Princess II 排列生成

    解题报告:1-n这n个数,有n!中不同的排列,将这n!个数列按照字典序排序,输出第m个数列. 第一次TLE了,没注意到题目上的n和m的范围,n的范围是小于1000的,然后m的范围是小于10000的,很 ...

  9. hdu 1027 Ignatius and the Princess II(产生第m大的排列,next_permutation函数)

    题意:产生第m大的排列 思路:使用 next_permutation函数(头文件algorithm) #include<iostream> #include<stdio.h> ...

随机推荐

  1. 论文解读(SimCLR)《A Simple Framework for Contrastive Learning of Visual Representations》

    1 题目 <A Simple Framework for Contrastive Learning of Visual Representations> 作者: Ting Chen, Si ...

  2. 3.15学习总结(Python爬取网站数据并存入数据库)

    在官网上下载了Python和PyCharm,并在网上简单的学习了爬虫的相关知识. 结对开发的第一阶段要求: 网上爬取最新疫情数据,并存入到MySql数据库中 在可视化显示数据详细信息 项目代码: im ...

  3. 反转链表middle

    eg: 输入:head = [1,2,3,4,5], left = 2, right = 4 输出:[1,4,3,2,5]相关解法:图解: /** * Definition for singly-li ...

  4. PHP获取目录中的全部内容RecursiveDirectoryIterator

    这次我们来介绍一个SPL库中的目录迭代器,它的作用其实非常简单,从名字就可以看出来,就是获取指定目录下的所有内容.之前我们要遍历目录获取目录及目录下的所有文件一般是需要进行递归遍历的,自己写这个代码说 ...

  5. 根据类拼凑成url参数

    /// <summary>        /// 根据类拼凑成url参数        /// </summary>        /// <typeparam name ...

  6. 【PHP】随机生成名字

    public function test(){ $a = "赵 钱 孙 李 周 吴 郑 王 冯 陈 楮 卫 蒋 沈 韩 杨 朱 秦 尤 许 何 吕 施 张 孔 曹 严 华 金 魏 陶 姜 戚 ...

  7. dede调用数据时,字符串替换函数使用

    {dede:sql sql="SELECT typename,typedir,typeimg FROM #@__arctype where topid=30 limit 0,6"} ...

  8. Java基础系列(2)- Java开发环境搭建

    JDK下载与安装 安装JDK 1.百度搜素JDK8,找到下载地址 2.下载电脑对应的版本 3.双击安装JDK 4.记住安装的路径,可以自定义,默认路径如图 卸载JDK 删除Java安装目录 删除环境变 ...

  9. Linux系类(8) - 文件搜索命令locate

    文件搜索命令locate 命令格式 locate [文件名] 在后台数据库中按文件名搜索,搜索速度更快,而find.which是遍历所有目录去查找:后台数据库在/var/lib/mlocate (保存 ...

  10. mysql 优化的相关配置:总结中...

    centos 为例:mysql 怎么获取配置参数信息: /etc/my.cnf; /etc/myql/my.cnf/; 家目录:或者指定目录:作用域 客户端:全局 set global 会话 set[ ...