zoukankan      html  css  js  c++  java
  • Mathematics:Ultra-QuickSort(POJ 2299)

                      

                      极度快速排序

      题目大意:在一个输入数组中找逆序数。。。

      水题,求逆序数的很好的算法,就是MergeSort,和我之前发的DNA那个差不多,最后就是后台数据很大,答案要用long long

      

     1 #include <iostream>
     2 #include <functional>
     3 #include <algorithm>
     4 
     5 using namespace std;
     6 
     7 static int num[500005], tmp[500005];
     8 typedef int Position;
     9 
    10 void Merge_Sort(Position, Position, long long *const);
    11 void Merge(Position,Position, Position, long long *const);
    12 
    13 int main(void)
    14 {
    15     int length;
    16     long long ans;
    17     while (~scanf("%d", &length))
    18     {
    19         if (length == 0)break;
    20         for (int i = 0; i < length; i++)
    21             scanf("%d", &num[i]);
    22         ans = 0;
    23         Merge_Sort(0, length - 1, &ans);
    24         printf("%lld
    ", ans);
    25     }
    26     return 0;
    27 }
    28 
    29 void Merge_Sort(Position Left, Position Right, long long *const reverse_num)
    30 {
    31     if (Left < Right)
    32     {
    33         Position Mid = (Left + Right) / 2;
    34         Merge_Sort(Left, Mid, reverse_num);
    35         Merge_Sort(Mid + 1, Right, reverse_num);
    36         Merge(Left, Mid, Right, reverse_num);
    37     }
    38 }
    39 
    40 void Merge(Position Left, Position mid, Position Right, long long *const reverse_num)
    41 {
    42     Position lpos = Left, rpos = mid + 1, lend = mid, rend = Right, pos = Left;
    43 
    44     while (lpos <= lend && rpos <= rend)
    45     {
    46         if (num[lpos] <= num[rpos])
    47             tmp[pos++] = num[lpos++];
    48         else
    49         {
    50             (*reverse_num) += lend - lpos + 1;
    51             tmp[pos++] = num[rpos++];
    52         }
    53     }
    54     while (lpos <= lend)
    55         tmp[pos++] = num[lpos++];
    56     while (rpos<=rend)
    57         tmp[pos++] = num[rpos++];
    58     for (pos = Left; pos <= Right; pos++)
    59         num[pos] = tmp[pos];
    60 }

          

  • 相关阅读:
    Shiro学习(一)总体介绍
    Spring-MVC理解之一:应用上下文webApplicationContext
    现代软件工程 第十四章 【质量保障】 练习与讨论
    rtsp 流媒体服务器,播放器
    ios app 打包
    xcrun: error: unable to find utility "PackageApplication", not a developer tool or in PATH ​
    Bootstrap日期和时间表单组件
    微信小程序组件action-sheet
    微信小程序组件radio
    微信小程序组件slider
  • 原文地址:https://www.cnblogs.com/Philip-Tell-Truth/p/5014831.html
Copyright © 2011-2022 走看看