归并排序求逆序对数:

和归并排序一样,划分和递归求解都好理解,关键在于合并,对于右边的j ,统计出左边比j 大 的元素个数 f(j),所有的f(j)家和就是我们要的逆序对数!

在归并排序中,我们将右边的元素向临时数组中加入的时候,左边还没加入得便是比j 大的元素!  既有m-p个

在加右边时,不断累加m-p即可!

以 POJ 1804 为例!

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. using namespace std;
  5. int a[5007],b[5007],n,sum,ks;
  6. void sort(int x,int y){
  7. if (x + 1 >= y)return;
  8. int m = x + (y-x)/2;
  9. sort(x,m); sort(m,y);
  10. int p = x, q = m, i = x;
  11. while (p < m || q < y){
  12. if (q >= y || (p < m && a[p] <= a[q])) b[i++] = a[p++];
  13. else b[i++] = a[q++], sum+=m-p;
  14. }
  15. for (int i = x; i < y; ++i) a[i] = b[i];
  16. }
  17. int main(){
  18. int T;
  19. scanf("%d",&T);
  20. while(T--){
  21. scanf("%d",&n);
  22. for (int i = 0; i < n; ++i)scanf("%d",a+i);
  23. sum = 0;
  24. sort(0,n);
  25. if (ks++)puts("");
  26. printf("Scenario #%d:\n%d\n",ks,sum);
  27. }
  28. return 0;
  29. }
Brainman
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 9861   Accepted: 5227

Description

Background 
Raymond Babbitt drives his brother Charlie mad. Recently Raymond counted 246 toothpicks spilled all over the floor in an instant just by glancing at them. And he can even count Poker cards. Charlie would love to be able to do cool things like that, too. He wants to beat his brother in a similar task.

Problem 
Here's what Charlie thinks of. Imagine you get a sequence of N numbers. The goal is to move the numbers around so that at the end the sequence is ordered. The only operation allowed is to swap two adjacent numbers. Let us try an example: 
Start with: 2 8 0 3 
swap (2 8) 8 2 0 3 
swap (2 0) 8 0 2 3 
swap (2 3) 8 0 3 2 
swap (8 0) 0 8 3 2 
swap (8 3) 0 3 8 2 
swap (8 2) 0 3 2 8 
swap (3 2) 0 2 3 8 
swap (3 8) 0 2 8 3 
swap (8 3) 0 2 3 8
So the sequence (2 8 0 3) can be sorted with nine swaps of adjacent numbers. However, it is even possible to sort it with three such swaps: 
Start with: 2 8 0 3 
swap (8 0) 2 0 8 3 
swap (2 0) 0 2 8 3 
swap (8 3) 0 2 3 8
The question is: What is the minimum number of swaps of adjacent numbers to sort a given sequence?Since Charlie does not have Raymond's mental capabilities, he decides to cheat. Here is where you come into play. He asks you to write a computer program for him that answers the question. Rest assured he will pay a very good prize for it.

Input

The first line contains the number of scenarios. 
For every scenario, you are given a line containing first the length N (1 <= N <= 1000) of the sequence,followed by the N elements of the sequence (each element is an integer in [-1000000, 1000000]). All numbers in this line are separated by single blanks.

Output

Start the output for every scenario with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the minimal number of swaps of adjacent numbers that are necessary to sort the given sequence. Terminate the output for the scenario with a blank line.

Sample Input

4
4 2 8 0 3
10 0 1 2 3 4 5 6 7 8 9
6 -42 23 6 28 -100 65537
5 0 0 0 0 0

Sample Output

Scenario #1:
3

Scenario #2:
0

Scenario #3:
5

Scenario #4:
0

Source

TUD Programming Contest 2003, Darmstadt, Germany

[Submit]   [Go Back]   [Status]   [Discuss]

POJ 1804 Brainman (归并排序 -- 求逆序对数)相关推荐

  1. 信息竞赛进阶指南--归并排序求逆序对

    // 归并排序求逆序对 void merge(int l, int mid, int r) {// 合并a[l~mid]与a[mid+1~r]// a是待排序数组, b是临时数组, cnt是逆序对个数 ...

  2. 分治法:归并排序求逆序对

    现给出这个定义,什么是逆序对,NOIP火柴排队这个题是逆序对的一个比较好的例题,这里我们只讨论求逆序对的一些高效的算法 一般有归并排序以及树状数组两种方法,本文只讨论归并排序求逆序对 这里不给出原理只 ...

  3. hdu 4911 求逆序对数+树状数组

    http://acm.hdu.edu.cn/showproblem.php?pid=4911 给定一个序列,有k次机会交换相邻两个位置的数,问说最后序列的逆序对数最少为多少. 实际上每交换一次能且只能 ...

  4. 归并排序求逆序对(C语言)

    目录 1.归并排序过程 (1)逆序对概念 (2)基本思想 (3)算法实现 1.归并排序过程 使用归并排序实现求逆序对,那么首先得了解什么是归并排序(关于归并排序可以求逆序对,也是在做题中学会的). 首 ...

  5. hust1347(归并排序求逆序对)

    题意: 给出一个数列,你要对这个数列的数字进行k次交换操作,使得交换之后的数列逆序对虽少. 思路: 求原数列的逆序对,再和k比就行了.求逆序对要用归并排序,因为树状数组开不下. 代码: #includ ...

  6. -9 逆序输出一个整数的各位数字_【每日算法】基础算法——归并排序[求逆序对的数量](四)(思想很经典)...

    题目内容 给定一个长度为n的整数数列,请你计算数列中的逆序对的数量. 逆序对的定义如下:对于数列的第 i 个和第 j 个元素,如果满足 i < j 且 a[i] > a[j],则其为一个逆 ...

  7. 利用归并排序求逆序对

    在逆序对的问题中,如果采用暴力求解的方法,一般也是有效的,但是O(n2)时间复杂度实在是难以接受的.但是对于逆序对问题,却有一个看似不想关的算法来解决–归并排序.时间复杂度和空间复杂度完全与归并排序一 ...

  8. codeforces 414C C. Mashmokh and Reverse Operation(归并排序求逆序对)

    题目链接: C. Mashmokh and Reverse Operation time limit per test 4 seconds memory limit per test 512 mega ...

  9. 归并排序——求逆序对个数

    求解逆序对个数 逆序对 对于一个包含N个非负整数的数组A[1-n],如果有i < j,且A[ i ]>A[ j ],则称(A[ i] ,A[ j] )为数组A中的一个逆序对. 例如,数组( ...

  10. 【NOI导刊】【归并排序求逆序对】最接近神的人

    题目描述 破解了符文之语,小FF开启了通往地下的道路.当他走到最底层时,发现正前方有一扇巨石门,门上雕刻着一幅古代人进行某种活动的图案.而石门上方用古代文写着"神的殿堂".小FF猜 ...

最新文章

  1. 百度开源业内首个口罩人脸检测及分类模型,携手开发者共同抗疫
  2. 用PC端Chrome浏览器进行模拟微信浏览器的开发
  3. 本地区间管理 oracle,Oracle:本地表空间管理,字典表空间管理
  4. web文件 群晖_【原创】群晖NAS 上部署静态网站
  5. 深入理解c++中的函数模板
  6. .net后台怎么提取html中的多个图片的绝对地址_SpringBoot中yml配置文件说明和一些常用配置项说明...
  7. 如何使用SQL删除某个字段重复的记录,保留其中一条
  8. 《机器学习系统设计:Python语言实现》一2.1 Python与机器学习
  9. APK 包名修改工具
  10. matlab存储为二进制txt,matlab读取内容为二进制的TXT文件
  11. Android截屏的几种方法
  12. 致敬mentohust,路由器使用Socket认证华科校园网
  13. win7网络里计算机登录失败,Win7访问网上邻居提示“登陆失败”原因及解决方法...
  14. 第119章 SQL函数 RIGHT
  15. Python修改桌面分辨率
  16. 利用Eclipse JDT抽取Java AST
  17. 自写网络验证,支持注册 充值 在线消息 自动更新
  18. 神经网络架构搜索(NAS)综述
  19. 再看 Kafka Lag
  20. 缓冲区溢出的基本原理

热门文章

  1. Android-TextView跑马灯效果
  2. Spring--quartz中cronExpression 的配置方法
  3. Android 基本测试工具的使用
  4. objective-c 使用NSNumber 将int float long等数据类型加入到数组或字典中
  5. java中412是什么错_HTTP 412 错误 – 先决条件失败 (Precondition failed)
  6. mount 安卓system只读_Android如何让system分区可读写(MTK安卓6.0)-阿里云开发者社区...
  7. 查询Oracle正在执行的sql语句,锁表,解锁
  8. java set方法赋值_java方面:private属性,没有set方法,只有get方法,如何给这个属性赋值?...
  9. window 如何查看tomcat 实时日志_如何处理生产环境Tomcat的catalina.out日志?
  10. [转载] 2020最新Java面试题,常见面试题及答案汇总