洛谷-P2524 Uim的情人节礼物·其之弐

洛谷-P2524 Uim的情人节礼物·其之弐

原题链接:https://www.luogu.com.cn/problem/P2524


题目描述

前传:详见洛谷P2525

Uim成功地按照顺序将礼物送到了N个妹子的手里并维持她们的和谐。

Uim现在想知道,他最终选择的顺序是所有给N个妹子送礼顺序中、字典序第几小的。

输入格式

第一行一个整数N,表示有N个数。

第二行一个整数X,表示给出的排列。

输出格式

一个整数,表示是第几小的字典序。

输入输出样例

输入 #1

3
231

输出 #1

4

说明/提示

1<=N<=9

输入的排列没有空格

C++代码

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int n, ans = 1;
    cin >> n;
    int a[n];
    char c;
    for (int i=0; i<n; ++i) {
        cin >> c;
        a[i] = c - '0';
    }
    while (prev_permutation(a, a+n))
        ++ans;
    cout << ans << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/yuzec/p/14312250.html