P1908 reverse order to the topic description
Cat Tom and Little mouse Jerry have recently played again, but after all, they are adults, they do not like to play that race each other game, now they like to play statistics. Tom's old cat recently looked up what humans call the "reverse pair", which is defined as: for a given sequence of positive integers, the inverse pair is the ordered pair of Ai>aj and i<j in the sequence. Knowing the concept, they compete on who first calculates the number of reverse pairs in a given sequence of positive integers.
Input/output format
Input Format:
The first line, a number n, indicates that there are n numbers in the sequence.
The second row n number that represents the given sequence.
output Format:
The number of reverse pairs in the given sequence.
Input/Output sample
Input Sample # #:
65 4 2 6 3 1
Sample # # of output:
11
Description
For 50% of data, n≤2500
For 100% of data, n≤40000.
Two ways of writing, on the code.
Tree-like array
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include < algorithm>const int MAXN = 40000 + 10;int data[maxn];int num[maxn];struct t{int rank; int num;} Cnt[maxn];int N;bool CMP (t A, T b) {return a.num > b.num;} inline int lowbit (int k) {return K & ( -1 * k);} inline int Ask (int k) {int x = 0; for (; k;k-= Lowbit (k)) {x + = Data[k]; } return x;} inline void Modify (int k, int num) {for (; k <= n;k + lowbit (k)) {data[k] + = num; }}inline int read () {int x = 0;char ch = getchar (); char c = 1; while (Ch > ' 9 ' | | ch < ' 0 ') c = ch,ch = GetChar (); while (Ch >= ' 0 ' && ch <= ' 9 ') x = x * + ch-' 0 ', ch = getchar (); if (c = = '-') x *=-1; return x;} inline void wri (int k) {if (k) {WRI (K/10); Putchar (K%10); }}inline void Wright (int k) {if (K < 0) Putchar ('-'), WRI ( -1 *k); else WRI (k);} int Ans;int Main () {n = read (); for (int i = 1;i <= n;i + +) {Cnt[i].num = read (); Cnt[i].rank = i; } std::sort (CNT + 1, CNT + 1 + N, CMP); for (int i = 1;i <= n;i + +) {Num[cnt[i].rank] = i; } for (int i = 1;i <= n;i + +) {ans + = ask (Num[i]); Modify (Num[i], 1); } printf ("%d", ans); return 0;}
Merge sort
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include < Algorithm>inline int read () {int x = 0;char ch = getchar (); char c = ch; while (Ch > ' 9 ' | | ch < ' 0 ') c = ch, ch = getchar (); while (Ch <= ' 9 ' && ch >= ' 0 ') x = x * + ch-' 0 ', ch = getchar (); if (c = = '-') x = x *-1; return x;} inline void wri (int k) {if (k) {WRI (K/10); Putchar (k% 10 + ' 0 '); }}inline void Write (int k) {if (K < 0) Putchar ('-'), k =-1 * k; WRI (k);} const int MAXN = 1000000 + 10;int n;int data[maxn];int temp[maxn];int ans;void merge_sort (int x, int y) {if (Y-x < 1) return; int mid =x + (y-x)/2; int p = X,q = mid + 1,i = x; Merge_sort (x, mid); Merge_sort (mid + 1, y); while (P <= Mid | | q <= y) {if ((P <= mid && data[p] <= data[q]) | | q > y) temp[i++] = dat A[p++]; else temp[i++] = Data[q++],ans + = mid-p + 1; } for (int i = x;i<= Y;i + +) data[i] = Temp[i];} int main () {n = read (); for (int i = 1;i <= n;i + +) {Data[i] = read (); } merge_sort (1, N); Write (ANS); return 0;}
"Templates" Reverse squad (tree array/merge sort)