c-Count Order
Editorial
Time Limit: 2 sec/ Memory Limit:1024MB
Score: 300 points
Problem Statement
We have two permutations Pand Q of size N(that is, Pand Q are both rearrangements of(1,2,…,N)).
There are N!possible permutatons of size N.Among them,let Pand Q be the a-th and b-th lexicographically smallest permutations,respectively.Find la - b|
Notes
For two sequencs X and Y, X is said to be lexicographiclly smaller than Y if and only if there exists an integer k such that Xi=Yi(1<=i<k)and Xk <Yk .
Constraints
.2<=N<=8
. Pand Q are permutations of size N.
lnput
Input is given from Standard Input in the following format:
N
P1 P2… PN
Q1 Q2… QN
Output
Print la - bl.
题解:
当且仅当存在k−1个排列时,X是字典序中的第k个
小于x,因此,可以通过生成所有的
长度为N的排列,计算有多少排列小于P Q。
在生成排列时,您可以执行搜索,例如深度优先搜索,或者您
还可以利用函数生成排列,比如c++中的next_permutation排列函数。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<time.h>
#include<vector>
using namespace std;
int main()
{
//ios::sync_with_stdio(false);
//cin.tie(0);
//cout.tie(0);
int n,t2 = 0,t1 = 0;
scanf("%d",&n);
int a[10][10];
for(int i=0; i<n; ++i)
a[0][i] = i+1;
for(int j=1; j<=2;++j)
for(int i=0; i<n; ++i)
scanf("%d",&a[j][i]);
do{
t1++;
int flag = 0;
for(int i=0; i<n; ++i){
if(a[0][i]!=a[1][i]){
flag = 1;
break;
}
}
if(!flag)
break;
}while(next_permutation(a[0],a[0]+n));
for(int i=0; i<n; ++i)
a[0][i] = i+1;
/*
for(int i=0; i<n; ++i)
printf("%d",a[2][i]);
printf("\n");
*/
do{
/*
for(int i=0; i<n; ++i)
printf("%d",a[0][i]);
printf("\n");*/
t2++;
int flag = 0;
for(int i=0; i<n; ++i){
if(a[0][i]!=a[2][i]){
flag = 1;
break;
}
}
if(!flag){
break;
}
}while(next_permutation(a[0],a[0]+n));
//printf("%d %d\n",t1,t2);
printf("%d",abs(t1-t2));
return 0;
}