Ultra-QuickSort
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 39397   Accepted: 14204

Description

In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence 

9 1 0 5 4 ,


Ultra-QuickSort produces the output 

0 1 4 5 9 .


Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.

Input

The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence
element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.

Output

For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.

Sample Input

  1. 5
  2. 9
  3. 1
  4. 0
  5. 5
  6. 4
  7. 3
  8. 1
  9. 2
  10. 3
  11. 0

Sample Output

  1. 6
  2. 0

归并排序。

另外,此题有一坑就是结果会超int32;

详细能够參考:点击打开链接

我写的代码例如以下:

  1. #include<cstdio>
  2. #include<stdlib.h>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<iostream>
  6.  
  7. using namespace std;
  8.  
  9. const int M = 500000 + 5;
  10. int n, A[M], T[M], i;
  11.  
  12. long long merge_sort(int l, int r, int *A)
  13. {
  14. if (r - l < 1) return 0;
  15. int mid = (l + r) / 2;
  16. long long ans = merge_sort(l, mid, A) + merge_sort(mid + 1, r, A);
  17. i = l;
  18. int p = l, q = mid + 1;
  19. while (p <= mid && q <= r)
  20. {
  21. if(A[p] <= A[q])
  22. T[i++] = A[p++];
  23. else
  24. {
  25. ans += (mid + 1 - p);
  26. T[i++] = A[q++];
  27. }
  28. }
  29. while (p <= mid) T[i++] = A[p++];
  30. while (q <= r) T[i++] = A[q++];
  31. for (int j = l; j <= r; j++)
  32. A[j] = T[j];
  33. return ans;
  34. }
  35.  
  36. int main()
  37. {
  38. int n;
  39. while(scanf("%d", &n) && n)
  40. {
  41. for(int j=0; j<n; j++)
  42. scanf("%d", &A[j]);
  43. printf("%lld\n", merge_sort(0, n - 1, A));
  44. }
  45.  
  46. return 0;
  47. }

POJ 2299:Ultra-QuickSort的更多相关文章

  1. 树状数组求逆序对:POJ 2299、3067

    前几天开始看树状数组了,然后开始找题来刷. 首先是 POJ 2299 Ultra-QuickSort: http://poj.org/problem?id=2299 这题是指给你一个无序序列,只能交换 ...

  2. 逆序数 POJ 2299 Ultra-QuickSort

    题目传送门 /* 题意:就是要求冒泡排序的交换次数. 逆序数:在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序. 一个排列中逆序的总数就称为这个排列的逆 ...

  3. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  4. POJ 2299 【树状数组 离散化】

    题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...

  5. POJ 2299 Ultra-QuickSort(线段树+离散化)

    题目地址:POJ 2299 这题以前用归并排序做过.线段树加上离散化也能够做.一般线段树的话会超时. 这题的数字最大到10^10次方,显然太大,可是能够利用下标,下标总共仅仅有50w.能够从数字大的開 ...

  6. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  7. POJ 3252:Round Numbers

    POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...

  8. 题解报告:poj 2299 Ultra-QuickSort(BIT求逆序数)

    Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...

  9. poj 2299 Ultra-QuickSort :归并排序求逆序数

    点击打开链接 Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 34676   Accepted ...

随机推荐

  1. SharePoint 2013网站突然不能登录了。

    SharePoint 2013网站突然不能登录了,访问的时候,总是报错: The list has not shared with you.   原因: 原来我不知道什么时候把web applicat ...

  2. (算法)Trapping Rain Water II

    题目: Given n * m non-negative integers representing an elevation map 2d where the area of each cell i ...

  3. ZH奶酪:putty远程登录Linux服务器非常慢

    11.pytty远程登录Linux服务器非常慢 http://www.it165.net/os/html/201209/3425.html 12.启动SSHD服务报错 http://blog.chin ...

  4. DataGrid前台数据绑定技巧

    (1)DataGrid控件不换行,数据显示不完全后面加"..." <%# DataBinder.Eval(Container.DataItem,? DataBinder.Ev ...

  5. Visual Studio使用中的问题

    1.后台断点调试,一到断点的时候就VS已停止 原因:安装插件问题,我的由于安装了" .NET Reflector Visual Studio Extension "插件 解决办法: ...

  6. 灰度图像二值化-----c++实现

    前天闲着没事干,就写了写BMP图像处理,感觉大家还比较感兴趣..所以现在没事,继续更新..这次简单的写了灰度图像二值化..这是什么概念呢? 图像的二值化的基本原理 图像的二值化处理就是将图像上的点的灰 ...

  7. Linux对文件内容基本操作(学习笔记七)

    一.cat 1.1.查看文件内容 格式:cat 文件名 [root@model /]# cat /etc/resolv.conf # Generated by NetworkManager names ...

  8. windows XP系统搜索无线网络时提示“windows无法配置此无线连接”,如何处理?

    转自:http://support1.lenovo.com.cn/lenovo/wsi/htmls/detail_12839009034375918.html 文章编号:C191612     201 ...

  9. 如何自定义oauthauthorizationserverprovider错误信息?

    We are using the OAuthAuthorizationServerProvider class to do authorization in our ASP.NET Web Api a ...

  10. DIV+CSS专题:十天学会DIV+CSS

    DIV+CSS专题:十天学会DIV+CSS,在网上看到的.感觉蛮好,推荐一下. 十天学会DIV+CSS(WEB标准)CHM格式文件下载 第十天 div+css网页标准布局实例教程(三) 第十天 div ...