这道题目最开始完全不懂,后来百度了一下,原来是字典序。而且还是组合数学里的东西。看字典序的算法看了半天才搞清楚,自己仔细想了想,确实也是那么回事儿。对于长度为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. 在有大量数据时 少用In(数据会丢失) 用left join 代替

    select @From, @To, EffectiveDate, GETDATE(), Rate from Config_Currency_ExchangeRate_Temp where Effec ...

  2. makefile文件制作入门

    一.首先,看一下最简单的C文件 //hello.c文件 #include <stdio.h> void main() { printf("hello world\n") ...

  3. 第六篇、CSS属性

    <!--1.可继承性 visible(可见的):hidden --掩藏,但是结构还保存 cursor(光标样式):pointer(手指)crosshair(十字架) 一般是文字控制属性 内联标签 ...

  4. ES6的promise的学习

    1.Promise的含义: Promise是异步编程的一种解决方案,比传统的解决方案——回调函数和事件——更合理和更强大.它由社区最早提出和实现,ES6将其写进了语言标准,统一了用法,原生提供了Pro ...

  5. Android开发系列之AChartEngine

    Android图表控件的开发 曾经开发过一个小程序,在Android电视机上面开发一个APP,用于显示一些统计图表的信息.最后找来找去基于Android Native开发有AChartEngine现成 ...

  6. winfrom 多语言切换

    1.首先将窗体的“Localizable”属性置为“True”,然后将“Language”属性置为自己想要的语言,点击重新生成项目 例如:置为“中文”,以及“英文”.当每次置为不同的语言并重新生成项目 ...

  7. LNMP下防跨站、跨目录安全设置,仅支持PHP 5.3.3以上版本

    PHP 5.3.3以上的版本,可以修改/usr/local/php/etc/php.ini在末尾里加入: [HOST=www.vpser.net] open_basedir=/home/wwwroot ...

  8. Java遍历所有网卡打印对应IP

    import java.util.Enumeration; import java.net.*; public class Test { /** * @param args */ public sta ...

  9. 【python】坑,坑,折腾一个下午python 3.5中 ImportError: No module named BeautifulSoup

    将语句 from bs4 import BeautifulSoup4 改成 from bs4 import BeautifulSoup 通过 尼玛------------------------! 总 ...

  10. python 调用第三方库压缩png或者转换成webp

    因为工作需要去研究了下png的压缩,发现转换成webp可以小很多,但是webp在手机上的解码速度比png的解码速度慢很多.出于进几年手机设备的处理器的性能也不错了,所以准备两套方案. 在网上搜索了一些 ...