这道题目最开始完全不懂,后来百度了一下,原来是字典序。而且还是组合数学里的东西。看字典序的算法看了半天才搞清楚,自己仔细想了想,确实也是那么回事儿。对于长度为n的数组a,算法如下:
(1)从右向左扫描,找到满足a[i]<a[i+1]的第一个i,也就是i = max{i|a[i]<a[i+1]},同时也意味着a[i+1]~a[n]是升序;
(2)从右向左扫描,找到满足a[j]>a[i]的第一个j,也就是j = max{j|a[j]>a[i]},a[j]也是满足大于a[i]的最小数;
(3)交换a[i]与a[j];
(4)将a[j+1]与a[n]间的数字逆转。
直接实现算法:

 #include <stdio.h>

 #define MAXNUM 1005

 int array[MAXNUM];

 void chgonce(int n) {
int i, j, k, tmp; i = n-;
while (i && array[i]>array[i+])
i--; k = ;
j = n;
while (j && array[j]<array[i])
j--; if (array[j] > array[i])
k = ; if (k) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp; // reverse
for (k=i+; k*<=n+i+; ++k) {
tmp = array[k];
array[k] = array[n+i+-k];
array[n+i+-k] = tmp;
}
}
} int main() {
int n, m;
int i, k; while (scanf("%d %d", &n, &m) != EOF) {
for (i=; i<=n; ++i) {
array[i] = i;
}
k = ;
while (k<m) {
chgonce(n);
++k;
}
for (i=; i<=n; ++i) {
if (i==)
printf("%d", array[i]);
else
printf(" %d", array[i]);
}
printf("\n");
} return ;
}

STL的next_permutation也可以直接实现:

 #include <iostream>
#include <algorithm>
using namespace std; #define MAXNUM 1005 int array[MAXNUM]; int main() {
int n, m, k; while (cin>>n>>m) {
for (int i=; i<n; ++i)
array[i] = i+;
k = ;
while (k++<m)
next_permutation(array, array+n);
cout <<array[];
for (int i=; i<n; ++i)
cout <<" "<<array[i];
cout <<endl;
}
return ;
}

【HDOJ】1027 Ignatius and the Princess II的更多相关文章

  1. 【HDOJ】1026 Ignatius and the Princess I

    这道题搞了很久啊.搜索非常好的一道题.昨天想了2小时,以为是深搜,但后来发现深搜怎么也没法输出正确路径.今天拿宽搜试了一下,问题就是普通的队列宽搜没法得到当前时间最小值.看了一下讨论区,发现优先级队列 ...

  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. hdoj 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 全排列

    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[DFS/全排列函数next_permutation]

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

  7. poj 1027 Ignatius and the Princess II全排列

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

  8. 【HDOJ】1098 Ignatius's puzzle

    数学归纳法,得证只需求得使18+ka被64整除的a.且a不超过65. #include <stdio.h> int main() { int i, j, k; while (scanf(& ...

  9. 【母函数】hdu1028 Ignatius and the Princess III

    大意是给你1个整数n,问你能拆成多少种正整数组合.比如4有5种: 4 = 4;  4 = 3 + 1;  4 = 2 + 2;  4 = 2 + 1 + 1;  4 = 1 + 1 + 1 + 1; ...

随机推荐

  1. asp:手机扫描二维码跳转手机版

    如果想手机扫描用pc版网站生成的二维码跳转到对应的手机版的话,请在pc端的首页的<head></head>标签里面加入下面内容:   <script src=" ...

  2. C# ACM poj1002

    排序 public static void acm1002(string[] azx) { string[] a = new string[azx.Length]; ; i < azx.Leng ...

  3. props验证

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. javascript学习笔记20160121-css选择器

    元素可以用id.标签名或类来描述: 更一般的,元素可以基于属性来选取: 这些基本的选择器可以组合使用: 选择器可以指定文档结构(重要,之前一直不太明白>的使用): 选择器可以组合起来选取多个或多 ...

  5. 谱曲软件-MuseScore

    谱曲软件-MuseScore 参考: 1.MuseScore官网 2.免费乐谱制作软体MuseScore

  6. Windows 键盘操作快捷方式积累

    复制.粘贴: CTRL+C 复制被选择的项目到剪贴板 CTRL+V 粘贴剪贴板中的内容到当前位置 CTRL+X 剪切被选择的项目到剪贴板 Alt+ space + E + P CMD 窗口快速粘贴 关 ...

  7. Java 使用反射拷贝对象一般字段值

    在<Java解惑>上面看到第八十三例--诵读困难者,要求使用非反射实现单例对象的拷贝.查阅了部分资料,先实现通过反射拷贝对象. 1. 编写需要被拷贝的对象Person package co ...

  8. OpenCV2学习笔记03:Qt中配置OpenCV环境

    在Qt中开发基于OpenCV的应用时,需要配置对应函数库到环境变量,这时候我们需要使用到qmake能够识别的变量来指定环境变量. INCLUDEPATH: 用于指定搜索头文件到文件夹路径. LIBS: ...

  9. jQuery Mobile里xxx怎么用呀?(控件篇)

    jQuery Mobile里都有什么控件? http://api.jquerymobile.com/category/widgets/ jQuery Mobile里slider控件的change事件怎 ...

  10. C# Linq简介

    LInq是Language Integrated Query的简称,它是微软在.net framework 3.5里面新加入的特性,用以简化查询查询操作.它主要包含了3块,Linq to Object ...