POJ 2299-Ultra-QuickSort

Source: Internet
Author: User

The number of adjacent values can be exchanged, and the number of consecutive values is not decreased after the number of times is exchanged .. if each number is smaller than the number on the left, it is exchanged with the number on the left .. a value greater than the number on the right is exchanged with the number on the right... in this way, the answer is equivalent to the number of reverse orders of this series... if the reverse Number of brute force queries is O (n ^ 2... for this question data. unacceptable... the idea of sub-governance... count the number of reverse orders of edge sorting in the Merge Sorting Process... program:

# Include <iostream> # include <stdio. h> # include <string. h> # include <set> # include <ctime> # include <algorithm> # include <queue> # include <cmath> # include <map> # define oo 100000007 # define ll long long # define pi acos (-1.0) # define MAXN 500005 using namespace std; int n, a [MAXN], temp [MAXN]; ll ans; void merge (int s1, int e1, int s2, int e2) {int x, I, j, num = 0; I = s1, j = s2; for (x = s1; x <= e2; x ++) {if (I> e1) temp [x] = a [j ++]; else I F (j> e2) temp [x] = a [I ++]; else if (a [I]> a [j]) {temp [x] = a [j ++]; ans ++ = e1-s1 + 1-num; // The number of inserted segments from the back is inserted in front of the number of segments ...} else temp [x] = a [I ++], num ++;} for (I = s1; I <= e2; I ++) a [I] = temp [I]; return;} void merge_sort (int l, int r) {if (l = r) return; int mid = (l + r) /2; merge_sort (l, mid); merge_sort (mid + 1, r); merge (l, mid, mid + 1, r); return;} int main () {int I; while (~ Scanf ("% d", & n) {if (! N) break; for (I = 1; I <= n; I ++) scanf ("% d", & a [I]); ans = 0; merge_sort (1, n); printf ("% I64d \ n", ans);} return 0 ;}

 


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.