POJ 2299 ultra-quicksort (merge sort)

Source: Internet
Author: User

Test instructions: Swap adjacent two numbers to sort at least a few times

Ideas:

Test instructions can be converted into several reverse order numbers in the sequence

Can be thought of as bubble sort, but complexity is too high to complete with merge sort

(note n<=5000000 so that ans is used for long integer type)

#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;inta[5000000+ -];intt[5000000+ -];__int64 ans;intL,r;voidMergeintXinty) {    if(y-x>1)    {        intm=x+ (y-x)/2; intP=x,q=m,i=x;        Merge (X,m);        Merge (M,y);  while(p<m| | q<y) {if(q>=y| | (P<m&&a[p]<=a[q])) t[i++]=a[p++]; Else                {                    if(p<m) {//printf ("%d%d...\n", a[p],a[q]);ans+= (M-p); } t[i++]=a[q++]; }        }         for(i=x;i<y;i++) a[i]=T[i]; //printf ("%d%d%d\n", x,m,y);  for (i=0;i<r;i++) printf ("%d", a[i]); printf ("\n\n\n");    }}intMain () {intN; inti,j,k;  while(SCANF ("%d",&N) {ans=0;  for(i=0; i<n;i++) {scanf ("%d",&A[i]); }        //l=0;r=n;Merge0, N); printf ("%i64d\n", ans); }    return 0;}

POJ 2299 ultra-quicksort (merge sort)

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.