poj 2299 归并排序求逆序数 (可做模板)
Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 48077 | Accepted: 17533 |
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
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
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
5
9
1
0
5
4
3
1
2
3
0
Sample Output
6
0
Source
#include<string.h>
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std; long long sum;
int temp[]; void sort2(int a[],int l,int mid,int r){
// memset(temp,0,sizeof(temp));
int i=l,j=mid+,k=;
while(i<=mid&&j<=r){
if(a[i]<a[j]){
temp[k++]=a[i++];
}
else{
temp[k++]=a[j++];
sum+=mid-i+;
}
}
while(i<=mid) temp[k++]=a[i++];
while(j<=r) temp[k++]=a[j++]; for(int i=l,k=;i<=r;k++,i++)
a[i]=temp[k]; } void sort1(int a[],int l,int r){
int mid;
if(l<r){
mid=(l+r)/;
sort1(a,l,mid);
sort1(a,mid+,r);
sort2(a,l,mid,r);
}
}
int main(){
int n;
int a[];
while(scanf("%d",&n)!=EOF){ if(n==)
break; sum=;
for(int i=;i<n;i++)
scanf("%d",&a[i]);
sort1(a,,n-);
printf("%lld\n",sum); }
return ;
}
poj 2299 归并排序求逆序数 (可做模板)的更多相关文章
- POJ 2299 Ultra-QuickSort 求逆序数 (归并或者数状数组)此题为树状数组入门题!!!
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 70674 Accepted: 26538 ...
- Ultra-QuickSort - poj 2299 (归并排序+统计逆序数)
利用归并排序统计逆序数,利用归并求逆序在对子序列s1和s2在归并时(s1,s2已经排好序),若s1[i]>s2[j](逆序状况),则逆序数加上s1.length-i,因为s1中i后面的数字对于s ...
- POJ 2299 Ultra-QuickSort 求逆序数 线段树或树状数组 离散化
我用的线段树写的. num数组表示已插入的数值的个数. 由于a[i]数值很大,但是n不是很大,所以要离散化处理 9 1 0 5 4 离散化后 4 1 0 3 2 这样保证最大值不会超过n #inclu ...
- poj 2299 Ultra-QuickSort 归并排序求逆序数对
题目链接: http://poj.org/problem?id=2299 题目描述: 给一个有n(n<=500000)个数的杂乱序列,问:如果用冒泡排序,把这n个数排成升序,需要交换几次? 解题 ...
- poj 2299 Ultra-QuickSort :归并排序求逆序数
点击打开链接 Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 34676 Accepted ...
- hiho一下 第三十九周 归并排序求逆序数
题目链接:http://hihocoder.com/contest/hiho39/problem/1 ,归并排序求逆序数. 其实这道题也是可以用树状数组来做的,不过数据都比较大,所以要离散化预处理一下 ...
- [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 ...
随机推荐
- 激活SQLPrompt7.4及以上版本
SQLPrompt是个很好的sql server提示工具,SQL-SERVER2016只能使用SQLPrompt7.x 以上版本,用激活工具激活后没多久就会检测到激活码不可用.所以感觉是不是有个服务在 ...
- 前端面试题二(来自前端网http://www.qdfuns.com/notes/23515/fa8b1e788ac39b04108fc33e5b543c4a.html)
HTML&CSS 1.请描述一下 cookies,sessionStorage 和 localStorage 的区别? cookie是网站为了标示用户身份而储存在用户本地终端(Client S ...
- $.ajax()与$.post()区别
当使用$.ajax时: var name = $('#txtUserName').val(); var pwd = $('#txtPassWord').val(); var param = " ...
- ES6初识-Symbol
Symbol的概念 变量是独一无二的 let a1=Symbol(); let a2=Symbol(); a1和a2严格意义不相等 let a3=Symbol.for('a3'); let a4=Sy ...
- c++标准之IO库
1.面向对象的标准库 2.多种IO标准库工具 istream,提供输入操作 ostream,提供输出操作 cin:读入标准输入的istream对象.全局对象extern std::istream ci ...
- spring-AspectJ
动态代理 ProxyFactoryBean织入切面数量太多不利于围护 BeanNameAutoProxyCreater-------------根据Bean名称创建代理 DefaultAdvisorA ...
- Percona-Tookit工具包之pt-mext
Preface We are always obliged to analyze many outputs generated by various tools directly ev ...
- python 一些基础知识
Python 注释的原理: 原理:根据对象的引用计数器,对象创建会给对象一个引用计数器属性.如果该属性的值为0,那么该对象会被释放.创建一个字符串对象,但是没有任何引用,计数器为0. Python小整 ...
- php结合redis实现高并发下的抢购、秒杀功能【转】
抢购.秒杀是如今很常见的一个应用场景,主要需要解决的问题有两个:1 高并发对数据库产生的压力2 竞争状态下如何解决库存的正确减少("超卖"问题)对于第一个问题,已经很容易想到用缓存 ...
- python学习之控制流1
配置环境:python 3.6 python编辑器:pycharm 代码如下: #!/usr/bin/env python #-*- coding: utf-8 -*- # 控制流: # 1.布尔值: ...