题目:给定一个1到n的排列,求字典序小于这个排列的所有排列的逆序对数之和。

思路:既然是求字典序小于这个排列的,不妨将排列根据和它前k位相同来分类,然后枚举第k+1位的数(小于原序列第k+1位的数),假设逆序对的位置为(x,y),对于1<=x<k+1,x<y<=k+1和1<=x<=k+1,k+2<=y<=n的答案是容易计算出来的,对于k+2<=x<n,x<y<=n的答案则可以通过dp来计算由于剩余的数已没有大小意义了,假设剩余p个不同的数,则p个数的全排列产生的逆序对总数与1-p这p个数产生的全排列的逆序对总数是相同的,所以可以令dp[n]表示n个数产生的全排列的逆序对总数,则dp[n] =dp[n-1]*n+C(n,2)*(n-1)!。

 #pragma comment(linker, "/STACK:10240000,10240000")

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <ctime>
#include <cctype>
#include <set>
#include <bitset>
#include <functional>
#include <numeric>
#include <stdexcept>
#include <utility> using namespace std; #define mem0(a) memset(a, 0, sizeof(a))
#define mem_1(a) memset(a, -1, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define define_m int m = (l + r) >> 1
#define rep_up0(a, b) for (int a = 0; a < (b); a++)
#define rep_up1(a, b) for (int a = 1; a <= (b); a++)
#define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
#define rep_down1(a, b) for (int a = b; a > 0; a--)
#define all(a) (a).begin(), (a).end()
#define lowbit(x) ((x) & (-(x)))
#define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
#define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
#define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
#define pchr(a) putchar(a)
#define pstr(a) printf("%s", a)
#define sstr(a) scanf("%s", a)
#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d%d", &a, &b)
#define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define pint(a) printf("%d\n", a)
#define test_print1(a) cout << "var1 = " << a << endl
#define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
#define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a) typedef unsigned int uint;
typedef long long LL;
typedef pair<int, int> pii;
typedef vector<int> vi; const int dx[] = {, , -, , , , -, -};
const int dy[] = {-, , , , , -, , - };
const int maxn = 1e8 + ;
const int md = 1e9 + ;
const int inf = 1e9 + ;
const LL inf_L = 1e18 + ;
const double pi = acos(-1.0);
const double eps = 1e-; template<class T>T gcd(T a, T b){return b==?a:gcd(b,a%b);}
template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
template<class T>T condition(bool f, T a, T b){return f?a:b;}
template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
int make_id(int x, int y, int n) { return x * n + y; } template<int mod>
struct ModInt {
const static int MD = mod;
int x;
ModInt(int x = ): x(x) { if (x < ) x += mod; }
int get() { return x; } ModInt operator + (const ModInt &that) const { int x0 = x + that.x; return ModInt(x0 < MD? x0 : x0 - MD); }
ModInt operator - (const ModInt &that) const { int x0 = x - that.x; return ModInt(x0 < MD? x0 + MD : x0); }
ModInt operator * (const ModInt &that) const { return ModInt((long long)x * that.x % MD); }
ModInt operator / (const ModInt &that) const { return *this * that.inverse(); } ModInt operator += (const ModInt &that) { x += that.x; if (x >= MD) x -= MD; }
ModInt operator -= (const ModInt &that) { x -= that.x; if (x < ) x += MD; }
ModInt operator *= (const ModInt &that) { x = (long long)x * that.x % MD; }
ModInt operator /= (const ModInt &that) { *this = *this / that; } ModInt inverse() const {
int a = x, b = MD, u = , v = ;
while(b) {
int t = a / b;
a -= t * b; std::swap(a, b);
u -= t * v; std::swap(u, v);
}
if(u < ) u += MD;
return u;
} };
typedef ModInt<md> mint;
mint dp[], fact[];
int n, a[];
bool vis[]; void init() {
fact[] = ;
rep_up1(i, ) {
dp[i] = dp[i - ] * i + fact[i - ] * i * (i - ) / ;
fact[i] = fact[i - ] * i;
}
} int main() {
//freopen("in.txt", "r", stdin);
init();
while (cin >> n) {
rep_up0(i, n) sint(a[i]);
mem0(vis);
mint ans = ;
rep_up0(i, n) {
int c = ;
rep_up0(j, i) {
for (int k = j + ; k < i; k ++) {
if (a[j] > a[k]) c ++;
}
}
rep_up1(j, a[i] - ) {
if (!vis[j]) {
ans += fact[n - i - ] * c;
rep_up0(k, i) {
if (a[k] > j) ans += fact[n - i - ];
}
int sum = ;
vis[j] = true;
rep_up1(k, n) {
if (!vis[k]) sum ++;
else ans += fact[n - i - ] * sum;
}
ans += dp[n - i - ];
vis[j] =false;
}
}
vis[a[i]] = true;
}
cout << ans.get() << endl;
}
return ;
}

最新文章

  1. awk命令详解
  2. 利用冒泡对List排序
  3. PYTHON发送邮件时,有的服务器不用密码认证的
  4. Android之判断某个服务是否正在运行的方法
  5. (转)介绍几个C#正则表达式工具
  6. Eclipse+超快速的模拟器Genymotion开展Android申请书(第一步:安装和配置Genymotion)
  7. Nvidia TX2 Robot 环境配置记录
  8. 2018-2019-2 20164312 Exp1 PC平台逆向破解
  9. 【LOJ6482】LJJ 爱数数 数论
  10. ansible中的map
  11. prometheus + grafana + node_exporter + alertmanager 的安装部署与邮件报警 (一)
  12. python高级(1)—— 基础回顾1
  13. Cloudflare DNS 域名解析
  14. IDEA常用配置
  15. 性能测试-12.Web页面性能指标与建议
  16. python入门-使用API
  17. Swift简单实现一个常规条款、免责声明文字+带有链接的展示形式
  18. 机器学习数学知识中令人费解的notation符号注解
  19. 微服务深入浅出(11)-- SpringBoot整合Docker
  20. kalilinux、parrotsecos没有声音

热门文章

  1. search(6)- elastic4s-CRUD
  2. Java中常用的七个阻塞队列第二篇DelayQueue源码介绍
  3. Java 多线程--ThreadLocal Timer ExecutorService
  4. shell脚本之六:shell脚本的条件测试与比较
  5. 记一次virtualbox和夜神模拟器冲突的问题
  6. 2019-2020-1 20199310《Linux内核原理与分析》第十一周作业
  7. 创建线程的方式三:实现Callable接口-----JDK5.0 新增
  8. python教程(目录)
  9. 浅析 JS 中的作用域链
  10. UVA10599:Robots(II)(最长上升子序列)