【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; ...
随机推荐
- jQuery 源码分析 7: sizzle
jQuery使用的是sizzle这个选择器引擎,这个引擎以其高速著称,其实现十分精妙但是也足够复杂,下面现简单分析一下相关的代码. 在jQuery的部分API接口是直接引用了Sizzle的方法,这些接 ...
- 第33条:用EnumMap代替序数索引
有时候,会见到利用ordinal方法来索引数组的代码.例如下面这个简化的类,表示一种烹饪用的香草: public class Herb { public enum Type { ANNUAL, PER ...
- iOS 分类思想(2)
/******************* NSString+NumCount.h文件 ******************************/ #import <Foundation/Fo ...
- 实时监听input输入框value值的变化
1.js 的 oninput & onpropertychange JS中的 oninput 事件在 IE9 以下版本不支持,需要使用 IE 特有的 onpropertychange 事件替代 ...
- AOP(以MVC中的过滤器为例)
MVC里面的Filter public class AOPFilterAttribute : ActionFilterAttribute, IExceptionFilter { public void ...
- NFS挂载及写入故障
最近在做架构时,分离出来一台图片服务器,图片服务器是通过NFS(网络文件系统)给两台web服务器提供图片存储的,在编辑NFS配置文件(/etc/exports)时:想了一下,允许访问NFS共享目录的范 ...
- [python]使用ElementTree解析XML【译】
19.7 The ElementTree XML API 源码:Lib/xml/etree/ElementTree.py Element类型是一个灵活的容器对象,设计出来是用于存储有层次的数据结构到内 ...
- 常用Oracle SQL语句(汇总版)
Oracle数据库常用sql语句 ORACLE 常用的SQL语法和数据对象 一.数据控制语句 (DML) 部分 1.INSERT (往数据表里插入记录的语句) INSERT INTO 表名(字段名1, ...
- C# - 定义集合,索引符
Animal 类 Cow类 Chicken类 Animal 集合类 调用
- hdu 1828 Picture(线段树 || 普通hash标记)
http://acm.hdu.edu.cn/showproblem.php?pid=1828 Picture Time Limit: 6000/2000 MS (Java/Others) Mem ...