【第K排列问题】

Cow Line

Description

The N (1 <= N <= 20) cows conveniently numbered 1...N are playing yet another one of their crazy games with Farmer John. The cows will arrange themselves in a line and ask Farmer John what their line number is. In return, Farmer John can give them a line number and the cows must rearrange themselves into that line.

A line number is assigned by numbering all the permutations of the line in lexicographic order.

Consider this example:
Farmer John has 5 cows and gives them the line number of 3.
The permutations of the line in ascending lexicographic order:
1st: 1 2 3 4 5
2nd: 1 2 3 5 4
3rd: 1 2 4 3 5
Therefore, the cows will line themselves in the cow line 1 2 4 3 5.

The cows, in return, line themselves in the configuration "1 2 5 3 4" and 
ask Farmer John what their line number is.

Continuing with the list:
4th : 1 2 4 5 3
5th : 1 2 5 3 4
Farmer John can see the answer here is 5

Farmer John and the cows would like your help to play their game. They have K (1 <= K <= 10,000) queries that they need help with. Query i has two parts: C_i will be the command, which is either 'P' or 'Q'.

If C_i is 'P', then the second part of the query will be one integer A_i (1 <= A_i <= N!), which is a line number. This is Farmer John challenging the cows to line up in the correct cow line.

If C_i is 'Q', then the second part of the query will be N distinct  integers B_ij (1 <= B_ij <= N). This will denote a cow line. These are the cows challenging Farmer John to find their line number.

Input

* Line 1: Two space-separated integers: N and K

* Lines 2..2*K+1: Line 2*i and 2*i+1 will contain a single query.

Line 2*i will contain just one character: 'Q' if the cows are lining up and asking Farmer John for their line number or 'P' if Farmer John gives the cows a line number.

If the line 2*i is 'Q', then line 2*i+1 will contain N space-separated integers B_ij which represent the cow line. If the line 2*i is 'P', then line 2*i+1 will contain a single integer A_i which is the line number to solve for.

Output

* Lines 1..K: Line i will contain the answer to query i.

If line 2*i of the input was 'Q', then this line will contain a single integer, which is the line number of the cow line in line 2*i+1.

If line 2*i of the input was 'P', then this line will contain N space separated integers giving the cow line of the number in line 2*i+1.

Sample Input

5 2
P
3
Q
1 2 5 3 4
Sample Output
1 2 4 3 5
5

Problem Source: USACO FEB11

题目就是问给出P和一个数字k时求出1~n所有按字典序的排列中第k个排列是什么,给出Q和一个排列时问这个排列是第几个?
可以用数学的方法解决,首先我们知道当固定前i个的时候剩下就有(n-i)!种组合方法,我们设一个集合S,初始时集合中所有元素都没有使用过,可以算出第i位在未使用过的集合中的序号是num = (k-1)/(n-i)!+1,然后k =k -  (num-1)*(n-i)!,一直这样递推下去即可
Code
LL f[22];
bool use[22];
int main(){
    int n,m;
    scanf("%d%d",&n,&m);
        int i,j;
        while(m--){
            char str[3];
            memset(use,0,sizeof(use));
            f[1] = 1;
            for(i=2;i<n;i++){
                f[i] = f[i-1]*i;
            }
            scanf("%s",str);
            if(str[0]=='P'){
                LL k;
                cin>>k;
                for(i=1;i<n;i++){
                    LL num = (k-1)/f[n-i];
                    num++;
                    LL cnt = 0;
                    for(j=1;j<=n;j++){
                        if(!use[j])cnt++;
                        if(cnt==num){
                            printf("%d ",j);
                            use[j] = 1;
                            break;
                        }
                    }
                    k -= (num-1)*f[n-i];
                }
                for(i=1;i<=n;i++)if(!use[i])printf("%d\n",i);
            } else {
                int a;
                LL ans = 0;
                for(i=1;i<n;i++){
                    scanf("%d",&a);
                    LL cnt = 0;
                    for(j=1;j<a;j++){
                        if(!use[j])cnt++;
                    }
                    use[a] = 1;
                    ans += cnt*f[n-i];
                }
                scanf("%d",&a);
                cout<<ans+1<<endl;
            }
        }
    return 0;
}

ps:UVA 12335 是上一题的小小变形





















11086 排序问题再探讨 时间限制:1000MS 内存限制:65535K 提交次数:0 通过次数:0 题型: 编程题 语言: 无限制 Description 此题以程序填空的形式进行,请将下列程序框架复制到本机,并按下面要求填充完整后再用g++编译器提交, 在不改变程序框架情况下,可以自由添加所需的函数和变量,或修改合适的函数参数。 1,请改写一个"递归"的插入排序,排序a[0…n-1],先递归的排序a[0…n-2],然后再将a[n-1]插入到已排序的a[0…n-2]中去。 2,自然合并排序,书上2.7节最后介绍的算法,请实现它。 3,快速排序,选择"中位数"作为轴值然后进行左右段分区,请实现它。 #include #include "stdlib.h" using namespace std; const int SIZE = 10001; int a[SIZE]; void RecurInsertionSort(int p, int q) //对a[p…q]的递归插入排序,参数可根据自己需要修改。 { …… } void NaturalMergeSort(int n) //对n个元素的自然合并排序,参数可根据自己需要修改。 { …… } int Partition(int x, int p, int q) //以x为基准元素划分a[p…q],返回基准下标. 书上2.8节有。参数可根据自己需要修改。 { …… } int median(int p, int q) //挑出a[p…q]的中位数,并返回中位数,参数可根据自己需要修改。 { …… } void QuickSort(int p,int q) //参数可根据自己需要修改。 { if(p>=q)return; int x = median(p, q); int i=Partition(x,p,q); QuickSort(p,i-1); QuickSort(i+1,q);//递归 } int main() { int i,n; cin >> n; //递归插入排序 for(i=0;i> a[i]; } RecurInsertionSort(0,n-1); cout << "Insert sort: "; for(i=0;i<n;i++) { cout << a[i] << ' '; } //自然合并排序 for(i=0;i> a[i]; } NaturalMergeSort(n); cout << "\nNatural merge sort: "; for(i=0;i<n;i++) { cout << a[i] << ' '; } //快速排序 for(i=0;i> a[i]; } QuickSort(0, n-1); cout << "\nQuick sort: "; for(i=0;i<n;i++) { cout << a[i] < 1) { RecurInsertionSort(p, q-1); Insert(p,q); } else return; } 2,自然合并排序 参照书上的思想. 3.选择问题:选中位数。用随机选轴值的“快速选择算法”获得,随机选轴值可以获得比较好的性能,倒是无须用“中间的中间”选轴值那么麻烦。 作者 zhengchan --------------------------------------------------------------------------------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值