zoukankan      html  css  js  c++  java
  • 逆序数 51nod 1019 归并 分治

    在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序。一个排列中逆序的总数就称为这个排列的逆序数。
     
    如2 4 3 1中,2 1,4 3,4 1,3 1是逆序,逆序数是4。给出一个整数序列,求该序列的逆序数。
    Input
    第1行:N,N为序列的长度(n <= 50000)
    第2 - N + 1行:序列中的元素(0 <= A[i] <= 10^9)
    Output
    输出逆序数
    Input示例
    4
    2
    4
    3
    1
    Output示例
    4

    看程序吧 不会讲
     1 #include <iostream>
     2 using namespace std;
     3 #include<string.h>
     4 #include<set>
     5 #include<stdio.h>
     6 #include<math.h>
     7 #include<queue>
     8 #include<map>
     9 #include<algorithm>
    10 #include<cstdio>
    11 #include<cmath>
    12 #include<cstring>
    13 #include <cstdio>
    14 #include <cstdlib>
    15 #include<stack>
    16 #include<vector>
    17 int a[51000];
    18 int b[51000];
    19 long long sum=0;
    20 void dfs1(int kaishi,int zhongjian,int jieshu)
    21 {
    22     int i=kaishi,j=zhongjian+1;
    23     int k=kaishi;
    24     while(i<=zhongjian&&j<=jieshu)
    25     {
    26         if(a[i]<a[j])
    27             b[k++]=a[i++];
    28         else
    29         {
    30 
    31             b[k++]=a[j++];
    32             sum+=zhongjian-i+1;
    33             //cout<<j<<"_"<<k<<endl;
    34             continue;
    35         }
    36     }
    37     while(i<=zhongjian)
    38     {
    39         b[k++]=a[i++];
    40     }
    41     while(j<=jieshu)
    42     {
    43         b[k++]=a[j++];
    44     }
    45     for(i=kaishi;i<=jieshu;i++)
    46         a[i]=b[i];
    47 }
    48 void dfs(int kaishi,int jieshu)
    49 {
    50     if(kaishi<jieshu)
    51     {
    52         int zhongjian=(kaishi+jieshu)/2;
    53         dfs(kaishi,zhongjian);
    54         dfs(zhongjian+1,jieshu);
    55         dfs1(kaishi,zhongjian,jieshu);
    56     }
    57 }
    58 int main()
    59 {
    60     int n;
    61     scanf("%d",&n);
    62     for(int i=0;i<n;i++)
    63         {
    64             scanf("%d",&a[i]);
    65             b[i]=a[i];
    66         }
    67     dfs(0,n-1);
    68     cout<<sum<<endl;
    69     return 0;
    70 }
    View Code
  • 相关阅读:
    Python 多线程、进程
    Python网络编程 Socket编程
    Python基础7 面向对象编程进阶
    Python基础6 面向对象编程
    Python基础5 常用模块学习
    Python基础4 迭代器、装饰器、软件开发规范
    Python基础3 函数、递归、内置函数
    Python基础2 列表 字典 集合
    21-Python-多进程
    20-Python-queue队列
  • 原文地址:https://www.cnblogs.com/dulute/p/7499093.html
Copyright © 2011-2022 走看看