归并排序 求逆序对数

合并排序/归并排序

分治策略

一种平衡、简单的二分分治策略

当只剩一个数的时候就说明他是有序的

  • 1.分解:一分为二  o(1)
  • 2.治理:对两个子序列合并排序 
  • 3.合并:O(n)的时间 把两个有序的序列合并  最主要的部分

树的高度是O(logN)

元素个数:high-low+1

子序列:

  • low        mid
  • mid+1     high

辅助数组 排序完之后将结果返回原数组  

时间的复杂度:O(NlogN)

排序稳定

快速排序学会划分、归并排序学习归并 这两个算法就能够很好的理解了,都采用分治思想。

 1 /* 快速排序的代码最主要的就是合并操作 */
 2 #include <iostream>
 3 
 4 using namespace std;
 5 
 6 void Merge(int A[], int low, int mid, int high){  // 合并操作
 7     int *B = new int[high-low+1];  // 申请辅助数组
 8     int i=low, j=mid+1, k=0;
 9     while(i<-mid && j<=high){
10         if(A[i]<=A[j])
11             B[k++] = A[i++];
12         else
13             B[k++] = A[j++];
14     }
15     // 处理剩余的部分
16     while(i<=mid) B[k++] = A[i++];
17     while(j<=high) B[k++] = A[j++];
18     for(i=low, k=0; i<high; i++, k++)
19         A[i] = B[k];
20     delete []B; // 释放空间 B数组
21 }
22 
23 void MergeSort(int A[], int low, int high){
24     if(low<high){  // 当low=high停止,此时low已经等于high了
25         int mid = (low+high)/2;
26         MergeSort(A, low, mid);
27         MergeSort(A, mid+1, high);
28         // 排序完了再排序,合并的两个序列一定是有序的
29         Merge(A, low, mid, high); // 合并
30     }
31 }
32 
33 int main(){
34     int n, A[100];
35     cout << "请输入数列中的元素个数n为:" << endl;
36     cin >> n;
37     cout << "请以此输入数列中的元素:" << endl;
38     for(int i=0; i<n; i++)
39         cin >> A[i];
40     MergeSort(A, 0, n-1);
41     cout << "合并排序的结果:" << endl;
42     for(int i=0; i<n; i++)
43         cout << A[i] << " ";
44     cout << endl;
45     return 0;
46 }

求逆序对数

https://www.nowcoder.com/practice/6ef4d5e5767b470da56e64ee48e0abea?tpId=98&tqId=32872&tPage=3&rp=3&ru=%2Fta%2F2019test&qru=%2Fta%2F2019test%2Fquestion-ranking

#include <iostream>

using namespace std;

int ans;

void cnt_n(int a[], int low, int mid, int high){
    for(int i=low; i<=mid; i++){
        for(int j=mid+1; j<=high; j++){
            if(a[i]>a[j])
                ans += 1;
        }
    }
}

void bi_pattion(int a[], int low, int high){
    if(low < high){
        int mid = (high+low)/2;
        bi_pattion(a, low, mid);
        bi_pattion(a, mid+1, high);
        cnt_n(a, low, mid, high);
    }
}

int main(){
    freopen("test.txt", "r", stdin);
    int n;
    ans = 0;
    int *a = new int[n];
    cin >> n;
    for(int i=0; i<n; i++)
        cin >> a[i];
    bi_pattion(a, 0, n-1);
    cout << ans << endl;
}
用例:
1000 65579 60421 32483 73595 85687 74923 55357 69628 46579 95861 91353 11535 18473 33656 60748 43458 25564 59974 61474 32827 81229 72044 81807 93221 71864 75979 13059 12341 32030 25457 35323 3321 18110 51476 30260 80764 47805 94347 13947 2328 88069 52201 51520 67576 86281 66487 3401 78345 64710 49068 65571 4733 12484 74475 95923 73659 52380 21466 43754 73318 61205 62266 11031 87784 2919 18376 10262 32635 74688 77999 65958 46837 54244 10695 71288 8685 52463 7054 54308 98542 43830 37633 6774 3511 39212 33560 68133 556 10304 98811 12708 23388 86628 25221 53970 48752 55368 71636 74828 33805 36037 72265 56898 69350 15926 59586 20710 45058 93472 46833 38272 82405 89010 95545 56287 32477 16647 87525 75531 93996 89992 97477 30166 38636 71823 58757 81649 4932 27111 94732 77946 78278 36559 33934 21074 74498 27517 93134 32461 10346 6719 50420 26979 19203 99580 77991 41315 23333 22905 14439 64039 7695 24120 58089 97123 75626 88507 8432 49975 69784 23494 82820 86724 61849 96835 1746 91707 11450 30622 80384 68785 59962 8305 22923 68047 84154 28538 35583 13445 68001 42343 34098 23519 82537 70676 50301 11375 99498 16620 50680 26666 18912 95147 21649 10383 81755 80292 54953 54465 21347 27166 11147 36694 91782 99737 68682 7433 60992 66031 19222 22762 59590 28213 98650 87892 12335 21184 35253 12863 12927 82373 59667 49036 30608 68953 77415 85758 25152 37687 29639 17064 64082 94656 17636 49857 24161 16540 21509 83476 88456 38814 50463 3028 9447 91462 46483 60621 43507 45888 54468 79318 40349 81802 61843 4226 33821 98611 75865 81269 61211 46751 52864 54996 66265 42023 44382 73333 47296 93021 35439 61462 74438 31021 84750 58825 71449 65055 93905 48845 83211 42872 88894 6488 25626 62464 5581 73680 45626 7423 74107 6457 22049 85697 3751 75208 52334 40313 53550 13110 13655 29057 96062 9009 66766 17119 20024 85772 58736 99 90087 24759 94218 20365 75192 40553 6984 37803 40533 87481 87195 16064 54370 38093 36325 69184 72274 37673 94948 92056 88489 3641 56683 90468 28330 22453 36311 20209 72578 2271 66423 77099 77424 47300 70401 44997 78766 85275 10606 8287 39171 50900 78938 81537 6581 69916 83553 81460 78555 48527 77386 88445 83857 44795 53984 50615 25541 86413 93595 49484 52526 87387 3198 98735 57212 60365 8377 19545 71338 85864 61838 11935 12304 49689 5521 29804 27861 68314 70056 7296 56674 53028 4242 73156 16018 73521 46212 55305 60741 1356 53198 66717 73268 42840 23068 76073 30814 13338 51129 27273 26145 17603 85038 45992 85733 48414 41224 3928 13842 6099 89112 41931 38049 2613 69908 66962 13616 84942 86906 1807 62191 92549 42324 3640 28632 62569 84567 19243 41468 29661 13733 11368 45496 54368 64083 86992 46854 12023 82309 33368 62927 54490 97973 41994 85502 2808 24755 74552 30352 41639 9894 56221 88552 34700 12689 84946 96159 79971 18033 22282 16216 35436 77538 7467 85841 31922 67584 64553 85462 19353 50315 11969 76240 25156 29541 85615 66203 41600 149 9772 68405 35920 75758 8071 26289 93717 63305 44703 67468 54746 83342 25018 78391 54362 60562 33317 47666 85582 68410 40297 13818 59191 18782 4870 39482 44676 99049 61743 20893 32673 69373 64417 53891 10972 23763 32577 31377 1525 4704 39409 66115 50625 68563 59919 70653 76539 57242 42923 53727 1320 28901 67234 98134 10181 63010 22971 81423 7151 93671 22878 29031 23535 89071 79724 71582 53792 52859 75391 6155 52926 84410 22783 80296 87309 34883 33277 36077 46723 31487 10896 43002 24019 64036 9833 50862 33815 98587 13685 61557 43001 77411 33616 88447 90238 93839 51680 35307 30639 24490 48789 87778 4710 84667 15290 44672 85170 86115 5816 26156 62918 99203 1416 53001 64708 15579 24435 34406 87745 92013 15464 18032 45781 26080 82616 23623 31586 67553 22674 33965 74143 57987 11958 57371 47744 52487 71896 71630 36995 68119 76819 15902 57945 7532 12680 32240 97646 93487 18578 58337 17592 74556 1574 66754 66136 93923 8620 72004 49936 53385 57908 64169 33058 51563 80984 40634 42944 81826 8077 84502 1429 92802 65796 80455 58638 58787 26609 52615 73572 54331 6276 67182 99276 81989 53233 49302 28559 6238 22842 33371 22315 91805 87553 6380 28581 90221 47394 19546 28080 76875 61853 8987 57448 81310 9611 46267 46233 25229 28626 86368 80669 63233 36147 36473 13858 80057 43009 54211 33929 94852 10156 53399 14923 22687 7419 38226 40246 1610 86264 68066 78485 67753 87494 22474 74158 54644 71277 76339 66981 31003 33270 45520 11710 7130 81170 41548 11200 69173 99412 81554 63316 83632 27423 504 41677 13934 6522 49896 13697 45252 34593 93676 32821 76600 95221 28214 43672 60635 9075 99626 14310 46081 56705 92233 60652 95897 71397 2171 97707 64109 79792 19927 96853 1560 49207 69810 56501 76126 33123 47170 87339 75213 99870 17101 11093 12014 99150 79795 70121 19688 56159 77173 98046 97265 96184 6880 99659 89426 23279 67898 2855 90582 37304 84584 61680 5326 19800 62585 3303 84665 48435 7928 76730 20086 84984 69623 72735 71606 72628 42182 36013 36021 74899 65890 85534 82440 80382 54616 33429 39037 91992 76771 24816 52247 56712 3175 1638 94466 55911 35124 5120 6288 2315 68511 55532 11425 9536 79582 90256 5614 51378 69887 43035 7059 9032 39301 40887 85175 22314 55680 89467 6041 36771 90377 38973 94200 85473 22039 39780 81151 12729 62272 47260 80780 5288 23728 52259 43452 62836 25727 30912 89972 25842 7762 44563 11982 43808 86896 36583 85022 72178 1343 69280 21514 26555 78946 21793 26240 8850 88056 83654 99265 68639 64946 91676 65844 53408 82528 28733 87224 1590 45931 77884 70409 6650 62818 54797 15841 89942 36723 21460 19579 23463 63382 95496 90866 47053 49123 8196 28488 40556 31140 63722 63748 2274 19396 22356 58611 24869 20423 52504 69326 36184 8403 5591 89468 47050 27340 33507 34926 30540 26078 6379 96264 47149 65363 99908 66080 68340 9188 44597 10175 35608 37484 45983 25892 68479 82599 39112 9842 45403 45922 5852 29433 54527 21724 79662 45688 25461 95556 38987 16133 9228 9592 33534 41349 74944 19720 88118 64451 17194 5354 52390 23212 91830 79577 20646 97130 30610 45034 7959 16698 
对应输出应该为:
255284 
你的输出为:
371610

本地测试是通过的,但是不知道为什么提交到牛客有问题?

牛客的这道题比较水,直接模拟也可以通过

#include <iostream>

using namespace std;

int a[50000+10];

int main(){
    // freopen("test.txt", "r", stdin);
    int n;
    int ans = 0;
    // int *a = new int[n];
    cin >> n;
    for(int i=0; i<n; i++)
        cin >> a[i];
    
    for(int i=0; i<n; i++){
        for(int j=i+1; j<n; j++){
            if(a[i]>a[j])
                ans++;
        }
    }
    cout << ans << endl;
}

最后返现是new数组出的问题;哎!

有超时!

ZHB失败!

猜你喜欢

转载自www.cnblogs.com/JCcodeblgos/p/11451243.html
今日推荐