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 ( ...
随机推荐
- 代理丶通知丶KVO之间区别和各自优势
文/OyeOnoOmg(简书作者)原文链接:http://www.jianshu.com/p/75d3fd218a23著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 今天在网上看到一个 ...
- [转]Windows7 64bit下配置Apache+PHP+MySQL
原文链接:http://blog.csdn.net/sbz0409/article/details/12946375 1.准备资料: 首先下载文件:Apache2.4.16 64bit,http:// ...
- web Listener
在web应用内部会不断地发生各种事件例如,web应用被启动,web应用停止,用户session开始,用户session 结束,用户请求到达,通常这些事件对开发者而言是透明的.实际上ServletAPI ...
- JMS 企业开发流程实现
关于JMS的一些介绍参见[http://blog.csdn.net/aking21alinjuju/article/details/6051421] [补充] 消息的组成 1. 头(head) 每条J ...
- android getDecorView()的作用
decorView是window中的最顶层view,可以从window中通过getDecorView获取到decorView.通过decorView获取到程序显示的区域,包括标题栏,但不包括状态栏.间 ...
- zk create() 方法
create() $path = $zkh->create($req_path, $data); $path = $zkh->create($req_path, $data, 'flags ...
- oschina插件和扩展
Eclipse插件 2551FireFox插件 14IE插件 19Prototype扩展 220Chrome插件/扩展 72WordPress插件 33NetBeans插件 12IDEA插件 25Xc ...
- 设置不输入密码ssh登录
在/etc/hosts文件下加入: 192.168.1.60 u60 #设置u60为主机名 在每个节点上创建RSA秘钥: # ssh-keygen -t rsa # 一直按确定键即可 # touch ...
- eclipse No Default Proposals 无提示
链接地址:http://blog.csdn.net/rogerjava/article/details/5689785 今天特抑郁,早上开机后发现eclipse的代码提示功能不好使了,Alt+/ 这么 ...
- inheritAll 及 ant antfile案例分析
<?xml version="1.0"?> <project name="a" default="targeta"> ...