POJ 1804 Brainman (归并排序 -- 求逆序对数)

归并排序求逆序对数:

和归并排序一样,划分和递归求解都好理解,关键在于合并,对于右边的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]

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/549373.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Online Judge System

原文&#xff1a;http://hi.baidu.com/myalgorithm/blog/item/936031105bdb5958f819b880.htmlOnline Judge System起源與由來「 Association for Computing Machinery (ACM) 」是一個致力於電腦科學教育的協會&#xff0c;出版大量專業期刊、文獻&#xff0c;舉辦重大的計算機科…

大学计算机网络复习题

模拟试题 一、填空题 1、局域网中常用的拓扑结构主要有星型、 环形 、总线型三种。 2 、在当前的网络系统中&#xff0c;由于网络覆盖面积的大小、技术条件和工作环境不同&#xff0c;通常分为广域网、 局域网 、和城域网三种。 3、常用的通信介…

Windows系统常用技巧总结

1、卸载office2003的时候会出现卸载不了的现象&#xff0c;提示“OFFICE2003不支持卸载修补程序”&#xff0c;打补丁也打不上。 解决方法&#xff1a; 在注册表HKEY_LOCAL_MACHINE\SOFTWARE\Classes \Installer\Products\找到OFFICE2003的东西&#xff0c;删除后&#xff0c;才…

计算机网络复习题(全套)

1、网络协议主要要素为_________。 【答案】语法、语义、同步。 2、一座大楼内的一个计算机网络系统&#xff0c;属于________。 【答案】LAN。 3、随着电信和信息技术的发展&#xff0c;国际上出现了所谓“三网融合”的趋势&#xff0c;下列属于三网之一的是(ABC) A.传统…

XXX管理平台系统——会议管理

XXX管理平台系统会议管理 关于项目中的会议管理&#xff0c;其实并无定数可言&#xff0c;与个人的爱好、管理风格、沟通能力、团队组织、项目规模、甚至公司的 IT 管理风格息息相关。 在 H公司 工作的时候&#xff0c;我的 department manager 喜欢把所有的员工召集起来一起进…

为什么要使用信道复用技术?常用的信道复用技术有哪些?

时分复用&#xff0c;频分复用&#xff0c;码分复用&#xff0c;空分复用。 使用复用技术是为了区分用户&#xff0c;合理利用所有的频谱资源不浪费。

【讨论】从吉日的一段话说起+寻找WinForm架构的最佳实践

这两天园子里最火的莫过于吉日的白话反射&#xff0c;导致包子的批判&#xff0c;然后引来了老赵的两篇文章&#xff0c;然后又有若干人等一堆反射技术文章出世。可谓百花齐放&#xff0c;百家争鸣啊。喜欢这种氛围&#xff0c;呵呵。 今天我不谈反射&#xff0c;但和反射有关 …

C++猜单词游戏

游戏说明 运行须知&#xff1a; 请将压缩文件中的list.txt englishi.txt people.txt answer.txt放入D盘中的game文件夹中才能正常运行游戏&#xff0c;或者更改文件途径。 输入1可以登录用户&#xff0c;输入2可以游客试玩&#xff0c;用户登录可以查看单词本&#xff0c;里面…

JavaScript工具

在线 JavaScript格式美化工具 http://jsbeautifier.org/ 在线JS调试工具 http://jsbin.com/ 脚本兼容检查 http://www.thefrontside.net/crosscheck Komodo Edit http://www.activestate.com/komodo_edit/ Venkman Development http://www.hacksrus.com/~ginda/venkman/

Oracle分析函数一——函数列表

Oracle 分析函数 Oracle 分析函数——函数列表 SUM &#xff1a; 该函数计算组中表达式的累积和 MIN &#xff1a; 在一个组中的数据窗口中查找表达式的最小值 MAX &#xff1a; 在一个组中的数据窗口中查找表达式的最大值 AVG &#xff1a;…

这次,被游戏玩了

这次&#xff0c;我被游戏玩了 &#xff0c;我的一个朋友在玩台服的未来启示录&#xff0c;看那个场景有点像天堂。他的配置为Intel p43.0, gigabyte 915, DDR400512*2牌子忘记了&#xff0c;但也不错不是杂牌的, maxsun 9500GT TC512高清版&#xff0c;WD160G SATA。玩是…

用MATLAB实现神经网络

一 BP神经网络实现不使用MATLAB神经网络工具箱问题分析MATLAB实现代码运行结果绘制的图像 二 使用MATLAB的神经网络工具箱简易实现BP网络问题分析工具箱中的相关函数一些参考了MATLAB自带的英文手册mapminmax函数newff函数新版本关于nettrainParam的常用属性train函数sim函数 M…

Follow Me:CCIE RS--使用小凡模拟器搭建的CCIE拓扑图

我用小凡模拟器搭建了CCIE LAB 拓扑图有何不对的地方请指正转载于:https://blog.51cto.com/tanfo/216831

【积累】非常全面的开源数据集

非常全面的开源数据集 由skymind.ai公布 非常全面的开源数据集最近新增数据集自然图像数据集地理空间数据人工数据集人脸数据集视频数据集文本数据集问答数据集情感数据集推荐和排名系统语音数据集音符音乐数据集健康 &生物数据政府&统计数据网络和图形其它数据集 最近…

女性最常说的5大谎言:

女性最常说的5大谎言&#xff1a; “没事&#xff0c;我很好” “哦&#xff0c;这不是新买的&#xff0c;买很久了” “这不是很贵” “这是打折时买的” “我正在路上” 我好像都曾听过&#xff01; 摘自&#xff1a;http://lady.163.com/special/00261MPK/kexue004.html

【资源】史上最全数据集汇总

无论是数据挖掘还是目前大热的深度学习&#xff0c;都离不开“大数据”。大公司们一般会有自己的数据&#xff0c;但对于创业公司或是高校老师、学生来说&#xff0c;“Where can I get large datasets open to the public” 是一个不得不面对的问题。 本文将为您提供一个网站/…

1030利用三层交换机实现VLAN间通信

实验相关文件在附件中转载于:https://blog.51cto.com/network0546/219543

非线性最优化(二)——高斯牛顿法和Levengerg-Marquardt迭代

高斯牛顿法和Levengerg-Marquardt迭代都用来解决非线性最小二乘问题(nonlinear least square)。 From Wiki The Gauss–Newton algorithm is a method used to solve non-linear least squares problems. It is a modification of Newtons method for finding a minimum of a …

为右键新建菜单添加内容

右键菜单的内容由注册表关联,通过修改注册表就可以定制鼠标右键快捷菜单中的“新建”菜单所包含的项目。 需要注意的是&#xff0c;在修改注册表以前要先将注册表备份&#xff0c;以免出现问题时无法恢复。 一、增加菜单项目1&#xff0e;首先&#xff0c;决定要增加到菜单中的…

逆透视变换详解 及 代码实现(一)

逆透视变换详解 及 代码实现&#xff08;一&#xff09; 中主要是原理的说明&#xff1a; 一、世界坐标轴和摄像机坐标轴 从下图中可以看到&#xff0c;世界坐标为(X,Y,Z) 相机坐标为(Xc,Yc,Zc) 而世界坐标变换到相机坐标存在一个旋转矩阵变换R以及一个位移变换T。 根据上图…