题意:

给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. 328 day07线程池、Lambda表达式

    day07[线程池.Lambda表达式] 主要内容 等待与唤醒案例 线程池 Lambda表达式 教学目标 -[ ] 能够理解线程通信概念 -[ ] 能够理解等待唤醒机制 -[ ] 能够描述Java中线 ...

  2. CodeForce-801C Voltage Keepsake(二分)

    题目大意:有n个装备,每个设备耗能为每单位时间耗能ai,初始能量为bi;你有一个充电宝,每单位时间可以冲p能量,你可以在任意时间任意拔冲. 如果可以所有设备都可以一直工作下去,输出-1:否则,输出所有 ...

  3. shell脚本中 /dev/null 的用途

    /dev/null 是一个特殊的设备文件,它丢弃一切写入其中的数据 可以将它 视为一个黑洞, 它等效于只写文件, 写入其中的所有内容都会消失, 尝试从中读取或输出不会有任何结果,同样,/dev/nul ...

  4. 腾讯云与 Grafana Labs 达成深度合作, 推出全新 Grafana 托管服务

    9 月 23 日,腾讯云宣布与业界领先的开源数据可视化公司 Grafana Labs 达成深度合作协议,共同开发和验证全新的 Grafana 托管服务,通过 Grafana Labs 开源软件与腾讯云 ...

  5. 关于python如何构造测试数据

    参考资料:https://www.cnblogs.com/miaoxiaochao/p/13234589.html 一.Faker模块是什么? 一个Python第三方模块,主要用来创建伪数据 无需再手 ...

  6. Centos7 搭建sonarQube

       可以支持包括java,C#,C/C++,PL/SQL,Cobol,JavaScrip,Groovy等等二十几种编程语言的代码质量管理与检测: SonarQube官网:https://www.so ...

  7. P3643-[APIO2016]划艇【dp】

    正题 题目链接:https://www.luogu.com.cn/problem/P3643 题目大意 求有多少个\(n\)个数的序列\(x\)满足,\(x_i\in \{0\}\cup[a_i,b_ ...

  8. Ybt#452-序列合并【期望dp】

    正题 题目链接:https://www.ybtoj.com.cn/contest/113/problem/2 题目大意 一个空序列,每次往末尾加入一个\([1,m]\)中的随机一个数.如果末尾两个数相 ...

  9. P3793-由乃救爷爷【分块,ST表】

    正题 题目链接:https://www.luogu.com.cn/problem/P3793 题目大意 给出\(n\)个数字的一个序列\(m\)次询问区间最大值 保证数据随机 \(1\leq n,m\ ...

  10. Markdown 编写技巧汇总(一)

    编写文档,有很多格式选择,也有不同平台选择.下面就自己接触到的MarkDown编写文档的各种技巧做简单梳理,供自己参阅,也希望帮到网友. [1]添加空格 ①   这种写法比较老土,但是,很实用!注意都 ...