关闭

hdu1027Ignatius and the Princess II <vector>

319人阅读 评论(0) 收藏 举报
分类:

搜分类是数学或者STL,看题解数学的方法没看懂,STL的vector真心赞

http://blog.sina.com.cn/s/blog_9f7ea4390101101u.html 讲next_permutation 好东西啊

本题属于链接的最后一种情况吧

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int n,m,i;
int main()
{
    while(cin>>n>>m)
    {
        vector<int>ivec;
        ivec.clear();
        vector<int>::iterator iter;
        for(i=1;i<=n;i++) ivec.push_back(i);
        for(i=1;i<m;i++) next_permutation(ivec.begin(),ivec.end());
        for(i=0,iter=ivec.begin();iter!=ivec.end();++i,++iter)//iter之前一直写成i编译提示我需要运算符重载orz
         cout<<(i==0?"":" ")<<*iter;
         cout<<endl;
    }
    return 0;
}

Problem Description
Now our hero finds the door to the BEelzebub feng5166. He opens the door and finds feng5166 is about to kill our pretty Princess. But now the BEelzebub has to beat our hero first. feng5166 says, "I have three question for you, if 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?
 

Input
The input contains several test cases. Each test case consists of two numbers, N and M(1<=N<=1000, 1<=M<=10000). You may assume that there is always a sequence satisfied the BEelzebub's demand. The input is terminated by the end of file.
 

Output
For each test case, you only have to output the sequence satisfied the BEelzebub's demand. When output a sequence, you should print a space between two numbers, but do not output any spaces after the last number.
 

Sample Input
6 4 11 8
 

Sample Output
1 2 3 5 6 4 1 2 3 4 5 6 7 9 8 11 10

0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

HDU--杭电--1026--Ignatius and the Princess I--广搜--直接暴力0MS,优先队列的一边站

别人都是广搜+优先队列,我没空临时学,所以就直接自己暴力了 Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)  ...
  • hncu1206401liuhao
  • hncu1206401liuhao
  • 2013-08-03 14:04
  • 1990

机器学习笔记:Fisher Vector基本原理与用法

近期在看的动作识别相关的工作中fisher vector及其改进版本被广泛的应用,因此打算从Fisher Vector开始入手整理相关知识。 参考的博客内容: http://blog.csdn...
  • wzmsltw
  • wzmsltw
  • 2016-07-27 22:20
  • 7422

Micrium 开放了µC/OS-III源代码

Micrium 开放了µC/OS-III源代码  发布时间:2011-8-19 9:27:29   发布方:Dave   点击数:1641 高品质嵌入式系统...
  • autumn20080101
  • autumn20080101
  • 2013-07-25 15:29
  • 1460

HDU1027 Ignatius and the Princess II

Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...
  • d_x_d
  • d_x_d
  • 2016-03-04 20:53
  • 1271

hdu1027 Ignatius and the Princess II(全排列)

题意:给定两个整数N、M。将1~N排列组合,按字典序从小到大排列,求出排在第M个的排列。 虽然N的范围很大,到1000,1000的阶乘很吓人呀,但是M的范围只到10000,而8的阶乘就已经超过1000...
  • Willona_C
  • Willona_C
  • 2015-07-16 23:18
  • 219

Ignatius and the Princess II (HDU 1027) ——next_permutation排列函数

Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...
  • jxust_tj
  • jxust_tj
  • 2014-05-17 21:23
  • 810

hdu 1027 ignatius and the princess ii

Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...
  • chengouxuan
  • chengouxuan
  • 2011-10-15 13:52
  • 701

ACM-简单题之Ignatius and the Princess II——hdu1027

ACM 简单题 Ignatius and the Princess II hdu1027 康拓展开
  • lx417147512
  • lx417147512
  • 2014-05-19 14:35
  • 1788

hdu1027Ignatius and the Princess II

/*此题是博主做过的最简单的dfs,但不得不说很经典的dfs的思路,简单的思路是在给定的一序列中找到第m个最小序列,转换一下思路,若将初始序列作为一个排列,那么每次都是在向下找一个排列的问题,最终找到...
  • small__snail__5
  • small__snail__5
  • 2016-10-07 22:07
  • 181

HDU1027_Ignatius and the Princess II_非模拟做法

Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...
  • yuege38
  • yuege38
  • 2017-03-02 22:13
  • 97
    碎碎念
    周小姐,你还想毕业找不到工作吗????
    个人资料
    • 访问:262022次
    • 积分:8585
    • 等级:
    • 排名:第2643名
    • 原创:605篇
    • 转载:25篇
    • 译文:0篇
    • 评论:38条
    友情链接