C/C++_log2000_2017春季算法实验1_3

来源:互联网 发布:冒泡法排序c语言解释 编辑:程序博客网 时间:2024/04/29 04:54

[分治]-逆序对问题

Description

给定一整数数组A=(A1,A2,…An), 若i<j且Ai>Aj,则<I,j>就为一个逆序对。1≤n≤30000。例如数组(3,1,4,5,2)的逆序对有<3,1>,<3,2>,<4,2>,<5,2>

Input

n和A数组

Output

逆序对数目


Sample Input

5
3 1 4 5 2

Sample Output

4


asw:

#include<stdio.h>#define M 100int main(){    int i, j;    int size;    int data[M];    scanf("%d",&size);    int result=0; //计算出的逆序对数    for (j = 0; j<size; j++) scanf("%d",&data[j]);    for(i = 0; i < size; ++i)    {        for(j = i+1; j < size; ++j)        {            if(data[i] > data[j])            {                ++result;            }        }    }    printf("%d",result);    return 0;}
原创粉丝点击