HDU1027 Ignatius and the Princess II 【next_permutation】【DFS】
Ignatius and the Princess II
you can work them out, I will release the Princess, or you will be my dinner, too." Ignatius says confidently, "OK, at last, I will save the Princess."
"Now I will show you the first problem." feng5166 says, "Given a sequence of number 1 to N, we define that 1,2,3...N-1,N is the smallest sequence among all the sequence which can be composed with number 1 to N(each number can be and should be use only once
in this problem). So it's easy to see the second smallest sequence is 1,2,3...N,N-1. Now I will give you two numbers, N and M. You should tell me the Mth smallest sequence which is composed with number 1 to N. It's easy, isn't is? Hahahahaha......"
Can you help Ignatius to solve this problem?
file.
6 4
11 8
1 2 3 5 6 4
1 2 3 4 5 6 7 9 8 11 10
题意:求数列1~n的第m个全排列。
题解:用STL里的next_permutation和DFS都可解,但时间复杂度就相去甚远了。
STL:耗时31ms
#include <cstdio>
#include <algorithm>
using namespace std; int arr[1002]; int main()
{
int n, m, i;
while(scanf("%d%d", &n, &m) != EOF){
for(i = 0; i < n; ++i)
arr[i] = i + 1;
while(--m) next_permutation(arr, arr + n);
for(i = 0; i < n; ++i)
if(i != n - 1) printf("%d ", arr[i]);
else printf("%d\n", arr[i]);
}
return 0;
}
DFS:耗时390ms
#include <stdio.h>
#include <string.h>
#define maxn 1002 int vis[maxn], n, m, arr[maxn], count, ok; void PRINT()
{
for(int i = 1; i <= n; ++i)
if(i != n) printf("%d ", arr[i]);
else printf("%d\n", arr[i]);
} void DFS(int k)
{
if(k > n) return;
for(int i = 1; i <= n; ++i){
if(!vis[i]){
vis[i] = 1; arr[k] = i;
if(k == n && m == ++count){
PRINT(); ok = 1; return;
}
DFS(k + 1); vis[i] = 0;
if(ok) return;
}
}
} int main()
{
while(scanf("%d%d", &n, &m) != EOF){
memset(vis, 0, sizeof(vis));
ok = count = 0; DFS(1);
}
return 0;
}
HDU1027 Ignatius and the Princess II 【next_permutation】【DFS】的更多相关文章
- hdu1027 Ignatius and the Princess II (全排列 & STL中的神器)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=1027 Ignatiu ...
- HDU1027 Ignatius and the Princess II
Problem Description Now our hero finds the door to the BEelzebub feng5166. He opens the door and fin ...
- HDU1027 Ignatius and the Princess II( 逆康托展开 )
链接:传送门 题意:给出一个 n ,求 1 - n 全排列的第 m 个排列情况 思路:经典逆康托展开,需要注意的时要在原来逆康托展开的模板上改动一些地方. 分析:已知 1 <= M <= ...
- 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 ( ...
- ACM-简单题之Ignatius and the Princess II——hdu1027
转载请注明出处:http://blog.csdn.net/lttree Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Othe ...
- ACM-简单的主题Ignatius and the Princess II——hdu1027
转载请注明出处:http://blog.csdn.net/lttree Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Othe ...
- (next_permutation)Ignatius and the Princess II hdu102
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
- 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 ( ...
随机推荐
- OpenSSL命令---req
用途: 本指令用来创建和处理PKCS#10格式的证书.它还能够建立自签名证书,做Root CA. 用法: openssl req [-inform PEM|DER] [-outform PEM|DER ...
- OpenSSL命令---rand
用途: 用来产生伪随机字节.随机数字产生器需要一个seed,先已经说过了,在没有/dev/srandom系统下的解决方法是自己做一个~/.rnd文件.如果该程序能让随机数字产生器很满意的被seeded ...
- Android的回调
学了两三周的安卓了,最先开始是看mars老师的视频,看了一两天结合慕课网上的一些安卓视频,到现在算是有点入门了. 安卓立用得比较多的回调函数有点不明是怎么实现的,网上找了一些资料,结合自己的实践,总算 ...
- Linux gcc/g++下GDB调试及其调试脚本的使用
GDB调试及其调试脚本的使用返回脚本百事通一.GDB调试 1.1. GDB 概述 GDB 是GNU开源组织发布的一个强大的UNIX下的程序调试工具.或许,各位比较喜欢那种图形界面方式的,像VC.BCB ...
- Java学习笔记:内部类/匿名内部类的全面介绍
编写java程序时,一般一个类(或者接口)都是放在一个独立的java文件中,并且类名同文件名(如果类是public的,类名必须与文件名一致:非public得,无强制要求).如果想把多个java类放在一 ...
- vs2010断点使用技巧
msdn:http://msdn.microsoft.com/zh-cn/library/vstudio/5557y8b4.aspx 有很多断点的文章. 为提供更大的灵活性,Visual Studio ...
- 已知的CPropertysheet bug: 切换焦点导致无响应
当一个页面内容比较多时我们首先可能考虑用Tab Control,但如果有很多页面内容需要动态加载则用CPropertySheet比较好点~ CPropertySheet有两种不同的显示模式.一种就是向 ...
- c语言中float、double、long double在内存中存储方式
存储格式中的二机制转为浮点数: 浮点型变量在计算机内存中占用4个字节(4 Byte),即32-bit,一个浮点数由2部分组成:底数m 和 指数e: 底数部分:使用2进制数来表示此浮点数的实际值: 指 ...
- 2008r2 做windows域控制器
新配一个: 1.装DNS服务. 2.装domain管理. config domain: 客户端172.16.1.34 ping zyctest
- 在Vista以上版本运行WTL程序,有时候会提示“这个程序可能安装补正确...”的错误
在Win7/Vista下,如何以兼容模式运行exe? https://msdn.microsoft.com/en-us/library/dd371711(VS.85).aspx 问题描 ...