Ignatius and the Princess II(フル配列アルゴリズム)

3009 ワード

Ignatius and the Princess II
タイトルの説明
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.
サンプル入力:6 4 11 8サンプル出力:1 2 3 3 5 6 4 1 2 3 4 5 6 7 8 11 10タイトルリンク:http://acm.hdu.edu.cn/showproblem.php?pid=1027
考え方:
この問題は多組のテストで、問題の大意はm,nを入力して、n番目のmの最小の全列のシーケンスを求めます.例えば6 4と入力する.1~6を全配列:1 2 3 4 5 6 1 2 3 4 6 6 5 1 3 5 4 6 1 2 3 6 4...すなわち出力4番目の配列:1 2 3 6 4.この問題はC++STLの全配列の中の関数next_を使うことができますpermutation next_の使用permutationはヘッダファイルincludenext_を導入する必要があるpermutation:次の配列方法を求める使用方法はnext_permutation(a,a+a.size()は、aが配列名であり、その戻り値はbool型であり、次の配列方式が存在しない場合falseを返し、残りはtrueを返し、配列されたデータは元の配列に格納される.それに対してprev_permutation関数は、前の配列を求める方法です.next_permutationの使用方法は同様で、戻り値はbool型です.
全配列アルゴリズムの構想:全配列は再帰によって実現され、一つの数を絶えず座標とし、次の数を探し、次の数を新しい座標(座標前のデータは変わらない)として、再帰によってすべてのデータを探すまで、全配列方式が得られる.
#include 
using namespace std;
void Permutations(int a[],int begin,int end)  //     
{
    if(begin == end)   //            
    {
        for(int i = 0;i<=end;i++)
            cout << a[i] << ' ';
        cout <> n)
    {
        int a[n];
        for(int i = 0;i

コード:
#include 
#include 
using namespace std;
int main()
{
    int n,m;
    while(cin >> n >> m)
    {
        int a[n];
        for(int i = 0;i