博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
组合数学 + STL --- 利用STL生成全排列
阅读量:6292 次
发布时间:2019-06-22

本文共 4052 字,大约阅读时间需要 13 分钟。

Ignatius and the Princess II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 4730    Accepted Submission(s): 2840

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
 

 

Author
Ignatius.L
  

 

Mean: 

 给你一个n和m,让你输出从1~n这n个数全排列的第m个序列。

analyse:

 如果我们用暴力的话,这题肯定超时,还好STL中自带了个这样的函数,可以直接调用。

下面来讲解一下 next_permutation  函数的运用:

在C++ Reference中查看了一下next_permutation的函数声明:

 

#include 
bool next_permutation( iterator start, iterator end );The next_permutation() function attempts to transform the given range of elements [start,end) into the next lexicographically greater permutation of elements. If it succeeds, it returns true, otherwise, it returns false.

 

从说明中可以看到 next_permutation 的返回值是布尔类型。按照提示写了一个标准C++程序:

#include 
#include
#include
using namespace std; int main(){ string str; 也可以是其他容器 cin >> str; sort(str.begin(), str.end()); cout << str << endl; while (next_permutation(str.begin(), str.end())) { cout << str << endl; } return 0;}

其中还用到了 sort 函数和 string.begin()、string.end() ,函数声明如下:

#include 
void sort( iterator start, iterator end );

sort函数可以使用NlogN的复杂度对参数范围内的数据进行排序。

#include 
iterator begin();const_iterator begin() const;#include
iterator end();const_iterator end() const;

string.begin()和string.end() 可以快速访问到字符串的首字符和尾字符。

发现以上函数的效率不是很高,可能是没开 g++ -O2 优化吧……

下面的程序换成puts来输出,比上面快多了。

#include 
#include
#include
#define MAX 100 using namespace std; int main(){ int length; char str[MAX]; gets(str); length = strlen(str); sort(str, str + length); puts(str); while (next_permutation(str, str + length)) { puts(str); } return 0;}

  

 

 

Time complexity:O(n)?

 

Source code:

 

// Memory   Time// 1347K     0MS// by : Snarl_jsb// 2014-09-15-22.17#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define N 1000010#define LL long longusing namespace std;int main(){// freopen("C:\\Users\\ASUS\\Desktop\\cin.cpp","r",stdin);// freopen("C:\\Users\\ASUS\\Desktop\\cout.cpp","w",stdout); int n,m; vector
a; while(cin>>n>>m) { a.clear(); for(int i=1;i<=n;++i) { a.push_back(i); } int cnt=1; while(next_permutation(a.begin(),a.end())) { cnt++; if(cnt==m) { for(int i=0;i

  

转载于:https://www.cnblogs.com/crazyacking/p/3973910.html

你可能感兴趣的文章
初学structs2,简单配置
查看>>
Laravel5.0学习--01 入门
查看>>
时间戳解读
查看>>
sbin/hadoop-daemon.sh: line 165: /tmp/hadoop-hxsyl-journalnode.pid: Permission denied
查看>>
@RequestMapping 用法详解之地址映射
查看>>
254页PPT!这是一份写给NLP研究者的编程指南
查看>>
《Data Warehouse in Action》
查看>>
String 源码浅析(一)
查看>>
Spring Boot 最佳实践(三)模板引擎FreeMarker集成
查看>>
Fescar 发布 0.2.3 版本,支持 Redis 和 Apollo
查看>>
Google MapReduce到底解决什么问题?
查看>>
CCNP-6 OSPF试验2(BSCI)
查看>>
Excel 2013 全新的图表体验
查看>>
openstack 制作大于2TB根分区自动扩容的CENTOS镜像
查看>>
Unbuntu安装遭遇 vmware上的Easy install模式
查看>>
几个常用的ASP木马
查看>>
python分析postfix邮件日志的状态
查看>>
Mysql-5.6.x多实例配置
查看>>
psutil
查看>>
在git@osc上托管自己的代码
查看>>