字典序:(联合康托展开就也可以按照中介数求)

邻位对换、递增进位制数,递减进位制数:具体的实现和算法讲解如下:

代码。。C++版的实现并不好。。因为是挨个向后找的,如果K很大的时候会超时,不过。。。思路是这样。。。,第二版C++没有完成。。。因为不想写了,思路很简单,一次加减K就可以了

python代码直接给出,很完美

 #include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm> using namespace std;
void Swap(int &a, int &b)
{
a == b ? : a ^= b ^= a ^= b;
}
void Print(int A[],int n)
{
for (int i = ; i < n; i++)
{
printf("%d%c", A[i], i == n - ? '\n' : ' ');
}
}
//阶乘
int factorial(int x) { return x > ? x*factorial(x - ) : ; }
//字典序法
void Reverse(int A[],int a,int b)//反转
{
while (a < b)
{
Swap(A[a], A[b]);
a++;
b--;
}
}
bool my_next_permutation(int A[], int n)
{
int i = n - ;
while ((A[i + ] <= A[i])&&i>=)i--;
if (i<)
{
Reverse(A,,n-);
return false;
}
else
{
int j = i+;
while ((A[j] > A[i])&&j<n)j++;
Swap(A[j-], A[i]);
Reverse(A ,i+ , n-);
return true;
}
}
bool my_prev_permutation(int A[], int n)
{
int i = n - ;
while ((A[i + ] >= A[i])&&i>=)i--;
if (i<)
{
Reverse(A,,n-);
return false;
}
else
{
int j = i+;
while ((A[j] < A[i])&&j<n)j++;
Swap(A[j-], A[i]);
Reverse(A ,i+ , n-);
return true;
}
}
//中介数 递增
int* get_permutation_medium_plus(int A[], int n)//求递增进位制数
{
int* temp = new int[n];
for (int i = ; i < n; i++)
{
temp[n-A[i]] = ;
for (int j = i + ; j <= n - ; j++)
{
if (A[j] < A[i])
{
temp[n-A[i]]++;
}
}
}
return temp;
}
int* get_permutation_plus(int medium[], int n)
{
int* temp = new int[n];
memset(temp, , n * sizeof(int));
for (int i = ; i < n; i++)
{
int empty = -,j=n;//防止末尾已经被占的情况故提前一位
while (empty < medium[i] && j >= )
{
j--;
if (temp[j] <= )
{
empty++;
}
}
temp[j] = n - i;
}
return temp;
}
void next_permutation_increas_medium_plus(int A[],int n)
{
int* mid = get_permutation_medium_plus(A,n);
int last_ = n-;
while(){
if(mid[last_]+<n-last_){
mid[last_]++;
break;
}
mid[last_] = ;
last_--;
}
int* anstm = get_permutation_plus(mid, n);
//Print(anstm,n);
for(int i = ; i < n; i++) A[i] = anstm[i];
return;
}
void prev_permutation_increas_medium_plus(int A[],int n)
{
int* mid = get_permutation_medium_plus(A,n);
int last_ = n-;
while(){
if(mid[last_]->=){
mid[last_]--;
break;
}
mid[last_] = n-last_-;
last_--;
}
int* anstm = get_permutation_plus(mid, n);
//Print(anstm,n);
for(int i = ; i < n; i++) A[i] = anstm[i];
//Print(get_permutation_plus(mid, n),n);
return;
}
//递减
int* get_permutation_medium_sub(int A[], int n)//求递减进位制数
{
int* temp = new int[n];
for (int i = ; i < n; i++)
{
temp[n-A[i]] = ;
for (int j = i + ; j <= n - ; j++)
{
if (A[j] < A[i])
{
temp[n-A[i]]++;
}
}
}
Reverse(temp,,n-);
return temp;
}
int* get_permutation_sub(int medium[], int n)
{
int* temp = new int[n];
memset(temp, , n * sizeof(int));
for (int i = ; i < n; i++)
{
int empty = -, j=n;//防止末尾已经被占的情况故提前一位
while (empty < medium[n-i-] && j >= )
{
j--;
if (temp[j] <= )
{
empty++;
}
}
temp[j] = n - i;
}
return temp;
}
void next_permutation_increas_medium_sub(int A[],int n)
{
int* mid = get_permutation_medium_sub(A,n);
//Print(mid,n);
int last_ = ;
while(){
if(mid[n-last_]+<n-last_+){
mid[n-last_]++;
break;
}
mid[n-last_] = ;
last_++;
}
int* anstm = get_permutation_sub(mid, n);
//Print(mid,n);
for(int i = ; i < n; i++) A[i] = anstm[i];
return;
}
void prev_permutation_increas_medium_sub(int A[],int n)
{
int* mid = get_permutation_medium_sub(A,n);
//Print(mid,n);
int last_ = ;
while(){
if(mid[n-last_]->=){
mid[n-last_]--;
break;
}
mid[n-last_] = n-last_;
last_++;
}
int* anstm = get_permutation_sub(mid, n);
//Print(anstm,n);
for(int i = ; i < n; i++) A[i] = anstm[i];
//Print(get_permutation_plus(mid, n),n);
return;
}
//邻位对换法
int my_find(int A[],int n, int key){
for(int i = ; i < n; i++){
if(A[i]==key) return i;
}
return -;
}
int* get_permutation_medium_neighbor(int A[], int dirct[], int n)//求递减进位制数
{
//dirct[]标记方向,-1向左 1向右
int* temp = new int[n];
temp[] = ;
dirct[] = ;
for(int i = ; i <= n; i++){
int id_ = my_find(A,n,i);
if(i==) dirct[i-] = -;
else if(i%==){
if(temp[i-]%==)dirct[i-] = ;
else dirct[i-]=-;
}
else if(i%==){
if((temp[i-]+temp[i-])%==) dirct[i-] = ;
else dirct[i-]=-;
}
int j = id_;
int bi_ = ;
while(j < n && j>=){
if(A[j]<i) bi_++;
j += -dirct[i-]*;
}
temp[i-] = bi_;
}
return temp;
}
int* get_permutation_neighbor(int medium[], int dirct[] ,int n)
{
int* temp = new int[n];
for(int i = ; i < n; i++) temp[i] = ;
for (int i = ; i < n; i++)
{
if((n-i+)==) dirct[n-i]=-;
else if((n-i+)%==){
if(medium[n-i-]%==) dirct[n-i]=;
else dirct[n-i]=-;
}
else if((n-i+)%==){
if((medium[n-i-] + medium[n-i-])%==) dirct[n-i] = ;
else dirct[n-i] = -;
}
if(dirct[n-i] == -){
int j = n-;int empty = ;
while(empty <= medium[n-i]&&j>=){
if(temp[j]==) empty++;
j--;
}
temp[j+] = n-i+;
}
else{
int j = ;int empty = ;
while(empty <= medium[n-i]&&j<n){
if(temp[j]==) empty++;
j++;
}
temp[j-] = n-i+;
}
//Print(temp,n);
}
//Print(medium,n);
//Print(dirct,n);
return temp;
}
void next_permutation_increas_medium_neighbor(int A[],int dirct[],int n)
{
int* mid = get_permutation_medium_neighbor(A,dirct,n);
//Print(mid,n);
//Print(dirct,n);
int last_ = ;
while(){
if(mid[n-last_]+<n-last_+){
mid[n-last_]++;
break;
}
mid[n-last_] = ;
last_++;
}
int* anstm = get_permutation_neighbor(mid,dirct, n);
//Print(mid,n);
for(int i = ; i < n; i++) A[i] = anstm[i];
return;
}
void prev_permutation_increas_medium_neighbor(int A[],int dirct[],int n)
{
int* mid = get_permutation_medium_neighbor(A,dirct,n);
//Print(mid,n);
int last_ = ;
while(){
if(mid[n-last_]->=){
mid[n-last_]--;
break;
}
mid[n-last_] = n-last_;
last_++;
}
int* anstm = get_permutation_neighbor(mid, dirct,n);
//Print(anstm,n);
for(int i = ; i < n; i++) A[i] = anstm[i];
//Print(get_permutation_plus(mid, n),n);
return;
} int main()
{
int n,type,k;
while(~scanf("%d%d%d",&n,&type,&k)){
int tm[n];
for(int i = ; i < n; i++){
scanf("%d",&tm[i]);
}
if(type==){
if(k>=){
while(k--) next_permutation(tm,tm+n);
Print(tm,n);
}
else{
k = -k;
while(k--) prev_permutation(tm,tm+n);
Print(tm,n);
}
/*下面自己写的实现竟然超时了,气人
if(k>=0){
while(k--) my_next_permutation(tm,n);
Print(tm,n);
}
else{
k = -k;
while(k--) my_prev_permutation(tm,n);
Print(tm,n);
}
*/
}
else if(type==){
if(k>=){
while(k--) next_permutation_increas_medium_plus(tm,n);
Print(tm,n);
}
else{
k = -k;
while(k--) prev_permutation_increas_medium_plus(tm,n);
Print(tm,n);
}
}
else if(type==){
if(k>=){
while(k--) next_permutation_increas_medium_sub(tm,n);
Print(tm,n);
}
else{
k = -k;
while(k--) prev_permutation_increas_medium_sub(tm,n);
Print(tm,n);
}
}
else{
int dict[n];
if(k>=){
while(k--) next_permutation_increas_medium_neighbor(tm,dict,n);
Print(tm,n);
}
else{
k = -k;
while(k--) prev_permutation_increas_medium_neighbor(tm,dict,n);
Print(tm,n);
}
}
}
return ;
}
 #include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<cmath> using namespace std;
void Swap(int &a, int &b)
{
a == b ? : a ^= b ^= a ^= b;
}
void Print(int A[],int n)
{
for (int i = ; i < n; i++)
{
printf("%d%c", A[i], i == n - ? '\n' : ' ');
}
}
//阶乘
int factorial(int x) { return x > ? x*factorial(x - ) : ; }
//字典序法
void Reverse(int A[],int a,int b)//反转
{
while (a < b)
{
Swap(A[a], A[b]);
a++;
b--;
}
}
bool my_next_permutation(int A[], int n)
{
int i = n - ;
while ((A[i + ] <= A[i])&&i>=)i--;
if (i<)
{
Reverse(A,,n-);
return false;
}
else
{
int j = i+;
while ((A[j] > A[i])&&j<n)j++;
Swap(A[j-], A[i]);
Reverse(A ,i+ , n-);
return true;
}
}
bool my_prev_permutation(int A[], int n)
{
int i = n - ;
while ((A[i + ] >= A[i])&&i>=)i--;
if (i<)
{
Reverse(A,,n-);
return false;
}
else
{
int j = i+;
while ((A[j] < A[i])&&j<n)j++;
Swap(A[j-], A[i]);
Reverse(A ,i+ , n-);
return true;
}
}
//用中介数求字典序法 //中介数 递增
int* get_permutation_medium_plus(int A[], int n)//求递增进位制数
{
int* temp = new int[n];
for (int i = ; i < n; i++)
{
temp[n-A[i]] = ;
for (int j = i + ; j <= n - ; j++)
{
if (A[j] < A[i])
{
temp[n-A[i]]++;
}
}
}
return temp;
}
int* get_permutation_plus(int medium[], int n)
{
int* temp = new int[n];
memset(temp, , n * sizeof(int));
for (int i = ; i < n; i++)
{
int empty = -,j=n;//防止末尾已经被占的情况故提前一位
while (empty < medium[i] && j >= )
{
j--;
if (temp[j] <= )
{
empty++;
}
}
temp[j] = n - i;
}
return temp;
}
void next_permutation_increas_medium_plus(int A[],int n,int k)
{
int* mid = get_permutation_medium_plus(A,n);
int last_ = n-;
while(){
if(mid[last_]+k<n-last_){
mid[last_] += k;
break;
}
int dim = (mid[last_]+k);
mid[last_] = (mid[last_]+k)%(n-last_);
k = dim/(n-last_);
last_--;
}
int* anstm = get_permutation_plus(mid, n);
//Print(anstm,n);
for(int i = ; i < n; i++) A[i] = anstm[i];
return;
} void prev_permutation_increas_medium_plus(int A[],int n,int k)
{
int* mid = get_permutation_medium_plus(A,n);
int last_ = n-;
while(){
if(mid[last_]-k>=){
mid[last_]-=k;
break;
}
int dim = (k-mid[last_]);
int c = ceil(double(dim)/(n-last_));
mid[last_] = c*(n-last_)-(k-mid[last_]);
k = c;
last_--;
}
int* anstm = get_permutation_plus(mid, n);
//Print(anstm,n);
for(int i = ; i < n; i++) A[i] = anstm[i];
//Print(get_permutation_plus(mid, n),n);
return;
} //递减
int* get_permutation_medium_sub(int A[], int n)//求递减进位制数
{
int* temp = new int[n];
for (int i = ; i < n; i++)
{
temp[n-A[i]] = ;
for (int j = i + ; j <= n - ; j++)
{
if (A[j] < A[i])
{
temp[n-A[i]]++;
}
}
}
Reverse(temp,,n-);
return temp;
}
int* get_permutation_sub(int medium[], int n)
{
int* temp = new int[n];
memset(temp, , n * sizeof(int));
for (int i = ; i < n; i++)
{
int empty = -, j=n;//防止末尾已经被占的情况故提前一位
while (empty < medium[n-i-] && j >= )
{
j--;
if (temp[j] <= )
{
empty++;
}
}
temp[j] = n - i;
}
return temp;
}
void next_permutation_increas_medium_sub(int A[],int n,int k)
{
int* mid = get_permutation_medium_sub(A,n);
//Print(mid,n);
int last_ = ;
while(){
if(mid[n-last_]+k<n-last_+){
mid[n-last_] += k;
break;
}
int dim = (mid[n-last_]+k);
mid[n-last_] = (mid[n-last_]+k)%(n-last_+);
k = dim/(n-last_+);
last_++;
}
int* anstm = get_permutation_sub(mid, n);
//Print(mid,n);
for(int i = ; i < n; i++) A[i] = anstm[i];
return;
}
void prev_permutation_increas_medium_sub(int A[],int n,int k)
{
int* mid = get_permutation_medium_sub(A,n);
//Print(mid,n);
int last_ = ;
while(){
if(mid[n-last_]-k>=){
mid[n-last_]-=k;
break;
}
int dim = (k-mid[n-last_]);
int c = ceil(double(dim)/(n-last_+));
mid[n-last_] = c*(n-last_+)-(k-mid[n-last_]);
k = c;
last_++;
}
int* anstm = get_permutation_sub(mid, n);
//Print(anstm,n);
for(int i = ; i < n; i++) A[i] = anstm[i];
//Print(get_permutation_plus(mid, n),n);
return;
}
//邻位对换法
int my_find(int A[],int n, int key){
for(int i = ; i < n; i++){
if(A[i]==key) return i;
}
return -;
}
int* get_permutation_medium_neighbor(int A[], int dirct[], int n)//求递减进位制数
{
//dirct[]标记方向,-1向左 1向右
int* temp = new int[n];
temp[] = ;
dirct[] = ;
for(int i = ; i <= n; i++){
int id_ = my_find(A,n,i);
if(i==) dirct[i-] = -;
else if(i%==){
if(temp[i-]%==)dirct[i-] = ;
else dirct[i-]=-;
}
else if(i%==){
if((temp[i-]+temp[i-])%==) dirct[i-] = ;
else dirct[i-]=-;
}
int j = id_;
int bi_ = ;
while(j < n && j>=){
if(A[j]<i) bi_++;
j += -dirct[i-]*;
}
temp[i-] = bi_;
}
return temp;
}
int* get_permutation_neighbor(int medium[], int dirct[] ,int n)
{
int* temp = new int[n];
for(int i = ; i < n; i++) temp[i] = ;
for (int i = ; i < n; i++)
{
if((n-i+)==) dirct[n-i]=-;
else if((n-i+)%==){
if(medium[n-i-]%==) dirct[n-i]=;
else dirct[n-i]=-;
}
else if((n-i+)%==){
if((medium[n-i-] + medium[n-i-])%==) dirct[n-i] = ;
else dirct[n-i] = -;
}
if(dirct[n-i] == -){
int j = n-;int empty = ;
while(empty <= medium[n-i]&&j>=){
if(temp[j]==) empty++;
j--;
}
temp[j+] = n-i+;
}
else{
int j = ;int empty = ;
while(empty <= medium[n-i]&&j<n){
if(temp[j]==) empty++;
j++;
}
temp[j-] = n-i+;
}
//Print(temp,n);
}
//Print(medium,n);
//Print(dirct,n);
return temp;
}
void next_permutation_increas_medium_neighbor(int A[],int dirct[],int n,int k)
{
int* mid = get_permutation_medium_neighbor(A,dirct,n);
//Print(mid,n);
//Print(dirct,n);
int last_ = ;
while(){
if(mid[n-last_]+k<n-last_+){
mid[n-last_] += k;
break;
}
int dim = (mid[n-last_]+k);
mid[n-last_] = (mid[n-last_]+k)%(n-last_+);
k = dim/(n-last_+);
last_++;
}
int* anstm = get_permutation_neighbor(mid,dirct, n);
//Print(mid,n);
for(int i = ; i < n; i++) A[i] = anstm[i];
return;
}
void prev_permutation_increas_medium_neighbor(int A[],int dirct[],int n,int k)
{
int* mid = get_permutation_medium_neighbor(A,dirct,n);
//Print(mid,n);
int last_ = ;
while(){
if(mid[n-last_]-k>=){
mid[n-last_]-=k;
break;
}
int dim = (k-mid[n-last_]);
int c = ceil(double(dim)/(n-last_+));
mid[n-last_] = c*(n-last_+)-(k-mid[n-last_]);
k = c;
last_++;
}
int* anstm = get_permutation_neighbor(mid, dirct,n);
//Print(anstm,n);
for(int i = ; i < n; i++) A[i] = anstm[i];
//Print(get_permutation_plus(mid, n),n);
return;
} int main()
{
int n,type,k;
while(~scanf("%d%d%d",&n,&type,&k)){
int tm[n];
for(int i = ; i < n; i++){
scanf("%d",&tm[i]);
} if(type==){
/* if(k>=0){
my_next_permutation(tm,n,k);
Print(tm,n);
}
else{
k = -k;
my_prev_permutation(tm,n,k);
Print(tm,n);
}
下面自己写的实现竟然超时了,气人
if(k>=0){
while(k--) my_next_permutation(tm,n);
Print(tm,n);
}
else{
k = -k;
while(k--) my_prev_permutation(tm,n);
Print(tm,n);
}*/ } else if(type==){
if(k>=){
next_permutation_increas_medium_plus(tm,n,k);
Print(tm,n);
}
else{
k = -k;
prev_permutation_increas_medium_plus(tm,n,k);
Print(tm,n);
} }
else if(type==){
if(k>=){
next_permutation_increas_medium_sub(tm,n,k);
Print(tm,n);
}
else{
k = -k;
prev_permutation_increas_medium_sub(tm,n,k);
Print(tm,n);
}
}
else{
int dict[n];
if(k>=){
next_permutation_increas_medium_neighbor(tm,dict,n,k);
Print(tm,n);
}
else{
k = -k;
prev_permutation_increas_medium_neighbor(tm,dict,n,k);
Print(tm,n);
}
}
}
return ;
}

C++ 修改版

 #!/usr/bin/env python
# coding: utf-8 # In[2]: def dictionary(a,b):
zhongjie=[]
for i in range(len(b)-1):
sum0=0
for j in b[(i+1):]:
if j<b[i]:
sum0+=1
zhongjie.append(sum0)
xushu=zhongjie_to_ten(zhongjie)+a[2]
zhongjie=ten_to_zhongjie(a[0],xushu)
result=zhongjie_to_pailie(zhongjie)
return result # In[3]: def increase(a,b):
temp=inc_paixu_zhongjie(b)
temp=zhongjie_to_ten(temp)+a[2]
temp=ten_to_zhongjie(a[0],temp)
result=inc_zhongjie_paixu(temp)
result.reverse()
return result # In[4]: def decrease(a,b):
zhongjie=inc_paixu_zhongjie(b)
zhongjie.reverse()
xuhao=int(dec_zhongjie_ten(zhongjie))
xuhao+=int(a[2])
zhongjie=dec_ten_zhongjie(a[0],xuhao)
zhongjie.reverse()
result=inc_zhongjie_paixu(zhongjie)
result.reverse()
return result # In[30]: def exchange(a,b):
zhongjie=exc_paixu_zhongjie(b)
xuhao=int(dec_zhongjie_ten(zhongjie))
xuhao1=int(a[2])+xuhao
zhongjie=dec_ten_zhongjie(a[0],xuhao1)
result=exc_zhongjie_paixu(zhongjie)
return result # In[6]: ##exchange
def exc_paixu_zhongjie(b):
direction=[0]*len(b)
zhongjie=[0]*(len(b)-1)
##向左为0,向右为1
sum0=0
for i in b[(b.index(2)):]:
if i<2:
sum0+=1
zhongjie[0]=sum0
for i in range(3,len(b),2):
##奇数位
direction[b.index(i)]=zhongjie[i-3]%2
if direction[b.index(i)]:
sum0=0
for j in b[0:(b.index(i))]:
if j<i:
sum0+=1
zhongjie[i-2]=sum0
else:
sum0=0
for j in b[(b.index(i)):]:
if j<i:
sum0+=1
zhongjie[i-2]=sum0
##偶数位
direction[b.index(i+1)]=(zhongjie[i-3]+zhongjie[i-2])%2
if direction[b.index(i+1)]:
sum0=0
for j in b[0:(b.index(i+1))]:
if j<(i+1):
sum0+=1
zhongjie[i-1]=sum0
else:
sum0=0
for j in b[(b.index(i+1)):]:
if j<(i+1):
sum0+=1
zhongjie[i-1]=sum0
if len(b)%2==1:
direction[b.index(len(b))]=zhongjie[len(b)-3]%2
if direction[b.index(len(b))]:
sum0=0
for j in b[0:(b.index(len(b)))]:
if j<len(b):
sum0+=1
zhongjie[len(b)-2]=sum0
else:
sum0=0
for j in b[(b.index(len(b))):]:
if j<len(b):
sum0+=1
zhongjie[len(b)-2]=sum0
return zhongjie def exc_zhongjie_paixu(zhongjie):
paixu=[1]*(len(zhongjie)+1)
for i in range(len(paixu),2,-1):
if i%2==1:##奇数
if zhongjie[i-3]%2==1:##向右 sum0=0
for j in range(len(paixu)):
sum0+=(paixu[j]==1)
if sum0==zhongjie[i-2]+1:
paixu[j]=i
break
else:#向左
paixu.reverse()
sum0=0
for j in range(len(paixu)):
sum0+=(paixu[j]==1)
if sum0==zhongjie[i-2]+1:
paixu[j]=i
paixu.reverse()
break else:#偶数
if (zhongjie[i-4]+zhongjie[i-3])%2==1:##向右
sum0=0
for j in range(len(paixu)):
sum0+=(paixu[j]==1)
if sum0==zhongjie[i-2]+1:
paixu[j]=i
break
else:#向左
paixu.reverse()
sum0=0
for j in range(len(paixu)):
sum0+=(paixu[j]==1)
if sum0==zhongjie[i-2]+1:
paixu[j]=i
paixu.reverse()
break
if zhongjie[0]==0:
paixu.reverse()
paixu[paixu.index(1)]=2
if zhongjie[0]==0:
paixu.reverse()
return paixu # In[58]: ###decrease###
def dec_zhongjie_ten(zhongjie):
sum0=0
for i in range(len(zhongjie)):
sum0+=zhongjie[i]*int(jiecheng(len(zhongjie)+1))//int(jiecheng(i+2))
return sum0
def idec_zhongjie_ten(zhongjie):
sum0=zhongjie[0]
for i in range(3,len(zhongjie)+2):
sum0=sum0*i+zhongjie[i-2]
return sum0
def dec_ten_zhongjie(n,xuhao):
abs_n=int(xuhao)
out=[0]*(n-1)
for i in range(n,1,-1):
out[i-2]=abs_n%i
abs_n=abs_n//i
return out # In[8]: ###increase###
def inc_paixu_zhongjie(b):##增序排序到中介数
result=[]
for i in range(len(b),1,-1):
sum0=0
for j in b[(b.index(i)):]:
if j<i:
sum0+=1
result.append(sum0)
return result
def error(zhongjie):
paixu=[1]*(len(zhongjie)+1)##逆序数,最后倒置
for i in range(len(zhongjie)):
sum=0
for j in range(len(paixu)):
if paixu[j]==1:
sum+=1
if sum==zhongjie[i] and paixu[j+1]==1:
paixu[j+1]=len(zhongjie)+1-i
break
if zhongjie[i]==0 and paixu[j]==1:
paixu[j]=len(zhongjie)+1-i
break
paixu.reverse()
return paixu
def inc_zhongjie_paixu(zhongjie):
paixu=[1]*(len(zhongjie)+1)
for i in range(len(zhongjie)):
sum0=0
for j in range(len(paixu)):
if paixu[j]==1:
if sum0==zhongjie[i]:
paixu[j]=len(paixu)-i
break
else:
sum0+=1
paixu.reverse
return paixu # In[32]: #######字典序#######
def jiecheng(n):##阶乘
if n==0 or n==1 or n>20:
return 1
else:
return int(n*jiecheng(n-1))
def ten_to_zhongjie(n,ten):##十进制转换中介数
abs_n=abs(ten)
out=[]
for i in range(n-1,0,-1):
zheng=abs_n//jiecheng(i)
abs_n=abs_n%jiecheng(i)
out.append(zheng)
return out
def zhongjie_to_ten(zhongjie):##中介数转换10进制
sum=0
for i in range(len(zhongjie)):
sum=sum+zhongjie[-(i+1)]*jiecheng(i+1)
return sum
def zhongjie_to_pailie(zhongjie):##中介数转排列
pailie=[]
for i in range(len(zhongjie)):
temp=sorted(pailie)
pailie_new=zhongjie[i]+1
for j in temp:
if j<=pailie_new:
pailie_new=pailie_new+1
pailie.append(pailie_new)
for i in range(len(pailie)+1):
if (i+1) not in pailie:
pailie.append(i+1)
return pailie # In[33]: #a=input()
#b=input()
##初始化
#a=a.split()
#b=b.split()
#for i in range(len(a)):
# a[i]=int(a[i])
#for i in range(a[0]):
# b[i]=int(b[i])
a=list(map(int,input().split(" ")[:3]))
b=list(map(int,input().split(" ")[:a[0]]))
##分类运算
if a[1]==1:
output=dictionary(a,b)
elif a[1]==2:
output=increase(a,b)
elif a[1]==3:
output=decrease(a,b)
elif a[1]==4:
output=exchange(a,b)
else :
output=b #输出
for i in range(a[0]):
output[i]=str(output[i])
print(" ".join(output)) # In[ ]:

ACM 求全排列(字典序、邻位对换、递增进位制数,递减进位制数)的更多相关文章

  1. 求全排列Permutation

    是在教材(<计算机算法设计与分析(第4版)>王晓东 编著)上看见的关于求全排列的算法: 我们可以看一下书上怎么写的: #include<bits/stdc++.h> using ...

  2. PermutationsUnique,求全排列,去重

    问题描述:给定一个数组,数组里面有重复元素,求全排列. 算法分析:和上一道题一样,只不过要去重. import java.util.ArrayList; import java.util.HashSe ...

  3. next_permutation()函数 和 prev_permutation() 按字典序求全排列

    next_permutation功能:    求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm> 与之完全相反的函数还有prev_permutation 这个 ...

  4. nyoj-366-D的小L(求全排列)

    D的小L 时间限制:4000 ms  |  内存限制:65535 KB 难度:2 描述       一天TC的匡匡找ACM的小L玩三国杀,但是这会小L忙着哩,不想和匡匡玩但又怕匡匡生气,这时小L给匡匡 ...

  5. [Swust OJ 541]--排列字典序问题

    题目链接:http://acm.swust.edu.cn/problem/0541/ Time limit(ms): 2000 Memory limit(kb): 65535 n个元素{1,2,... ...

  6. ACM 擅长排列的小明

    擅长排列的小明 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 小明十分聪明,而且十分擅长排列计算.比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想 ...

  7. LeetCode:Permutations, Permutations II(求全排列)

    Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...

  8. LeetCode46 回溯算法求全排列,这次是真全排列

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode的26篇文章,我们来实战一下全排列问题. 在之前的文章当中,我们讲过八皇后.回溯法,也提到了全排列,但是毕竟没有真正写 ...

  9. leetcode31 下一个排列 字典序

    数字的字典序就是比大小 先想几个例子  123_>132  1243-> 1324 ,12453-> 12534 1.不可排的序列就是降序序列 2.两个相同长度的串比大小,前面相同, ...

随机推荐

  1. 强化学习之MDP

    前言 最近又入坑RL了,要搞AutoML就要学会RL,真的是心累.. 正文 MDP里面比较重要的就是状态值函数和动作-状态值函数吧,然后再求最优状态值函数和最优动作状态值函数,状态值函数的公式推导一开 ...

  2. linux/work

    0.切换用户 //默认root用户是无固定密码的,并且是被锁定的,如果想给root设置一个密码 sudo passwd root //输入密码 & 确认密码 //切换root用户 su roo ...

  3. Mysql函数----控制流函数介绍

    MySQL有4个函数用来进行条件操作的,可以实现SQL的条件逻辑,允许开发者将一些应用程序业务逻辑转换到数据库后台.   MySQL控制流函数: 1.CASE WHEN[test1] THEN [re ...

  4. HashMap底层为什么一定用数组

    HashMap源码数据结构: Entry[] table = new Entry[capacity]; 其中,Entry就是一个链表节点.如果将数组替换成LinkedList是否可行?如下: List ...

  5. 使用foreach一次性添加多个单选按钮

    <?php //这个函数创建一个单选按钮 //函数接收一个论据:值 //函数也会让按钮有"黏性"-->记住你是如何填写它的. function create_radio ...

  6. [Codeforces 1246B] Power Products (STL+分解质因数)

    [Codeforces 1246B] Power Products (STL+分解质因数) 题面 给出一个长度为\(n\)的序列\(a_i\)和常数k,求有多少个数对\((i,j)\)满足\(a_i ...

  7. codeforces 597 div 2

    A 题意: 有无限个方块,上面分别是0,1,2......若方块i可以被表示为ax+by(x,y>0),则方块为白色,否则为黑色,给出a,b.问黑方块是否有无限个. 分析: 1:若(a,b)=1 ...

  8. linux下occi操作oracle数据库,中文乱码的问题

    转载:http://www.linuxidc.com/Linux/2008-02/11238.htm 前几日调通了OCI连接数据库的问题后,用Oracle自带的例子测试了一下,能正常读取数据(都是英文 ...

  9. 如何在centos6和centos7上部署nfs共享服务器和客户端

    nfs共享服务为中小型企业在存储上提供了有效的节省空间,许多大型的网站也在使用nfs,如百度和阿里等,下面结合自己所学的知识,阐述如何在centos6和centos7下配置nfs.注:除了必要的说明外 ...

  10. Python 数据分析:Pandas 缺省值的判断

    Python 数据分析:Pandas 缺省值的判断 背景 我们从数据库中取出数据存入 Pandas None 转换成 NaN 或 NaT.但是,我们将 Pandas 数据写入数据库时又需要转换成 No ...