Ultra-QuickSort POJ - 2299 (逆序对)
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
Output
Sample Input
5
9
1
0
5
4
3
1
2
3
0
Sample Output
6
0 题意:将一个序列从小到大排序,如果只能交换相邻的数,最少需要交换多少次
思路:和冒泡排序一样,一个数需要交换的次数就是它的逆序对数,所以就是求总的逆序对的个数 求逆序对可以用两种方法
①归并排序:
#include<cstdio>
#include<iostream>
using namespace std; int n;
const int maxn = 5e5+;
int num[maxn];
typedef long long ll; ll Mersort(int l,int r)
{
int mid = (l+r)/;
int i=l,j=mid+;
int b[r-l+];
int k=;
ll ans = ;
while(i <= mid && j <= r)
{
if(num[i] <= num[j])
b[k++] = num[i++];
else
b[k++] = num[j++],ans+=mid-i+;
}
while(i <= mid)
{
b[k++] = num[i++];
}
while(j <= r)
{
b[k++] = num[j++];
}
for(int i=l; i<=r; i++)
{
num[i] = b[i-l+];
}
return ans;
} int Merge(int l,int r,ll &ans)
{
int mid = (l+r)/;
if(l == r)
return ;
Merge(l,mid,ans);
Merge(mid+,r,ans);
ans += Mersort(l,r);
}
int main()
{
while(~scanf("%d",&n) && n)
{
for(int i=; i<=n; i++)
scanf("%d",&num[i]);
ll ans = ;
Merge(,n,ans);
printf("%lld\n",ans);
}
}
②树状数组:(要注意离散,离散可以二分,也可以map)
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std; const int maxn = 5e5+;
int n;
int tree[maxn];
typedef long long ll; int lowbit(int x)
{
return x&(-x);
} void add(int x)
{
for(int i=x;i<=n;i+=lowbit(i))
{
tree[i]++;
}
} int Query(int x)
{
int ans = ;
for(int i=x;i>;i-=lowbit(i))
{
ans+=tree[i];
}
return ans;
}
int query(int x,int n,int *b)
{
return lower_bound(b+,b++n,x) - b;
}
int main()
{
while(~scanf("%d",&n) && n)
{
memset(tree,,sizeof(tree));
int a[n+],b[n+];
for(int i=;i<=n;i++)scanf("%d",&a[i]),b[i] = a[i];
sort(b+,b++n);
int len = unique(b+,b++n)-b-;
ll ans = ;
for(int i=;i<=n;i++)
{
int pos = query(a[i],len,b);
add(pos);
ans += pos - - Query(pos-);
}
printf("%lld\n",ans);
}
}
Ultra-QuickSort POJ - 2299 (逆序对)的更多相关文章
- POJ 2299 逆序对
Crossings Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463 Description I ...
- POJ 1804 逆序对数量 / 归并排序
Brainman Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12175 Accepted: 6147 Descrip ...
- poj 2299 逆序数
http://poj.org/problem?id=2299 坑:答案是long long 输出……!!!!! 题意是:求一个数组进行冒泡排序交换的次数 题解:求逆序数 题解Ⅰ: 归并排序求逆序数 归 ...
- poj2299——逆序对
题目:http://poj.org/problem?id=2299 逆序对,注意树状数组维护后缀和. 代码如下: #include<iostream> #include<cstdio ...
- 【POJ】2299 Ultra-QuickSort(逆序对)
http://poj.org/problem?id=2299 在两个元素相同的数列里,其中一个数列要移动到另一个数列相同元素相同的位置,那么要移动的次数就是这个数列关于另一个数列的逆序对数(hash后 ...
- 树状数组求逆序对:POJ 2299、3067
前几天开始看树状数组了,然后开始找题来刷. 首先是 POJ 2299 Ultra-QuickSort: http://poj.org/problem?id=2299 这题是指给你一个无序序列,只能交换 ...
- POJ 2299 Ultra-QuickSort 离散化加树状数组求逆序对
http://poj.org/problem?id=2299 题意:求逆序对 题解:用树状数组.每读入一个数x,另a[x]=1.那么a数列的前缀和s[x]即为x前面(或者说,再x之前读入)小于x的个数 ...
- POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)
POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...
- poj 2299 树状数组求逆序对数+离散化
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 54883 Accepted: 20184 ...
- Poj 2299 - Ultra-QuickSort 离散化,树状数组,逆序对
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 52306 Accepted: 19194 ...
随机推荐
- 纯webpack打包项目
webpack 3 零基础入门教程 http://webpackbook.rails365.net/466996(文本) https://www.rails365.net/movies/webpack ...
- WampServer & XAMPP Configure with MariaDB and MySQL
第一部分补上次的一个问题 1.WampServer 3不支持的硬件格式 FAT3和 exFAT 他只能工作在NTFS的格式硬盘上. 不能在Windows XP上运行. 安装 WampServer 必须 ...
- Python笔记记录
python2和python3的不同: Unicode(统一码.万国码),在3里面可以直接写中文了. python2里rae_input与python3中的input效果一样 在计算机内存中,统一用U ...
- Ubuntu点击dash home就崩溃
很崩溃的一个问题,搞了好久.并没有很清楚的知道具体哪个细节导致的问题,只是大概知道了原因,以及搞出了一个解决方案. 问题描述 台式机,没有独立显卡,也就是只有一个intel CPU在一起的小破显卡(我 ...
- Linux下Nginx安装/启动/重启/停止
Nginx是高性能的web服务器也是非常好用反向代理服务器,可以实现负载均衡,动静分离等策略,在linux下用的非常多.下面是下载地址 http://nginx.org/en/download.h ...
- 18/03/18 04:53:44 WARN TaskSchedulerImpl: Initial job has not accepted any resources; check your cluster UI to ensure that workers are registered and have sufficient resources
1:遇到这个问题是在启动bin/spark-shell以后,然后呢,执行spark实现wordcount的例子的时候出现错误了,如: scala> sc.textFile()).reduceBy ...
- springcloud feign传输List的坑
无法直接传输List 错误方法1: @RequestMapping(value = "/stat/merchant/get_merchant_compare_info", meth ...
- [转] React Hot Loader 3 beta 升级指南
前言 在用 react-hot-loader v1.3 的时候有些深层组件不会很完美的热更新(可能是我使用有问题).然后在 react-hot-loader 首页中看到 React Hot Loade ...
- Centos7编译安装GCC7.2
通常编译的时候可能需要新版本的gcc,本文就说明下基于低版本的gcc升级为gcc7.2 wget 'http://mirrors-usa.go-parts.com/gcc/releases/gcc-7 ...
- JSP基础知识➣Cookie和Session(五)
JSP Cookie 处理 Cookie是存储在客户机的文本文件,它们保存了大量轨迹信息.在servlet技术基础上,JSP显然能够提供对HTTP cookie的支持.JSP脚本通过request对象 ...