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 ( ...
随机推荐
- Linux下Django开发学习(一)
一,环境配置: 环境:Ubuntu 14.04 LTS, Python 2.7, Django 1.8 安装Python : Ubuntu自带python2.7,不用安装 安装pip:sudo ap ...
- Android:ServiceDemo
效果图: layout的main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLay ...
- python成长之路——第二天
cpython:c解释器 .pyc(字节码)——机器码 jpython :java解释器 java字节码 ironpython :C#解释器 C#字节码 .... 上面的:编译完之后 ...
- CentOS 添加常用 yum 源
CentOS 的官方源去掉了一些与版权有关的软件,因此想要安装这些软件或者手动下载安装,或者使用其他源. 下面我推荐常用的两个源, 这两个源基本可以满足一般服务器的使用需求. 首先, 添加源之前要确定 ...
- CocoaPods on Xcode 6 and Yosemite
老子今天又给环境跪了..... cocoapods 在升级完新系统以后无法工作 解决cocoapods 在 mac 10.10下报错 错误例如以下. /System/Library/Framework ...
- Wakelock API详解
官方资料 http://developer.android.com/intl/zh-CN/reference/android/os/PowerManager.WakeLock.html http:/ ...
- getopt、getopt_long和getopt_long_only
GNU/Linux的命令行选项有两种类型:短选项和长选项,前者以 '-' 作为前导符,后者以 '--' 作为前导符.比如有一个命令: $ myprog -a vv --add -b --file a. ...
- jsp页面格式化数字或时间
Tags fmt:requestEncoding fmt:setLocale fmt:timeZone fmt:setTimeZone fmt:bundle fmt:setBundle fmt:m ...
- JMS开源比较
Java开源JMS消息中间件 mom4j mom4j是一个完全实现JMS1.1规范的消息中间件并且向下兼容JMS1.0与1.02.它提供了自己的消息处理存储使它独立于关系数据与语言,所以它的客户端可以 ...
- [zencart教程]zencart外贸建站仿站交流俱乐部
[zencart教程]zencart外贸建站仿站交流俱乐部 1.你想自主一天仿做一个精美的zencart 外贸网站; 2.你想自已自主定制精美的psd 图 zencart模板,并把它变成自定义精美 z ...