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 }

          

  • 相关阅读:
    使用反射调用某个类的成员方法
    java反射机制
    SVProgressHUD
    __objc__
    loadView
    v2ex 下拉刷新 SCRootViewController
    stuck with
    v2ex 下拉刷新模块
    转载
    vue-cli脚手架构建了项目如何去除Eslint验证(语法格式验证)
  • 原文地址:https://www.cnblogs.com/Philip-Tell-Truth/p/5014831.html
Copyright © 2011-2022 走看看