题目:给定一个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)!。
1 #pragma comment(linker, "/STACK:10240000,10240000") 2 3 #include <iostream> 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstdlib> 7 #include <cstring> 8 #include <map> 9 #include <queue> 10 #include <deque> 11 #include <cmath> 12 #include <vector> 13 #include <ctime> 14 #include <cctype> 15 #include <set> 16 #include <bitset> 17 #include <functional> 18 #include <numeric> 19 #include <stdexcept> 20 #include <utility> 21 22 using namespace std; 23 24 #define mem0(a) memset(a, 0, sizeof(a)) 25 #define mem_1(a) memset(a, -1, sizeof(a)) 26 #define lson l, m, rt << 1 27 #define rson m + 1, r, rt << 1 | 1 28 #define define_m int m = (l + r) >> 1 29 #define rep_up0(a, b) for (int a = 0; a < (b); a++) 30 #define rep_up1(a, b) for (int a = 1; a <= (b); a++) 31 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--) 32 #define rep_down1(a, b) for (int a = b; a > 0; a--) 33 #define all(a) (a).begin(), (a).end() 34 #define lowbit(x) ((x) & (-(x))) 35 #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) {} 36 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {} 37 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {} 38 #define pchr(a) putchar(a) 39 #define pstr(a) printf("%s", a) 40 #define sstr(a) scanf("%s", a) 41 #define sint(a) scanf("%d", &a) 42 #define sint2(a, b) scanf("%d%d", &a, &b) 43 #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c) 44 #define pint(a) printf("%d ", a) 45 #define test_print1(a) cout << "var1 = " << a << endl 46 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl 47 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl 48 #define mp(a, b) make_pair(a, b) 49 #define pb(a) push_back(a) 50 51 typedef unsigned int uint; 52 typedef long long LL; 53 typedef pair<int, int> pii; 54 typedef vector<int> vi; 55 56 const int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1}; 57 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 }; 58 const int maxn = 1e8 + 17; 59 const int md = 1e9 + 7; 60 const int inf = 1e9 + 7; 61 const LL inf_L = 1e18 + 7; 62 const double pi = acos(-1.0); 63 const double eps = 1e-6; 64 65 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);} 66 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;} 67 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;} 68 template<class T>T condition(bool f, T a, T b){return f?a:b;} 69 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];} 70 int make_id(int x, int y, int n) { return x * n + y; } 71 72 template<int mod> 73 struct ModInt { 74 const static int MD = mod; 75 int x; 76 ModInt(int x = 0): x(x) { if (x < 0) x += mod; } 77 int get() { return x; } 78 79 ModInt operator + (const ModInt &that) const { int x0 = x + that.x; return ModInt(x0 < MD? x0 : x0 - MD); } 80 ModInt operator - (const ModInt &that) const { int x0 = x - that.x; return ModInt(x0 < MD? x0 + MD : x0); } 81 ModInt operator * (const ModInt &that) const { return ModInt((long long)x * that.x % MD); } 82 ModInt operator / (const ModInt &that) const { return *this * that.inverse(); } 83 84 ModInt operator += (const ModInt &that) { x += that.x; if (x >= MD) x -= MD; } 85 ModInt operator -= (const ModInt &that) { x -= that.x; if (x < 0) x += MD; } 86 ModInt operator *= (const ModInt &that) { x = (long long)x * that.x % MD; } 87 ModInt operator /= (const ModInt &that) { *this = *this / that; } 88 89 ModInt inverse() const { 90 int a = x, b = MD, u = 1, v = 0; 91 while(b) { 92 int t = a / b; 93 a -= t * b; std::swap(a, b); 94 u -= t * v; std::swap(u, v); 95 } 96 if(u < 0) u += MD; 97 return u; 98 } 99 100 }; 101 typedef ModInt<md> mint; 102 mint dp[107], fact[107]; 103 int n, a[107]; 104 bool vis[107]; 105 106 void init() { 107 fact[0] = 1; 108 rep_up1(i, 102) { 109 dp[i] = dp[i - 1] * i + fact[i - 1] * i * (i - 1) / 2; 110 fact[i] = fact[i - 1] * i; 111 } 112 } 113 114 int main() { 115 //freopen("in.txt", "r", stdin); 116 init(); 117 while (cin >> n) { 118 rep_up0(i, n) sint(a[i]); 119 mem0(vis); 120 mint ans = 0; 121 rep_up0(i, n) { 122 int c = 0; 123 rep_up0(j, i) { 124 for (int k = j + 1; k < i; k ++) { 125 if (a[j] > a[k]) c ++; 126 } 127 } 128 rep_up1(j, a[i] - 1) { 129 if (!vis[j]) { 130 ans += fact[n - i - 1] * c; 131 rep_up0(k, i) { 132 if (a[k] > j) ans += fact[n - i - 1]; 133 } 134 int sum = 0; 135 vis[j] = true; 136 rep_up1(k, n) { 137 if (!vis[k]) sum ++; 138 else ans += fact[n - i - 1] * sum; 139 } 140 ans += dp[n - i - 1]; 141 vis[j] =false; 142 } 143 } 144 vis[a[i]] = true; 145 } 146 cout << ans.get() << endl; 147 } 148 return 0; 149 }