POJ 2299 Ultra-quicksort Seeking reverse order number, tree array solution, detailed analysis

Source: Internet
Author: User

Ultra-quicksort
Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 44554 Accepted: 16195

Description

In this problem, you has to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping, adjacent sequence elements until the sequence is Sorted in ascending order. For the input sequence
9 1 0 5 4,
Ultra-quicksort produces the output
0 1 4 5 9.
Your task is to determine what many swap operations Ultra-quicksort needs to perform in order to sort a given input sequenc E.

Input

The input contains several test cases. Every test case begins with a line this contains a single integer n < 500,000-the length of the input sequence. Each of the following n lines contains a single integer 0≤a[i]≤999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must is processed.

Output

For every input sequence, your program prints a single line containing an integer number OP, the minimum number of swap op Erations necessary to sort the given input sequence.

Sample Input

59105431230

Sample Output

60
My AC Status:
Haha, the time even the one-tenth of the topic requested, I am still very satisfied with the tree-like array.
First of all, I would like to say the tree array to find the inverse number of the method, the number of reverse order definition: Click to open the link
After reading the definition of reverse number, first of all, from the definition, we can actually convert, there is a sequence an, assuming that there is a CI behind it is smaller than he, the reverse number is equal to c=c1+c2+ ... ci+ ... Cn, he is actually equivalent to assuming that the number I, he has a Di in front of the number of larger than him, d=d1+d2+...+di+...+dn=c. This is easy to understand, I believe you can read it.
Then you might think it's over, obviously, it's not over, think about the structure of the tree array, and if you ask how many of them are older than him, he has to go straight from 11 to infinity. It's scary to think about, so we have to switch again, can we first find out how many of the first I number is not greater than the number of first I? This is very simple, we just have to traverse the 1->i, we solve, that good, a total number of I, assuming that there is X in front of it is not greater than the number of I, that is greater than the number of the large nature does not come out, is not equal to i-x-1?
Well, the analysis here, I think everyone should understand, I have to make it clear that the tree array cannot directly find a sequence containing a number less than or equal to 0 of the number of reverse order,
As to why there can be no negative numbers, do you think that the subscript of the array can be negative? As for why can't have 0, that is because the tree array has a 0 trap, this own Baidu, not much to say.
Can not be directly asked, but we can indirectly beg ah, as to how to beg, I simply say, the conversion of the original point of the axis is not OK!! Suppose 10000>ai>-10000, we can put ai+10000, get 20000<ai+10000<0, you see, this question is not become we will! I don't know if other people are dealing with this, this is my own way of thinking, the way other people deal with the estimation and my not too much, after all, so simple,
OK, here's My Code:
#include <stdio.h> #include <string.h> #define MAX 1001000int t[max];int lowbit (int x) {return x& (-X);} void Update (int pos) {while (Pos<max) {++t[pos];p os + = Lowbit (pos);}} Long long query (int pos) {Long Long count = 0; while (pos>0) {count + = T[pos];p os-= Lowbit (POS);} return count;} int main () {int n;while (~scanf ("%d", &n) && N) {memset (t,0,sizeof (t)); long long sum = 0; for (int i = 0; i < n; ++i) {int temp; scanf ("%d", &temp), int count = query (temp+1),//prevents the tree array from 0 trap sum + = i-count; update (temp+1);} printf ("%i64d\n", sum);} return 0;}

You see,,,, is not a lot less than the merger of the number of reverse order code, but also easier to understand!
Hard to code so many words, and we support a bit more
Reprint please indicate the source

POJ 2299 Ultra-quicksort Seeking reverse order number, tree array solution, detailed analysis

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.