POJ 2299 Ultra-quicksort

Source: Internet
Author: User

Test instructions: Ask a bunch of numbers to sort how many times to swap ... This sorting method did not see what it was when reading the question ... Later Baidu said it was bubbling, that is, it is a problem of the number of reverse order.

Solution: To find the number of reverse order three methods, line tree, tree array, merge sort. The above three methods are sorted according to my order, the actual optimal is the merging and the tree array, the segment tree has constant.

Line segment tree: The first discretization process, the discretization of the method is to give the number of numbers, and then to sort, ordered sequence is the sequence after the discretization. Then ask the number of reverse order to see the number before each digit is larger than their own, so you can insert Edge query, record which number appears, appears as 1 did not appear as 0 (this problem seems to all the numbers are different, if there is the same number should record the number of occurrences), with the line segment tree maintenance interval and, That is, you can know how many numbers appear in a certain interval, and for the current number, you can tell how many of the numbers are larger than the number that is present in the interval of the current number to N.

Code:

#include <stdio.h> #include <iostream> #include <algorithm> #include <string> #include < string.h> #include <math.h> #include <limits.h> #include <time.h> #include <stdlib.h># include<map> #include <queue> #include <set> #include <stack> #include <vector> #define LL Long longusing namespace std; #define Lson L, M, RT << 1#define Rson m + 1, R, RT << 1 |    1int sum[500005 << 2];struct node{int num, no;        BOOL operator < (const node &AMP;TMP) const {if (num = = tmp.num) return no < tmp.no;    return num < Tmp.num; }}num[500005];void pushup (int rt) {Sum[rt] = Sum[rt << 1] + sum[rt << 1 | 1];}        void Update (int pos, int l, int r, int rt) {if (L = = r) {Sum[rt] = 1;    return;    } int m = (L + r) >> 1;    if (POS <= m) Update (POS, Lson);    else Update (POS, Rson); Pushup (RT);} int query (int ll, int rr, int l, int r, int rt) {    if (ll <= l && RR >= R) return SUM[RT];    int m = (L + r) >> 1;    int res = 0;    if (ll <= m) Res + = query (ll, RR, Lson);    if (RR > m) res + = query (ll, RR, Rson); return res;}    int main () {int n;        while (~SCANF ("%d", &n) && N) {memset (sum, 0, sizeof sum);            for (int i = 1; I <= n; i++) {scanf ("%d", &num[i].num);        num[i].no = i;        } sort (num + 1, num + n + 1);        LL ans = 0;            for (int i = 1; I <= n; i++) {ans + = query (num[i].no, n, 1, N, 1);        Update (num[i].no, 1, N, 1);    } printf ("%lld\n", ans); } return 0;}

  

Tree array: The idea is the same as the line tree, maintenance interval and, but the time is reduced by half.

Code:

#include <stdio.h> #include <iostream> #include <algorithm> #include <string> #include < string.h> #include <math.h> #include <limits.h> #include <time.h> #include <stdlib.h># include<map> #include <queue> #include <set> #include <stack> #include <vector> #define LL    Long longusing namespace std;int sum[500005];struct node{int num, no;        BOOL operator < (const node &AMP;TMP) const {if (num = = tmp.num) return no < tmp.no;    return num < Tmp.num; }}num[500005];int n;inline int lowbit (int x) {return x & (-X);} void Update (int pos) {for (int i = pos; I <= n; i + = Lowbit (i)) sum[i] + = 1;}    int getsum (int pos) {int res = 0;    for (int i = pos; i > 0; I-= Lowbit (i)) res + = Sum[i]; return res;}        int main () {while (~SCANF ("%d", &n) && N) {memset (sum, 0, sizeof sum); for (int i = 1; I <= n; i++) {scanf ("%d", &num[I].num];        num[i].no = i;        } sort (num + 1, num + n + 1);        LL ans = 0;            for (int i = 1; I <= n; i++) {ans + = Getsum (n)-getsum (num[i].no);        Update (num[i].no);    } printf ("%lld\n", ans); } return 0;}

  

Merge sort: Each time a combination of two sections of the sequence of the interval, when the following interval has a number inserted before a number in the previous interval, example 1 3 4 7 and 2 5 6 8,2 to be inserted in front of 3, then the equivalent of bubbling and 7, 4, 3 exchange positions respectively, so 2 in the exchange of 3 times, the same 5 Exchange 1 times, 6 , 8 swaps 0 times.

Code:

#include <stdio.h> #include <iostream> #include <algorithm> #include <string> #include < string.h> #include <math.h> #include <limits.h> #include <time.h> #include <stdlib.h># include<map> #include <queue> #include <set> #include <stack> #include <vector> #define LL Long longusing namespace Std;int a[500010], ta[500010];    LL ans = 0;void mergesort (int l, int r) {if (L >= R) return;    int m = (L + r) >> 1;    MergeSort (L, M);    MergeSort (M + 1, R);  int p1 = l, p2 = m + 1, p3 = L;//P1 is the previous interval pointer, p2 is the latter interval pointer, p3 is the temporary array ta pointer while (p1 <= m | | P2 <= r) {if (P2 > R | | (P1 <= m && a[p1] <= A[P2])) When the latter interval is complete or the previous interval is not complete and the previous interval current number is more than the current number of hours (previously not written P1 <= m This condition has been re ...        ) ta[p3++] = a[p1++];            else//The current interval is complete or the latter interval is not complete and the number of the previous interval is greater {if (P1 <= m) ans + = (m + 1-p1);        ta[p3++] = a[p2++]; }} for (int i = l; I <= R; i++) A[i] = ta[I];}    int main () {int n;        while (~SCANF ("%d", &n) && N) {for (int i = 0; i < n; i++) scanf ("%d", &a[i]);        Ans = 0;        MergeSort (0, n-1);    printf ("%lld\n", ans); } return 0;}

  

POJ 2299 Ultra-quicksort

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.