【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; ...
随机推荐
- iOS-Reachability的使用
// 下载地址:http://pan.baidu.com/s/1gd5NNDD // 1. 添加Reachability.h和Reachibility.m文件到项目中 // 2. 添加System ...
- 12天学好C语言——记录我的C语言学习之路(Day 3)
12天学好C语言--记录我的C语言学习之路 Day 3: 不知不觉到了第三天的学习,我们前两天学习的东西很杂乱,各个方面都有学习.我觉得这不是不系统,也不是学的不扎实,这种学习对于初学者而言我认为是很 ...
- Java实战之02Hibernate-04多表映射
十.多表映射 0.内容补充:数据完整性 作用:防止用户的误操作. 实体完整性:主键.用于确定表中唯一的一条记录. 域完整性:表中的字段. 数据类型约束: 非空约束: 唯一约束: 参照完整性: 多表设计 ...
- 导出Execel
//请求入口 @SuppressWarnings("serial")@ParentPackage("default") //action请求@Results( ...
- WIX: Hide installed program from the Add/Remove Programs window.
Reference article : How to hide an entry in the Add/Remove Programs applet? In Wix source files, set ...
- [转载]浅析STL allocator
本文转载自水目沾博客:http://www.cnblogs.com/zhuwbox/p/3699977.html 向大师致敬 一般而言,我们习惯的 C++ 内存配置操作和释放操作是这样的: 1 c ...
- 通过 ANE(Adobe Native Extension) 启动Andriod服务 推送消息(五)
这一节,用个简单的例子来调用下之前生成的service.ane 首先建一个flex手机项目 然后在构建路径中把ane引进来 可以看到此ane支持Android平台. serviceMobile.mxm ...
- QtSQL学习笔记(2)- 连接到数据库
要使用QSqlQuery或者QSqlQueryModel访问一个数据库,首先需要创建并打开一个或多个数据库连接(database connections). 一般地,数据库连接是根据连接名(conne ...
- php 开启缓冲,页面纯静态化
服务器默认不开启php缓冲区 两种方法开启 1.php.ini out_put_buffer = on 2.ob_start(); 页面纯静态化 file_put_contents()写文件 ob_s ...
- C# 目录与文件管理
文件读写 学习了一点点希望对以后的学习工作有帮助 在应用程序中基本任务是对数据的操作,这就是对数据进行访问和保存挤兑数据的读写,应用程序访问一个文本文件叫做“读”:对文本文件的内容进行修改后保存这些修 ...