hdu5225---Tom and permutation(规律,枚举+乱搞)

本文介绍了一种计算特定排列下所有小于该排列的字典序排列中逆序数之和的方法,并给出了一段C++实现代码。该问题通过预处理逆序数和阶乘,利用线段树进行区间查询优化输入,最终输出结果模10^9+7。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description
Tom has learned how to calculate the number of inversions in a permutation of n distinct objects by coding, his teacher gives him a problem:
Give you a permutation of n distinct integer from 1 to n, there are many permutations of 1-n is smaller than the given permutation on dictionary order, and for each of them, you can calculate the number of inversions by coding. You need to find out sum total of them.
Tom doesn’t know how to do, he wants you to help him.
Because the number may be very large, output the answer to the problem modulo 109+7.

Input
Multi test cases(about 20). For each case, the first line contains a positive integer n, the second line contains n integers, it’s a permutation of 1-n.
n≤100

Output
For each case, print one line, the answer to the problem modulo 109+7.

Sample Input

3
2 1 3
5
2 1 4 3 5

Sample Output

1
75

Hint
The input may be very big, we might as well optimize input.

Source
BestCoder Round #40

Recommend

首先,n个数的全排列的逆序数有规律,可以递推
然后枚举第k位开始变小,然后后面的就是全排列了
然后考虑k-1位以前的逆序数还有前k-1位对k+1位以后的数产生的逆序数,第k位往前往后分别产生的逆序数,乘上个阶乘

/*************************************************************************
    > File Name: hdu5225.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年05月09日 星期六 21时15分54秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

static const int mod = 1e9 + 7;
LL per_inver[110];
int arr[110];
bool vis[110];
LL sum[110];
LL f[110];
int tree[110];

int lowbit(int x) {
    return x & (-x);
}

void add(int x, int n) {
    for (int i = x; i <= n; i += lowbit(i)) {
        ++tree[i];
    }
}

int getsum(int x) {
    int ans = 0;
    for (int i = x; i; i -= lowbit(i)) {
        ans += tree[i];
    }
    return ans;
}

int main() {
    per_inver[0] = 0;
    per_inver[1] = 0;
    f[0] = 1;
    for (int i = 1; i <= 100; ++i) {
        f[i] = f[i - 1] * i;
        f[i] %= mod;
    }
    LL jiechen = 1;
    for (int i = 2; i <= 100; ++i) {
        LL last = per_inver[i - 1];
        jiechen *= (i - 1);
        jiechen %= mod;
        per_inver[i] = 0;
        for (int j = 0; j < i; ++j) {
            per_inver[i] += last;
            per_inver[i] %= mod;
            last += jiechen;
            last %= mod;
        }
    }
    int n;
    while (~scanf("%d", &n)) {
        for (int i = 1;i <= n; ++i) {
            scanf("%d", &arr[i]);
        }
        memset(tree, 0, sizeof(tree));
        sum[0] = 0;
        for (int i = 1; i <= n; ++i) {
            add(arr[i], n);
            LL cur = i - getsum(arr[i]);
            cur %= mod;
            sum[i] = sum[i - 1] + cur;
            sum[i] %= mod;
        }
        LL ans = 0;
        memset(vis, 0, sizeof(vis));
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j < arr[i]; ++j) { //这一位开始小,前面的都相同,枚举没出现过的
                LL pre = sum[i - 1]; //前面固定的逆序对
                if (!vis[j]) {
                    LL cur = 0, u = 0;
                    for (int k = 1; k < j; ++k) {
                        if (!vis[k]) {
                            ++u;
                        }
                    }
                    for (int k = j + 1; k <= n; ++k) {
                        if (vis[k]) {
                            ++cur;
                        }
                    }
                    int v = 0;
                    for (int k = 1; k < i; ++k) {
                        for (int l = i; l <= n; ++l) {
                            if (!vis[arr[l]] && arr[l] != j) {
                                if (arr[k] > arr[l]) {
                                    ++v;
                                }
                            }
                        }
                    }
                    ans += per_inver[n - i] + (cur + u + v + pre) * f[n - i];
                    ans %= mod;
                }
            }
            vis[arr[i]] = 1;
        }
        printf("%lld\n", ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值