Nyist oj 117 calculates the number of reverse orders (merge sort & amp; tree array), nyistoj

Source: Internet
Author: User

Nyist oj 117 calculates the number of reverse orders (merge sort & tree array), nyistoj
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 ;}




Poj2299 Merge (merge) sort the number in reverse order, correct the error why sum is always 0; help fuel, coming soon to the provincial Competition

This assignment is incorrect.
For (int j = 0; j <lr; j ++)
Right [j] = s [j + ls];
 
High scores and answers to C programming questions

/*
Returns the number of reverse orders by means of Merge Sorting. the time complexity is nlg (2n)
*/

# Include <stdio. h>
# Include <time. h>
# Include <string. h>
# Include <stdlib. h>
// Maximum array capacity, which can be changed to 100000
Const int MAX_N = 100000;

// Range of random numbers
Const int RANGE = 100000000;

/*
Function Name: Random
Function: generate random data
Input: array a to save the random number. The number of random numbers to be generated is n.
Output: None
*/
Void Random (int a [], int n)
{
Int I;
Rand (time (NULL ));
For (I = 0; I <n; I ++)
A [I] = rand () % RANGE;
}

/*
Function Name: MergeSort
Function: Merge and sort and calculate the number of reverse orders.
Input: array a to be sorted and the range [begin, end] to be sorted
If the number of elements in a is n, the sorting interval [begin = 0, end = n-1]
Number of reverse orders from a [begin] to a [end]
(The data may be out of the int range, and a larger range of _ int64 is used)
*/
_ Int64 MergeSort (int a [], int begin, int end)
{
If (begin = end) return 0;
Int I, j, k, mid;
Int temp [MAX_N];
Mid = (begin + end)/2;
_ Int64 ret = MergeSort (a, begin, mid );
Ret + = MergeSort (a, mid + 1, end );
I = begin;
J = mid + 1;
For (k = begin; k <= end; k ++)
{
If (I <= mid & a [I] <= a [j] | j> end)
Temp [k] = a [I ++];
Else
{
Temp [k] = a [j ++];
Ret + = mid-I + 1;
}
}
Memcpy (a + begin, temp + begin, (end-begin + 1) * sizeof (int ));
Return ret;
}

/*
Function Name Output
Function: print data
Input: array a, number of data to be output n
Output: None
*/
Void Output (int a [], int n)
{
Int I;
For (I = 0; I <n-1; I ++)
Printf ("% d,", a [I]);
Printf ("% d \ n", a [I]);
}

Int main ()
{
Int a [MAX_N];
// Generate random data
Random (a, MAX_N );

// Output random data to the file
// Output (a, MAX_N );

// Sort the random data and calculate the number of reverse orders
Printf ("Number of reverse orders: % I64d \ n & q ...... the remaining full text>

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.