poj2299:ultra-quicksort-merge sort to solve the problem of reverse order number

Source: Internet
Author: User

1#include <iostream>2#include <malloc.h>3 using namespacestd;4 Long Longans;5 voidMergeint*a,intLeintMidintRT) {6     int*sort_data= (int*) malloc (sizeof(int) * (rt-le+1));7     if(!sort_data)return;8     intI=le,j=mid+1, pt=0;9      while(i<=mid&&j<=RT) {Ten         if(a[i]<=A[j]) { Onesort_data[pt++]=a[i++]; A         } -         Else{//exist swap action -sort_data[pt++]=a[j++]; the             /* - once we swap the postion of both the a[i] and A[j], - We do change the positon of A[i+1......mid] - as a result, we do the postion of A[i,i+1,.... mid]. +             */ -ans+=mid-i+1; +         } A     } at      while(i<=mid) { -sort_data[pt++]=a[i++]; -     } -      while(j<=RT) { -sort_data[pt++]=a[j++]; -     } in     intp; -      for(inti=le,p=0; i<=rt;i++,p++){ toa[i]=Sort_data[p]; +     } - } the voidMergeSort (intA[],intStinted) { *     if(st<ed) { $         intMid= (st+ed)/2;Panax Notoginseng mergesort (a,st,mid); -MergeSort (a,mid+1, ed); the merge (a,st,mid,ed); +     } A } the intMain () { +     intn,num[500010]; -      while(SCANF ("%d",&N), N) { $ans=0; $          for(intI=0; i<n;i++){ -scanf"%d",&num[i]); -         }  theMergeSort (NUM,0, N-1); -printf"%i64d\n", ans);Wuyi     } the     return 0; -}
View Code

poj2299:ultra-quicksort-merge sort to solve the problem of reverse order number

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.