POJ 2299:Ultra-QuickSort
Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 39397 | Accepted: 14204 |
Description
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
element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.
Output
Sample Input
- 5
- 9
- 1
- 0
- 5
- 4
- 3
- 1
- 2
- 3
- 0
Sample Output
- 6
- 0
归并排序。
另外,此题有一坑就是结果会超int32;
详细能够參考:点击打开链接
我写的代码例如以下:
- #include<cstdio>
- #include<stdlib.h>
- #include<cstring>
- #include<algorithm>
- #include<iostream>
- using namespace std;
- const int M = 500000 + 5;
- int n, A[M], T[M], i;
- long long merge_sort(int l, int r, int *A)
- {
- if (r - l < 1) return 0;
- int mid = (l + r) / 2;
- long long ans = merge_sort(l, mid, A) + merge_sort(mid + 1, r, A);
- i = l;
- int p = l, q = mid + 1;
- while (p <= mid && q <= r)
- {
- if(A[p] <= A[q])
- T[i++] = A[p++];
- else
- {
- ans += (mid + 1 - p);
- T[i++] = A[q++];
- }
- }
- while (p <= mid) T[i++] = A[p++];
- while (q <= r) T[i++] = A[q++];
- for (int j = l; j <= r; j++)
- A[j] = T[j];
- return ans;
- }
- int main()
- {
- int n;
- while(scanf("%d", &n) && n)
- {
- for(int j=0; j<n; j++)
- scanf("%d", &A[j]);
- printf("%lld\n", merge_sort(0, n - 1, A));
- }
- return 0;
- }
POJ 2299:Ultra-QuickSort的更多相关文章
- 树状数组求逆序对:POJ 2299、3067
前几天开始看树状数组了,然后开始找题来刷. 首先是 POJ 2299 Ultra-QuickSort: http://poj.org/problem?id=2299 这题是指给你一个无序序列,只能交换 ...
- 逆序数 POJ 2299 Ultra-QuickSort
题目传送门 /* 题意:就是要求冒泡排序的交换次数. 逆序数:在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序. 一个排列中逆序的总数就称为这个排列的逆 ...
- POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)
POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...
- POJ 2299 【树状数组 离散化】
题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...
- POJ 2299 Ultra-QuickSort(线段树+离散化)
题目地址:POJ 2299 这题以前用归并排序做过.线段树加上离散化也能够做.一般线段树的话会超时. 这题的数字最大到10^10次方,显然太大,可是能够利用下标,下标总共仅仅有50w.能够从数字大的開 ...
- 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 ...
- POJ 3252:Round Numbers
POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...
- 题解报告:poj 2299 Ultra-QuickSort(BIT求逆序数)
Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...
- poj 2299 Ultra-QuickSort :归并排序求逆序数
点击打开链接 Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 34676 Accepted ...
随机推荐
- SharePoint 2013网站突然不能登录了。
SharePoint 2013网站突然不能登录了,访问的时候,总是报错: The list has not shared with you. 原因: 原来我不知道什么时候把web applicat ...
- (算法)Trapping Rain Water II
题目: Given n * m non-negative integers representing an elevation map 2d where the area of each cell i ...
- ZH奶酪:putty远程登录Linux服务器非常慢
11.pytty远程登录Linux服务器非常慢 http://www.it165.net/os/html/201209/3425.html 12.启动SSHD服务报错 http://blog.chin ...
- DataGrid前台数据绑定技巧
(1)DataGrid控件不换行,数据显示不完全后面加"..." <%# DataBinder.Eval(Container.DataItem,? DataBinder.Ev ...
- Visual Studio使用中的问题
1.后台断点调试,一到断点的时候就VS已停止 原因:安装插件问题,我的由于安装了" .NET Reflector Visual Studio Extension "插件 解决办法: ...
- 灰度图像二值化-----c++实现
前天闲着没事干,就写了写BMP图像处理,感觉大家还比较感兴趣..所以现在没事,继续更新..这次简单的写了灰度图像二值化..这是什么概念呢? 图像的二值化的基本原理 图像的二值化处理就是将图像上的点的灰 ...
- Linux对文件内容基本操作(学习笔记七)
一.cat 1.1.查看文件内容 格式:cat 文件名 [root@model /]# cat /etc/resolv.conf # Generated by NetworkManager names ...
- windows XP系统搜索无线网络时提示“windows无法配置此无线连接”,如何处理?
转自:http://support1.lenovo.com.cn/lenovo/wsi/htmls/detail_12839009034375918.html 文章编号:C191612 201 ...
- 如何自定义oauthauthorizationserverprovider错误信息?
We are using the OAuthAuthorizationServerProvider class to do authorization in our ASP.NET Web Api a ...
- DIV+CSS专题:十天学会DIV+CSS
DIV+CSS专题:十天学会DIV+CSS,在网上看到的.感觉蛮好,推荐一下. 十天学会DIV+CSS(WEB标准)CHM格式文件下载 第十天 div+css网页标准布局实例教程(三) 第十天 div ...