AtCoder Beginner Contest 150 C-Count Order (next_permutation排列函数的应用)

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

葛济维的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值