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 ...
随机推荐
- matlab 读取文件(mat)存储为json文件
fid= fopen('reqJosn.json', 'w+'); load('request-set-10.mat'); requests = requests.request; requestNu ...
- 旧文备份:AVR读写EEPROM分析
由于AVR的EEPROM写周期比较长(一般为毫秒级),因此在编程使用过程中要特别注意.对于读EEPROM没什么好说的,读一个字节的数据要耗费4个时钟周期,可以忍受,写就比较麻烦了,虽然放在EEPROM ...
- BeanNameAware接口和BeanFactoryAware接口(转)
迄今为止,所接触到的Bean都是“无知觉”的,就像黑客帝国中机械工厂里面“养殖”的人类,他们虽然能完成一定的功能,但是根本不知道自己在工厂(BeanFactory)中的代号(id) ...
- Target runtime Apache Tomcat v8.5 is not defined.
Target runtime Apache Tomcat v8.5(或者其它版本) is not defined. 这个错误通常是在从文件夹中导入别人的项目的时候发生,因为 在 .setting 中有 ...
- JAVA / MySql 编程——第二章 初始MySQL
1. MySQL: ● MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle公司. MySQL最流行的关系型数据库管理系统, ...
- 日期格式兼容iOS
iOS不支持2016-02-11 12:21:12格式的日期 目前Safari可以支持的标准格式: MM-dd-yyyy yyyy/MM/dd MM/dd/yyyy MMMM dd, yyyy MMM ...
- 编辑工具_vi
vi/vim平时经常会用到,但是一直没有时间系统的整理下,今天看到了一篇不错的介绍文章.引用下,就当做笔记了,但是不晓得该怎么填引用路径,如有侵权请告知,补上引用路径 01. vi 简介 1.1 学习 ...
- JRE和JDK区别
JRE: Java Runtime Environment JDK:Java Development Kit JRE顾名思义是java运行时环境, 包含了java虚拟机,java基础类库. 是使用ja ...
- iOS-UICollectionViewController 介绍
废话不多说,列几个列子 (几种情况下的做法): 情景一: 介绍:1. 在UIViewController 上加 UICollectionView (用代码 创建 UICollectionView). ...
- mysql sum 为 0 的解决方法
使用SQL语句SUM函数的时候,默认查询没有值的情况下返回的是null,而实际可能我们要用的是返回0. 解决方法:SELECT SUM(count) FROM test_table 改成: SELEC ...