数据结构实验之排序五:归并求逆序数(SDUT 3402)
归并排序详解(戳我)。
以下是搬了别人的。
#include<stdio.h>
#include<stdlib.h>
long long sum = 0;
int a[100005];
int temp[100005];
void Merge(int s1, int e1, int s2, int e2)
{
int p = 0;
int p1 = s1;
int p2 = s2;
while(p1 <= e1 && p2 <= e2)
{
if(a[p1] <= a[p2])
{
temp[p++] = a[p1 ++];
}
else
{
temp[p ++] = a[p2 ++];
sum += (e1 - p1 + 1);
}
}
while(p1 <= e1)
{
temp[p++] = a[p1++];
}
while(p2 <= e2)
{
temp[p++] = a[p2++];
}
for(int i = s1; i <= e2; i ++)
{
a[i] = temp[i - s1];
}
}
void Merge_sort(int s, int e)
{
int m;
if(s < e)
{
m = ( s + e ) / 2;
Merge_sort(s, m);
Merge_sort(m + 1, e);
Merge(s, m , m + 1, e);
}
}
int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
for(int i = 0; i < n; i ++)
{
scanf("%d",&a[i]);
}
sum = 0;
Merge_sort(0,n-1);
printf("%lld\n",sum);
}
return 0;
}
数据结构实验之排序五:归并求逆序数(SDUT 3402)的更多相关文章
- SDUT 3402 数据结构实验之排序五:归并求逆序数
数据结构实验之排序五:归并求逆序数 Time Limit: 40MS Memory Limit: 65536KB Submit Statistic Problem Description 对于数列a1 ...
- SDUT-3402_数据结构实验之排序五:归并求逆序数
数据结构实验之排序五:归并求逆序数 Time Limit: 50 ms Memory Limit: 65536 KiB Problem Description 对于数列a1,a2,a3-中的任意两个数 ...
- 2014多校第五场1001 || HDU 4911 Inversion (归并求逆序数)
题目链接 题意 : 给你一个数列,可以随意交换两相邻元素,交换次数不超过k次,让你找出i < j 且ai > aj的(i,j)的对数最小是多少对. 思路 : 一开始想的很多,各种都想了,后 ...
- 51 Nod 1107 斜率小于0的连线数量 (转换为归并求逆序数或者直接树状数组,超级详细题解!!!)
1107 斜率小于0的连线数量 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 二维平面上N个点之间共有C(n,2)条连线.求这C(n,2)条线中斜率小于0的线 ...
- 归并求逆序数(逆序对数) && 线段树求逆序数
Brainman Time Limit: 1000 MS Memory Limit: 30000 KB 64-bit integer IO format: %I64d , %I64u Java c ...
- 315. Count of Smaller Numbers After Self(二分或者算法导论中的归并求逆序数对)
You are given an integer array nums and you have to return a new counts array. The counts array has ...
- 数据结构实验之排序四:寻找大富翁(SDUT 3401)
#include <stdio.h> #include <stdlib.h> #include <string.h> void Swap(int a[], int ...
- 数据结构实验之排序七:选课名单 (SDUT 3404)
#include <stdio.h> #include <string.h> #include <stdlib.h> struct node { char data ...
- 数据结构实验之二叉树五:层序遍历 (SDUT 3344)
#include <bits/stdc++.h> using namespace std; struct node { char data; struct node *lc, *rc; } ...
随机推荐
- 从ghost映像.gho文件快速创建vmware虚拟机
从ghost映像.gho文件快速创建vmware虚拟机 https://www.cnblogs.com/blog2018/p/8857146.html ghost文件.gho和vmware文件都是磁盘 ...
- 利用PHPExcel快速导出excel
<?php $objPHPExcel = new PHPExcel(); $objSheet = $objPHPExcel->getActiveSheet(); $array = arra ...
- 解决SecureCRT 中文乱码??
在linux服务器上搭建solr,用的是SecureCRT 连接linux服务器,发现不能输入中文,配置文件中的中文也是乱码:先以为是SecureCRT工具编码的问题,把编码改成utf-8之后发现还 ...
- HTML基本代码
HTML 今天回顾html,总结一下今日所学内容. -------------------正文-------------------------- 目的:通过一些基础的标签制作关于LOL的静态网页 所 ...
- Fortify漏洞之 Log Forging(日志伪造)
继续对Fortify的漏洞进行总结,本篇主要针对 Log Forging(日志伪造)的漏洞进行总结,如下: 1.1.产生原因: 在以下情况下会发生 Log Forging 的漏洞: 1. 数据从一个不 ...
- Map转url ,url转Map工具类
/** * 将url参数转换成map * @param param aa=11&bb=22&cc=33 * @return */ public static Map<String ...
- Python——Int&Bool
整数类型: int类型,多用于数字运算 print(666) print(6+1) 整数类型转换: v1 = 666 v2 = str(v1) #会得出字符串的666 v1 = True v2 = i ...
- springboot 使用常用注解
找到方法封装成json格式 @RestController = @Controller+@ResponseBody //一个组合注解,用于快捷配置启动类,springboot启动主入口 @Spring ...
- linux 利用LDAP身份集中认证
碰巧所在的公司用到了ldap 集中身份认证,所有打算研究下这套架构,但是看遍了网络上的很多教程,要么不完整,要么就是照着根本弄不出来,十月一研究了三天,结合八方资源终于弄出来了,真是不容易,哎,特此记 ...
- 矩阵迹 tr(AB)=tr(BA)的证明
其实更为直观的理解是:AB与BA具有相同的对角线元素,因此tr(AB)=tr(BA)必然成立 ref:https://blog.csdn.net/silence1214/article/details ...