POJ训练计划2299_Ultra-QuickSort(归并排序求逆序数)
| Time Limit: 7000MS | Memory Limit: 65536K | |
| Total Submissions: 39279 | Accepted: 14163 |
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 issorted 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
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
Source
#include <iostream> using namespace std;
long long a[500010],t[500010],cnt=0;
void gbsort(long long *a,long long l,long long r)
{
if(l<r)
{
long long mid=(l+r)/2;
gbsort(a,l,mid);
gbsort(a,mid+1,r);
long long s=l,e=mid+1;
long p=0; while(s<=mid&&e<=r)
{
if(a[s]<=a[e])
{
t[p++]=a[s++];
}
else
{
t[p++]=a[e++];
cnt+=mid-s+1;
}
}
while(s<=mid)
{
t[p++]=a[s++];
}
while(e<=l)
{
t[p++]=a[e++];
}
for(int i=0;i<p;i++)
a[l+i]=t[i];
}
}
int main()
{
int n;
while(cin>>n)
{
if(!n)break;
cnt=0;
for(int i=0; i<n; i++)
cin>>a[i];
gbsort(a,0,n-1);
cout<<cnt<<endl;
}
return 0;
}
POJ训练计划2299_Ultra-QuickSort(归并排序求逆序数)的更多相关文章
- poj 2299 Ultra-QuickSort :归并排序求逆序数
点击打开链接 Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 34676 Accepted ...
- poj 2299 Ultra-QuickSort 归并排序求逆序数对
题目链接: http://poj.org/problem?id=2299 题目描述: 给一个有n(n<=500000)个数的杂乱序列,问:如果用冒泡排序,把这n个数排成升序,需要交换几次? 解题 ...
- [CF 351B]Jeff and Furik[归并排序求逆序数]
题意: 两人游戏, J先走. 给出一个1~n的排列, J选择一对相邻数[题意!!~囧], 交换. F接着走, 扔一硬币, 若正面朝上, 随机选择一对降序排列的相邻数, 交换. 若反面朝上, 随机选择一 ...
- POJ2299 Ultra-QuickSort(归并排序求逆序数)
归并排序求逆序数 Time Limit:7000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descri ...
- HDU 3743 Frosh Week(归并排序求逆序数)
归并排序求逆序数 #include <iostream> #include <cstdio> using namespace std; #define maxn 1000005 ...
- hiho一下 第三十九周 归并排序求逆序数
题目链接:http://hihocoder.com/contest/hiho39/problem/1 ,归并排序求逆序数. 其实这道题也是可以用树状数组来做的,不过数据都比较大,所以要离散化预处理一下 ...
- 题解报告:poj 2299 Ultra-QuickSort(BIT求逆序数)
Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...
- poj 2299 Ultra-QuickSort (归并排序 求逆序数)
题目:http://poj.org/problem?id=2299 这个题目实际就是求逆序数,注意 long long 上白书上的模板 #include <iostream> #inclu ...
- poj2299解题报告(归并排序求逆序数)
POJ 2299,题目链接http://poj.org/problem?id=2299 题意: 给出长度为n的序列,每次只能交换相邻的两个元素,问至少要交换几次才使得该序列为递增序列. 思路: 其实就 ...
随机推荐
- Hadoop运行中遇到的错误调试
在运行Hadoop的过程中遇到的最多的问题就是DataNode不能正常的启动,各种问题都有可能,说一下我遇到的两种情况: (1)第一种情况是Master的防火墙没有关闭.这样在启动Hadoop的时候, ...
- Android发送数据到web服务器4种方式
1./** 2. * Android中向web服务器提交数据的两种方式四种方法 3. */ 4.public class SubmitDataByHttpClientAndOrdinaryWay { ...
- DFS(White-Gray-Black)
参考<数据结构与算法> 本书在复杂深度优先遍历图时,采用三种颜色标记图中节点 1 white 表示未访问 2 gray 表示已经正在访问,其相邻节点 3 black 表示该节点所有的相邻节 ...
- python优秀库 - 使用envelopes发送邮件
这里有一个使用python自带lib发送邮件的例子(http://my.oschina.net/leejun2005/blog/74416),这里面讲解的很全面,可以供大家参考. 今天将的是使用env ...
- window下nodejs安装指南
相信对于很多关注javascript发展的同学来说,nodejs已经不是一个陌生的词眼.有关nodejs的相关资料网上已经铺天盖地.由于它的高并发特性,造就了其特殊的应用地位. 国内目前关注最高,维护 ...
- Qt见解:Post 与 Get 的区别(Get将参数直接与网址整合为一个整体,而Post则将其拆为两个部分)
第一次接触Qt的Http项目,今天看了一下Post和Get的基本使用方法,就开始尝试了.原先以为Post专门用于向服务器发送请求,然后接收服务器应答的: 而Get只是单纯从服务器获取资源,比如下载这个 ...
- Collection用法
Queue接口与List.Set同一级别,都是继承了Collection接口.LinkedList实现了Queue接 口.在队列这种数据结构中,最先插入的元素将是最先被删除的元素:反之最后插入的元素将 ...
- eclipse 部分颜色及部分字体设置
eclipse整体代码的颜色风格可以用插件 eclipse color theme 更改. 但尽管如此,有些颜色仍不是最满意的,还需自己设计. 1. 光标选中字体的颜色,如图 一个openItem被选 ...
- AFNetworking 进行网络监测
AFNetworking 进行网络监测 引入头文件,创建检测判断BOOL值 // 网络请求的头文件 #import <AFNetworking.h> @interface ViewCont ...
- Problem 2169 shadow
Problem 2169 shadow Accept: 141 Submit: 421 Time Limit: 1000 mSec Memory Limit : 32768 KB Pr ...