POJ 2299 Ultra-quicksort (tree-like array)

Source: Internet
Author: User

The previous time with the merge sort wrote this question, found the tree array also can solve this problem, went to learn a bit

First look at a sequence 6 1 2 7 3 4 8 5, this sequence of reverse number is 5+3+1=9. The bubbling method can be used to directly enumerate the reverse order number, but the time complexity is too high O (n^2). The principle of bubble sorting is to enumerate each array, and then find out how many of the numbers are smaller than this number, which is less than +1 of the number in reverse order. Think about it, if we don't have to enumerate all the numbers that follow the number, but we get the number less than that, then the efficiency will be greatly improved.
Total number of n, how to determine the number of i+1 to the last number of the number is less than the number of first? Suppose you have an interval [1,n], just to determine how many of the intervals [i+1,n] are smaller than the number of I. If we initialize the gross position interval to 0 and then place the number of the first number before it in the corresponding interval to set its value to 1, then the problem is converted to the sum of the [i+1,n] value. Think again, the value of the interval [1,i] + the value of the interval [i+1,n] = the value of the interval [1,n] (i is already marked as 1), so the sum of the interval [i+1,n] value equals the value of N-[1,i]! Because there are always n numbers, it is either smaller or larger than it (large or equal).
Now the problem has been transformed into an interval problem, enumerating each number and then querying the sum of the interval values preceding the number,I-[1,i] both in reverse order.

Tree-like array #include<iostream> #include <string.h> #include <algorithm>using namespace std; #define MAX    500010int c[max];int aa[max];int n;typedef struct nano{int val; int order;} Node;node in[max];int lowbit (int x) {return x& (-X);}        void Update (int x,int val) {while (x<=n) {c[x]+=val;    X+=lowbit (x);    }}int sum (int x) {int s=0;        while (x>=1) {s+=c[x];    X-=lowbit (x); } return s;//forgot to write this statement at first, and thought the tree-like array was wrong}bool CMP (node A,node b) {return a.val<b.val;}    int main (int argc, char *argv[]) {//freopen ("2299.in", "R", stdin);            while (scanf ("%d", &n) ==1&&n) {for (int i=1;i<=n;++i) {scanf ("%d", &in[i].val);        In[i].order=i;        } sort (in+1,in+n+1,cmp);        for (int i=1;i<=n;++i) aa[in[i].order]=i;//discretization to a small range to memset (c,0,sizeof (c));        Long Long ans=0;            for (int i=1;i<=n;++i) {update (aa[i], 1); ans+= (I-sum (Aa[i]));    } printf ("%lld\n", ans); } return 0;}


POJ 2299 Ultra-quicksort (tree-like array)

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.