Nyist OJ 117 calculates the number of reverse orders (merge sort & tree array)

Source: Internet
Author: User
Reverse Order Time Limit: 2000 MS | memory limit: 65535 kb difficulty: 5
Description

In an arrangement, if the front and back positions of a pair of numbers are in the opposite order of size, that is, the front number is greater than the back number, they are called a reverse order. The total number of reverse orders in an arrangement is called the number of reverse orders in this arrangement.

Now, we will give you a sequence of n elements. Please determine the number of its reverse orders.

For example, the reverse order of 1 3 2 is 1.

Input
Enter an integer T in the first line to indicate the number of test data groups (1 <= T <= 5)
Each row of test data in each group is an integer N, indicating that there are n elements in the series (2 <= n <= 1000000)
The subsequent line contains N integers (0 <= AI <1000000000), indicating all elements in the series.

Data ensures that, among multiple groups of test data, more than 0.1 million of the test data can only be in one group.
Output
Returns the number of reverse orders of this series.
Sample Input
221 131 3 2
Sample output
01
Source
[Zhang yuncong] original
Uploaded
Zhang yuncong
Calculate the number of reverse orders: It is very efficient to sort by merging; Merge Sorting uses the principle of divide and conquer. First, an array is divided into sequences, and then the sequence is sorted. Then, the sorted sequence is merged into the original array.
# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; const int maxn = 1000001; int A [maxn], B [maxn]; long sum; void merge (INT begin, int mid, int end) // merge {int I = begin, j = Mid + 1, Pos = begin; while (I <= Mid & J <= END) // process of sorting individual SEQUENCES {if (a [I] <= A [J]) {B [POS ++] = A [I ++];} else {B [POS ++] = A [J ++]; sum + = mid-I + 1; // returns the number of reverse orders} while (I <= mid) B [POS ++] = A [I ++]; while (j <= END) B [POS ++] = A [J ++]; for (INT I = begin, j = begin; I <= end; I ++, J ++) A [I] = B [J];} void sort (INT begin, int end) // sort {If (begin <End) {int mid = (begin + end)/2; sort (begin, mid); sort (Mid + 1, end); merge (begin, mid, end );}} int main () {int t, n; scanf ("% d", & T); While (t --) {sum = 0; scanf ("% d ", & N); For (INT I = 1; I <= N; I ++) scanf ("% d", & A [I]); sort (1, n); printf ("% LLD \ n", sum);} return 0 ;}

Calculate the number of reverse orders using a tree array:
# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; const int maxn = 1000005; struct node {int Val, ID;} s [maxn]; int A [maxn], n; int lowbit (int I) // bitwise operation {return I & (-I);} void Update (int I) // update {While (I <= N) {A [I] ++; I + = lowbit (I) ;}} int sum (int I) // sum {int sum = 0; while (I> 0) {sum + = A [I]; I-= lowbit (I);} return sum ;} bool CMP (node X, node y) // comparison function {If (X. val! = Y. val) return X. val <Y. val; return X. ID <Y. ID;} int main () {int T, I; scanf ("% d", & T); While (t --) {scanf ("% d ", & N); for (I = 1; I <= N; I ++) {scanf ("% d", & S [I]. val); s [I]. id = I; A [I] = 0;} long ans = 0; sort (S + 1, S + n + 1, CMP ); // discretization for (I = 1; I <= N; I ++) {Update (s [I]. ID); ans + = I-sum (s [I]. ID);} printf ("% LLD \ n", ANS);} return 0 ;}



Nyist OJ 117 calculates the number of reverse orders (merge sort & tree array)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.