题意:

给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. HDU2647Reward (拓扑排序)

    Reward Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he w ...

  2. POJ1426——Find The Multiple

    POJ1426--Find The Multiple Description Given a positive integer n, write a program to find out a non ...

  3. Elasticsearch(ES)的高级搜索(DSL搜索)(下篇)

    1. 概述 之前聊了Elasticsearch(ES)的高级搜索(DSL搜索)的一部分内容,今天把剩下的部分聊完. 2. 场景说明 2.1 创建索引同时创建映射 PUT  http://192.168 ...

  4. 设置自启动nginx(适用于其他软件)(LinuxDeploy里的Ubuntu)

    LinuxDeploy里的Ubuntu自启动nginx(适用于其他软件) 网上的教程是这样的,基本能用 1.编写脚本(这个文件及其内容安装Nginx后自动生成,没有的话内容自己Google) $ su ...

  5. dedecms织梦调用指定文章id

    {dede:arclist idlist="1349"  channelid="1" addfields="date,city"} idli ...

  6. openldap 双主模式部署

    规划两台机器 系统版本centos7.5 master1上部署ldap: 一.安装启动openldap软件 yum -y install openldap compat-openldap openld ...

  7. python学习笔记(一)-基础知识

    O.解释型语言和编译型语言 编译型语言就是先把写好的程序翻译成计算机语言然后执行,就是所谓的一次编译到处运行,比如c.c++就是编译型语言,这样的语言特点是运行速度快,但是需要事先把程序编译好才可以. ...

  8. ES增删改查

    了解了一下python对es 7.5的操作,记录下,不难: #!/usr/bin/env python # -*- coding: UTF-8 -*- from settings import Con ...

  9. Python-对Pcap文件进行处理,获取指定TCP流

    通过对TCP/IP协议的学习,本人写了一个可以实现对PCAP文件中的IPV4下的TCP流提取,以及提取指定的TCP流,鉴于为了学习,没有采用第三方包解析pcap,而是对bytes流进行解析,其核心思想 ...

  10. 【HTML】实战阿里云src页面css模仿基础学习

    实战结果页面gif图片 阿里云src首页模仿完整代码(500行左右) <!DOCTYPE html> <html> <head> <meta charset= ...