Topic: P2524 Uim Valentine's Day gift of their · Er: https://www.luogu.org/problemnew/show/P2524
Title Description
Prequel: see Luo Gu P2525
Uim successfully sent them in the order of the hands of the N sister and maintain their harmony.
Uim now want to know, he chose the order that all of the N sister gift order, lexicographical first of several small.
Input and output formats
Input formats:A first line integer N, the number of expressed N.
A second line integer X, represents a given arrangement.
Output formats:An integer that represents the first of several small lexicographical order.
Sample input and output
3 231
4
Explanation
1<=N<=9
No spaces are arranged entered
Title very simple, very small range of data.
We can use next_permutation (&, & + n) to solve all cases of violence and judgment.
Here, the main explanation is open to expand health. (Information Baidu , blog )
Ah, very good. After reading the above information should be very clear.
Here is the note:
1. Be sure to find a number smaller than its current number to the right of (>).
2. Each number (n) multiplied! (Tot-n-1) wherein tot is the total number of the last must be decremented. (In this case, n 0 from beginning to end)
3. The final calculation result is - arranged lexicographically arranged smaller than the current, and arranged in order but also add a current.
AC Code:
1 // 2 #include <bits/stdc++.h> 3 using namespace std; 4 typedef long long ll; 5 #define ri register ll 6 7 ll n; 8 string s; 9 10 ll jc(ll a) 11 { 12 ri ans=1; 13 for(ri i=1;i<=a;i++) 14 { 15 ans*=i; 16 } 17 return ans; 18 } 19 20 ll cantor() 21 { 22 ri end=0; 23 for(ri i=0;i<=n-1;i++) 24 { 25 ri smaller=0; 26 for(ri j=i+1;j<=n-1;j++) 27 { 28 if(s[i]>s[j]) smaller++; 29 } 30 end+= smaller*jc(n-i-1); 31 } 32 return end+1; 33 } 34 35 signed main() 36 { 37 ios::sync_with_stdio(0),cin.tie(0); 38 cin>>n; 39 cin>>s; 40 cout<<cantor(); 41 return 0; 42 } 43 //