【HDOJ】1027 Ignatius and the Princess II
这道题目最开始完全不懂,后来百度了一下,原来是字典序。而且还是组合数学里的东西。看字典序的算法看了半天才搞清楚,自己仔细想了想,确实也是那么回事儿。对于长度为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的更多相关文章
- 【HDOJ】1026 Ignatius and the Princess I
这道题搞了很久啊.搜索非常好的一道题.昨天想了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 ...
- hdoj 1027 Ignatius and the Princess II 【逆康托展开】
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
- HDU 1027 Ignatius and the Princess II(康托逆展开)
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
- HDU - 1027 Ignatius and the Princess II 全排列
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
- 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 ( ...
- poj 1027 Ignatius and the Princess II全排列
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
- 【HDOJ】1098 Ignatius's puzzle
数学归纳法,得证只需求得使18+ka被64整除的a.且a不超过65. #include <stdio.h> int main() { int i, j, k; while (scanf(& ...
- 【母函数】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; ...
随机推荐
- Java的基础概念
JDK (Java Development Kit) Java Developer Kit contains tools needed to develop the Java programs, an ...
- Java架构必会几大技术点(转)
关于学习架构,必须会的几点技术: 1. java反射技术 2. xml文件处理 3. properties属性文件处理 4. 线程安全机制 5. annocation注解 6. 设计模式 7. 代理机 ...
- CruiseControl.NET : svnrevisionlabeller
How to use svnrevisionlabeller as your labeller type in CC.NET: 1. Download ccnet.SvnRevisionLabelle ...
- 程序员面试题精选100题(38)-输出1到最大的N位数[算法]
作者:何海涛 出处:http://zhedahht.blog.163.com/ 题目:输入数字n,按顺序输出从1最大的n位10进制数.比如输入3,则输出1.2.3一直到最大的3位数即999. 分析:这 ...
- Yii 关于 find findAll 查找出制定的字段的方法
总所周知 modelName::model() -> find() //找出的是一个对象 modelName::model() -> findALL() //找出的是一个对象集合的数组 如 ...
- 阿里云ECS服务器被DDoS无解,请问我该何去何从?
阿里云ECS服务器被DDoS无解,请问我该何去何从?
- Linux 网络I/O模型
前言 本文是笔者的第一篇博文,在这篇文章的大部分内容基于steven大神的<Unix Network Programming>.一来是对书本内容的整理与归纳.二来也是为接下来的博文奠定基础 ...
- tomcat 192.168.1.110?不烦吗?
最近做一个在线播放器,因为要用到网络服务器做在线播放,又不想直接在本地用tomcat做实验,因为没有真实感. so,手边两台电脑,同时连在局域网. 客户端,笔记本,ip1:192.168.1.101 ...
- 使用vagrant作为开发环境后,js报错
当你尝试修改一个js,并且用同样的方法更新之后,会遇到类似的问题,是的,就算重启VM上任何服务,甚至重启VM,依旧没有用,当然,比起其他资源文件,浏览器的反应会强烈一些,因为浏览器会提示未知错误,而你 ...
- VC窗口最大化方法
一.主框架窗口最大化 一般方法 1.修改App::InitInstance中的 m_pMainWnd->ShowWindow(m_nCmdShow);m_pMainWnd->UpdateW ...