博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
nyoj322Sort归并排序
阅读量:1872 次
发布时间:2019-04-26

本文共 1962 字,大约阅读时间需要 6 分钟。

先收藏起来,改日在自己写一篇。
nyoj322Sort
时间限制:1000 ms | 内存限制:65535 KB
难度:4
描述
You want to processe a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. Then how many times it need.
For example, 1 2 3 5 4, we only need one operation : swap 5 and 4.
输入
The input consists of T number of test cases.(<0T<1000) Each case consists of two lines: the first line contains a positive integer n (n <= 1000); the next line contains a permutation of the n integers from 1 to n.
输出
For each case, output the minimum times need to sort it in ascending order on a single line.
样例输入
2
3
1 2 3
4
4 3 2 1
样例输出
0
6

//归并排序//http://blog.csdn.net/yuehailin/article/details/68961304 #include
#include
using namespace std;void mergeArray(int number[], int first, int mid, int last, int temp[]);void mergeSort(int number[], int first, int last, int temp[]);int count;int main() { int t, n, number[1005], temp[1005]; scanf("%d", &t); while (t--) { count = 0; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &number[i]); mergeSort(number, 0, n-1, temp);// for (int i = 0; i < n; i++) printf("%d ", number[i]);// printf("\n"); printf("%d\n", count); } return 0;}void mergeArray(int number[], int first, int mid, int last, int temp[]) { int i = first, j = mid + 1; int position = 0; while (i <= mid && j <= last) { if (number[i] <= number[j]) { temp[position++] = number[i++]; }else { count += mid - i + 1; temp[position++] = number[j++]; } } while (i <= mid) temp[position++] = number[i++]; while (j <= last) temp[position++] = number[j++]; for (i = 0; i < position; i++) number[first+i] = temp[i];}void mergeSort(int number[], int first, int last, int temp[]) { if (first < last) { int mid = (first + last) / 2; mergeSort(number, first, mid, temp); mergeSort(number, mid+1, last, temp); mergeArray(number, first, mid, last, temp); }}
你可能感兴趣的文章
推荐10个腾讯系大佬!第一个从华为离职,写了2本畅销书!
查看>>
ZooKeeper最佳指南--云平台技术栈11
查看>>
2019年美团,滴滴,蘑菇街Java大数据面经分享!
查看>>
一次 MySQL 千万级大表的优化过程
查看>>
深度学习和机器学习_面试问题(二)
查看>>
刚毕业就年薪百万!华为给予八名博士高薪惹争议:值这么多钱吗
查看>>
图解Spark原理及实践----大数据技术栈12
查看>>
我们的每个人的信息,行踪轨迹,正在地下黑市出售....
查看>>
中国人长期“霸榜”GitHub,国外开发者发文控诉
查看>>
他只有2年工作经验,现在却拿着40万年薪,只因他曾做过这件事
查看>>
Storm原理与实践--大数据技术栈14
查看>>
图解支付宝钱包技术架构
查看>>
知乎上40个有趣回复,很精辟
查看>>
RabbitMQ 可靠消息传输实战--云平台技术栈12
查看>>
农村程序员吐槽:虽然挣着2万高薪,但却舍不得吃舍不得穿
查看>>
网恋背后的骗局:那些被宰杀掉的猪!必看!
查看>>
魅族员工哀叹把青春献给了公司,当年如果选择小米,人生会不一样
查看>>
医疗数据自动化
查看>>
12位黄金技术大佬发出警告:一大波必读好书向你袭来!
查看>>
千万级并发!如何设计一个多级缓存系统?
查看>>