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 ...
随机推荐
- Subversion简介
作为一名编程人员,SVN经常作为代码.项目的版本控制,殊不知SVN也可作为其他领域的版本控制,例如对文档.音频.视频等 . SVN可以看成一种文件系统,为了使工作人员提高工作效率,可以进行并行的工作, ...
- Oracle必备知识.
一.基本数据类型 1.字符数据类型 char varchar2 long 三者区别: I.char固定长度字符串,存储字母数字值,列长度可以是 1 到 2000 个字节. II.varc ...
- Webpack4 学习笔记二 CSS模块转换
前言 此内容是个人学习笔记,以便日后翻阅.非教程,如有错误还请指出 webpack 打包css模块 webpack是js模块打包器, 如果在入口文件引入css文件或其它的less.sass等文件,需要 ...
- Open closed principle
#include <iostream> using namespace std; class Book { public: string getContents() { return &q ...
- 一张思维导图带你梳理HashMap相关知识
HashMap可以说是java中最常见也是最重要的key-value存储结构类,很多程序员可能经常用,但是不一定清楚这个类背后的数据结构和相关操作原理,为了复习HashMap相关的知识,今天花了一天的 ...
- linux mysql5.7 安装、 开机启动
一.安装 wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz h ...
- php-5.6.26源代码 - opcode处理器,“函数调用opcode”处理器,如何调用扩展模块的函数
// opcode处理器 --- ZEND_DO_FCALL_SPEC_CONST_HANDLER实现在 php-5.6.26\Zend\zend_vm_execute.h static int ZE ...
- day1_作业(账户登录检测)
#!/usr/local/bin/python3 # -*- coding:utf-8 -*- f=open('/users/zhangyu/PycharmProjects/s14/day1/Home ...
- Reward HDU - 2647
传送门 Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to dis ...
- ELK之Elasticsearch
安装并运行Elasetisearch cd elasticsearch-<version> ./bin/elasticsearch 如果你想把 Elasticsearch 作为一个守护进程 ...