Combined mathematics + STL --- use STL to generate full Arrangement

Source: Internet
Author: User
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 descriptionnow 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 shocould 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 shoshould tell m E 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?

 

Inputthe 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.

 

Outputfor each test case, you only have to output the sequence satisfied the Beelzebub's demand. when output a sequence, you shocould print a space between two numbers, but do not output any spaces after the last number.

 

Sample input6 411 8

 

Sample output1 2 3 5 6 41 2 4 5 6 7 9 8 11 10

 

Authorignatius. L

 

Mean: 

Give you n and M, let you output from 1 ~ N. The N number is in the ascending order of the M sequence.

Analyze:

If we use brute force, this question must have timed out. Fortunately, STL has such a function and can be called directly.

The following describes how to use the next_permutation function:

Check the next_permutation function declaration in C ++ reference:

 

#include <algorithm>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.

 

The returned value of next_permutation is boolean. Follow the prompts to write a standard C ++ program:

# Include <iostream> # include <algorithm> # include <string> using namespace STD; int main () {string STR; //// other containers can also be CIN> STR; sort (Str. begin (), str. end (); cout <STR <Endl; while (next_permutation (Str. begin (), str. end () {cout <STR <Endl;} return 0 ;}

The sort function and string. Begin () and string. End () are also used. The function declaration is as follows:

#include <algorithm>void sort( iterator start, iterator end );

The sort function can use the complexity of nlogn to sort data within the parameter range.

#include <string>iterator begin();const_iterator begin() const;#include <string>iterator end();const_iterator end() const;

String. Begin () and string. End () can quickly access the first and last characters of a string.

I found that the efficiency of the above functions is not very high. Maybe I didn't enable g ++-O2 optimization ......
The following program is replaced with puts for output, which is much faster than above.

#include <cstdio>#include <algorithm>#include <cstring>#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<algorithm>#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<vector>#include<queue>#include<stack>#include<map>#include<string>#include<climits>#include<cmath>#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<int> 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<n-1;i++)                {                    printf("%d ",a[i]);                }                printf("%d\n",a[n-1]);                break;            }        }    }    return 0;}

  

Combined mathematics + STL --- use STL to generate full Arrangement

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.