hdoj--1027--Ignatius and the Princess II(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
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int num[1010],vis[1010];
int n,ans;
bool f;
void dfs(int x)
{
if(x==n+1)
{
if(ans>0) ans--;
else
{
f=true;
for(int i=1;i<n;i++)
printf("%d ",num[i]);
printf("%d\n",num[n]);
}
}
if(f) return ;
for(int i=1;i<=n;i++)
{
if(!vis[i])
{
vis[i]=1;
num[x]=i;
dfs(x+1);
vis[i]=0;
}
}
}
int main()
{
while(scanf("%d%d",&n,&ans)!=EOF)
{
ans--;
f=false;
memset(num,0,sizeof(num));
memset(vis,0,sizeof(vis));
dfs(1);
}
return 0;
}
hdoj--1027--Ignatius and the Princess II(dfs)的更多相关文章
- 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 ...
- HDU 1027 Ignatius and the Princess II(康托逆展开)
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 ( ...
- Ignatius and the Princess II(全排列)
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
- HDOJ.1029 Ignatius and the Princess IV(map)
Ignatius and the Princess IV 点我跳转到题面 点我一起学习STL-MAP 题意分析 给出一个奇数n,下面有n个数,找出下面数字中出现次数大于(n+1)/2的数字,并输出. ...
- HDOJ 1028 Ignatius and the Princess III (母函数)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- hdu 1027 Ignatius and the Princess II(产生第m大的排列,next_permutation函数)
题意:产生第m大的排列 思路:使用 next_permutation函数(头文件algorithm) #include<iostream> #include<stdio.h> ...
- hdu 1027 Ignatius and the Princess II(正、逆康托)
题意: 给N和M. 输出1,2,...,N的第M大全排列. 思路: 将M逆康托,求出a1,a2,...aN. 看代码. 代码: int const MAXM=10000; int fac[15]; i ...
- hdu1027 Ignatius and the Princess II (全排列 & STL中的神器)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=1027 Ignatiu ...
随机推荐
- Robomongo 0.9.0 连接mongo数据库时,提示连接失败 的解决方案
Robomongo 0.9.0 连接mongo数据库时,提示连接失败.(IP和端口号确定是对的) 基本注意点: 1.mongodb服务打开,打开时,指定端口号,默认为27017,使用默认值,则不用指定 ...
- JSP中如何利用JS实现登录页面的跳转(JSP中如何利用JS实现跳转页面)
<%! <% url = word = } ...
- java攻城师之路--复习java web之Cookie_Session
Servlet技术 用来动态生成 网页数据资源Servlet生成HTML 页面数据时,所有内容都是通过 response.getWriter response.getOutputStream 向浏览器 ...
- Java_Web三大框架之Hibernate+jsp+selvect+HQL查询数据
俗话说:"好记性不如烂笔头".本人学习Hibernate也有一个星期了,对Hibernate也有一个初步的了解.下面对Hibernate显示数据做个笔记,使用租房系统的Hibern ...
- Mysql 之主从复制,mysql-proxy读写分离
准备两台mysql服务器,master(192.168.43.64).slave(192.168.84.129) master配置: log-bin=mysql-bin binlog_format=m ...
- Ubuntu 常用解压与压缩命令
参考 https://blog.csdn.net/songbinxu/article/details/80435665 示例: 把/home/wangju/gitlab/automationTest目 ...
- HTML5易漏知识点锦集
本文通过对w3schoolHTML5基础教程,整理出比较常见的却又容易遗忘或者忽略的HTML5相关知识点.本文的标题顺序与w3school中的HTML5基础教程标题顺序保持一致.适合翻阅和复习. 1. ...
- IO文件读取
/** *按字节读取文件 */@Testpublic void readerByte() { File file = new File("D:\\BindCheckControllerTes ...
- [C#] 序列化实现对象的深拷贝
//对象深拷贝 public static T Copy<T>(T oldObject) where T : class,new() { T newOrder = new T(); Mem ...
- CF51F Caterpillar (边双+树形DP)
题目传送门 题目大意:给你一张n个点m条边的图.每次操作可以把两个点合并成一个(与之相连的边也都要连到新点上).求把图中每个联通块都变成“毛毛虫”的最小操作次数.“毛毛虫”必须是一棵树(可以存在自环) ...