Poj2299 -- merge sort to calculate the number of reverse orders, poj2299 -- reverse order

Source: Internet
Author: User

Poj2299 -- merge sort to calculate the number of reverse orders, poj2299 -- reverse order

/** \ Brief poj2299 ** \ param date 2014/8/5 * \ param state AC * \ return memory 4640 K time 3250 ms **/# include <iostream> # include <fstream> # include <cstdio> # include <cstring> using namespace std; const int MAXN = 500000; int Arr [MAXN]; int T [MAXN]; // _ int64 t ;__ int64 t; void merge_sort (int * A, int x, int y, int * T) {if (y-x> 1) {int m = x + (y-x)/2; // partition int p = x, q = m, I = x; merge_sort (A, x, m, T); merge_sort (A, m, y, T); while (p <m | q <y) {if (q> = y | (p <m & A [p] <= A [q]) T [I ++] = A [p ++]; // copy from the left half array to the temporary space else {T [I ++] = A [q ++]; // from the right-hand array ...... T + = m-p; // calculate the number of Reverse Order pairs} for (int I = x; I <y; I ++) A [I] = T [I]; // copy back to A array from the auxiliary space} int main () {// cout <"Hello world! "<Endl; // freopen (" input.txt "," r ", stdin); int n = 0; while (scanf (" % d ", & n )) {if (n = 0) break; memset (Arr, 0, sizeof (Arr); memset (T, 0, sizeof (T )); for (int I = 0; I <n; I ++) cin> Arr [I]; merge_sort (Arr, 0, n, T ); printf ("% I64d", t); t = 0; cout <endl;} return 0 ;}


UseMerge SortingSequentialNumber of reverse orders



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.