题目:给定一个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 ;
}

[hdu5225]逆序对统计的更多相关文章

  1. 51nod1779 逆序对统计

    1779 逆序对统计 基准时间限制:1 秒 空间限制:131072 KB  lyk最近计划按顺序做n道题目,每道题目都分为很多分数档次,lyk觉得这些题太简单了,于是它想到了一个好玩的游戏. lyk决 ...

  2. UVA 11858 Frosh Week 逆序对统计

    题目链接: http://acm.hust.edu.cn/vjudge/contest/122094#problem/H Frosh Week Time Limit:8000MSMemory Limi ...

  3. 51nod 1779逆序对统计(状压DP)

    按照插入数的大小排序, 然后依次进行dp. 用一个状态表示n个数是否被选了 10110 就是表示第1.3.4个位置都选了 那么如果此时这个数该填到5这个位置,那么必定会造成一个逆序(因为下一个数会填到 ...

  4. SPOJ COWPIC(逆序对变形题)

    SPOJ COWPIC 题目链接 题意:一个序列,相邻能够交换.问最少交换几次使得变成循环的1-n的当中一种 思路:对于原来正常的变换成1-n而言,答案就是逆序对了,而多了这么一个变形,事实上仅仅须要 ...

  5. 逆序对 -- cogs1438 火柴排队

    题目链接:http://cogs.pro:8081/cogs/problem/problem.php?pid=vimiQkqjU [题目描述] 样例一输入: 4 2 3 1 4 3 2 1 4 样例二 ...

  6. 【CQOI2011】动态逆序对 BZOJ3295

    Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计 ...

  7. BZOJ 3295: [Cqoi2011]动态逆序对

    3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3865  Solved: 1298[Submit][Sta ...

  8. BZOJ 3295 【Cqoi2011】 动态逆序对

    Description 对于序列\(A\),它的逆序对数定义为满足\(i<j\),且\(A_i>A_j\)的数对\((i,j)\)的个数.给\(1\)到\(n\)的一个排列,按照某种顺序依 ...

  9. 【bzoj3295】 Cqoi2011—动态逆序对

    http://www.lydsy.com/JudgeOnline/problem.php?id=3295 (题目链接) 题意 给出某种排列,按照某种顺序依次删除m个数,在每次删除一个数前统计序列中逆序 ...

随机推荐

  1. CoreDNS解析异常记录

    CoreDNS解析异常记录 异常情况:集群是用kubespray部署的4个worknode,coredns默认部署2个deployment.今天发现部署了coredns的node上的pod正常解析内部 ...

  2. 基于 HTML5 WebGL 的高炉炼铁厂可视化系统

    前言       在当今 工业4.0 新时代的推动下,不仅迎来了 工业互联网 的发展,还开启了 5G 时代的新次元.而伴随着带宽的提升,网络信息飞速发展,能源管控上与实时预警在工业互联网中也占着举足轻 ...

  3. Spring Cloud 系列之 Gateway 服务网关(四)

    本篇文章为系列文章,未读第一集的同学请猛戳这里: Spring Cloud 系列之 Gateway 服务网关(一) Spring Cloud 系列之 Gateway 服务网关(二) Spring Cl ...

  4. 操作google_sheets

    起源:最近了使用flask和bootstrap写了测试小工具,数据全部使用excel存储,部署到测试环境. 问题:每次每个人在使用excel数据时都需要重新编辑好的excel通过upload按钮传到服 ...

  5. 新手想掌握Python技能需要众多Python项目练习,适合项目有哪些?

    适合新手练习的Python项目有哪些?简单易上手的Python项目汇总:Web 项目设计:内容聚合器.正则表达式查询工具.网址缩短.便利贴.功能.测验.GUI 项目设计:MP3 播放器.闹铃提醒工具. ...

  6. Java中集合概念

    集合的由来: 我们学习的是面向对象语言,而面向对象语言对事物的描述是通过对象体现的,为了方便对多个对象进行操作,我们就必须把这多个对象进行储存,而想要储存多个对象,就不能是一个基本的变量,而应该是一个 ...

  7. Python代码覆盖率分析工具Coverage

    简介 在测试中,为了度量产品质量,代码覆盖率被作为一种测试结果的评判依据,在Python代码中用来分析代码覆盖率的工具当属Coverage.代码覆盖率是由特定的测试套件覆盖被测源代码的程度来度量,Co ...

  8. Kubernetes笔记(一):十分钟部署一套K8s环境

    Kubernetes是Goole开源的一个容器编排引擎,它支持自动化部署.大规模可伸缩.应用容器化管理 -- 百度百科. 接触K8s也有半年多了,也基于阿里云平台搭建了包含多级服务.目前运行较为稳定的 ...

  9. Python selenium Chrome正在受到自动软件的控制 disable-infobars无效 的解决方法

    问题解决 前两天更新了google浏览器版本,今天运行以前的脚本,发现options一个参数的配置不生效了. 运行了几次都发现该参数没有生效,也检查了自己的代码参数,没有写错,于是就有了这一波“网中寻 ...

  10. phpcms 用phpexcel导入导出excel

    html <form method="post" action="?m=content&c=content&a=public_add_excel&q ...