poj 2299 Ultra-QuickSort(求逆序对)
| Time Limit: 7000MS | Memory Limit: 65536K | |
| Total Submissions: 52778 | Accepted: 19348 |
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 is sorted 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
Output
Sample Input
5
9
1
0
5
4
3
1
2
3
0
Sample Output
6
0 题意:给一组数,每次只能交换相邻两个数的位置,问最少交换多少次,使得这个序列从小到大排序;
题解:求逆序数的对数 线段树:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define LL long long
#define MAX 500500
using namespace std;
int n,m;
struct node
{
int val,pos;
}num[MAX];
int sum[MAX<<2];
void pushup(int o)
{
sum[o]=sum[o<<1]+sum[o<<1|1];
}
void gettree(int o,int l,int r)
{
sum[o]=0;
if(l==r)
return ;
int mid=(l+r)>>1;
gettree(o<<1,l,mid);
gettree(o<<1|1,mid+1,r);
pushup(o);
} void update(int o,int l,int r,int pos)
{
if(l==r)
{
sum[o]+=1;
return ;
}
int mid=(l+r)>>1;
if(pos<=mid) update(o<<1,l,mid,pos);
else update(o<<1|1,mid+1,r,pos);
pushup(o);
}
int find(int o,int l,int r,int L,int R)
{
if(L<=l&&R>=r)
return sum[o];
int mid=(l+r)>>1;
int ans=0;
if(L<=mid) ans+=find(o<<1,l,mid,L,R);
if(R>mid) ans+=find(o<<1|1,mid+1,r,L,R);
return ans;
}
bool cmp(node a,node b)
{
if(a.val!=b.val) return a.val<b.val;
else return a.pos<b.pos;
}
int main()
{
int j,i,t,k;
while(scanf("%d",&n),n)
{
gettree(1,1,n);
for(i=1;i<=n;i++)
{
scanf("%d",&num[i].val);
num[i].pos=i;
}
sort(num+1,num+1+n,cmp);
LL ant=0;
for(i=1;i<=n;i++)
{
update(1,1,n,num[i].pos);
ant+=i-find(1,1,n,1,num[i].pos);
}
printf("%lld\n",ant);
}
return 0;
}
树状数组:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define LL long long
#define MAX 500500
using namespace std;
int n,m;
int c[MAX<<1];
struct node
{
int val,pos;
}num[MAX];
bool cmp(node a,node b)
{
if(a.val!=b.val)
return a.val<b.val;
else
return a.pos<b.pos;
}
void update(int x)
{
while(x<=n)
{
c[x]+=1;
x+=x&-x;
}
}
LL sum(int x)
{
LL ans=0;
while(x>=1)
{
ans+=c[x];
x-=x&-x;
}
return ans;
}
int main()
{
int j,i,t,k;
while(scanf("%d",&n),n)
{
for(i=1;i<=n;i++)
{
scanf("%d",&num[i].val);
num[i].pos=i;
}
sort(num+1,num+n+1,cmp);
memset(c,0,sizeof(c));
LL ant=0;
for(i=1;i<=n;i++)
{
update(num[i].pos);
ant+=i-sum(num[i].pos);
}
printf("%lld\n",ant);
}
return 0;
}
poj 2299 Ultra-QuickSort(求逆序对)的更多相关文章
- poj 2299 Ultra-QuickSort (归并排序 求逆序数)
题目:http://poj.org/problem?id=2299 这个题目实际就是求逆序数,注意 long long 上白书上的模板 #include <iostream> #inclu ...
- poj 2299 Ultra-QuickSort 归并排序求逆序数对
题目链接: http://poj.org/problem?id=2299 题目描述: 给一个有n(n<=500000)个数的杂乱序列,问:如果用冒泡排序,把这n个数排成升序,需要交换几次? 解题 ...
- POJ 2188线段树求逆序对
题目给的输入是大坑,算法倒是很简单-- 输入的是绳子的编号wire ID,而不是上(或下)挂钩对应下(或上)挂钩的编号. 所以要转换编号,转换成挂钩的顺序,然后再求逆序数. 知道了这个以后直接乱搞就可 ...
- Poj 2299 Ultra-QuickSort(归并排序求逆序数)
一.题意 给定数组,求交换几次相邻元素能是数组有序. 二.题解 刚开始以为是水题,心想这不就是简单的冒泡排序么.但是毫无疑问地超时了,因为题目中n<500000,而冒泡排序总的平均时间复杂度为, ...
- POJ 2299 Ultra-QuickSort 离散化加树状数组求逆序对
http://poj.org/problem?id=2299 题意:求逆序对 题解:用树状数组.每读入一个数x,另a[x]=1.那么a数列的前缀和s[x]即为x前面(或者说,再x之前读入)小于x的个数 ...
- 树状数组求逆序对:POJ 2299、3067
前几天开始看树状数组了,然后开始找题来刷. 首先是 POJ 2299 Ultra-QuickSort: http://poj.org/problem?id=2299 这题是指给你一个无序序列,只能交换 ...
- POJ 2299树状数组求逆序对
求逆序对最常用的方法就是树状数组了,确实,树状数组是非常优秀的一种算法.在做POJ2299时,接触到了这个算法,理解起来还是有一定难度的,那么下面我就总结一下思路: 首先:因为题目中a[i]可以到99 ...
- Ultra-QuickSort POJ - 2299 树状数组求逆序对
In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a seque ...
- POJ 2299 求逆序对个数 归并排序 Or数据结构
题意: 求逆序对个数 没有重复数字 线段树实现: 离散化. 单点修改,区间求和 // by SiriusRen #include <cstdio> #include <cstring ...
随机推荐
- 使用tdcss.js轻松制作自己的style guide
http://jakobloekke.github.io/tdcss.js/ 在前端开发中,如果能够有一个style guide对于设计来说就显得专业稳定,一致性.在上述链接中,有一个tdcss.js ...
- 【笨嘴拙舌WINDOWS】剪切板
Windows剪贴板是一种比较简单同时也是开销比较小的IPC(InterProcess Communication,进程间通讯)机制.Windows系统支持剪贴板IPC的基本机制是由系统预留的一块全局 ...
- mokoid android open source HAL hacking in a picture
/************************************************************************** * mokoid android HAL hac ...
- Web Api 在线参考文档
参考文档: https://developer.mozilla.org/zh-CN/docs/Web/API
- Servlet容器的启动(Tomcat为例)
一.容器简介 在tomcat容器等级中,context容器直接管理servlet在容器中的包装类Wrapper,所以Context容器如何运行将直接影响servlet的工作方式. tomcat容器模型 ...
- MAC OSX 下安装Cscope
续前文,搞定CTAGS之后,需要被搞定的是cscope,依旧是上网拖一把,具体过程如下 #1 下载cscope最新版本 http://cscope.sourceforge.net/#downloa ...
- Mysql导入导出工具Mysqldump和Source命令用法详解
Mysql本身提供了命令行导出工具Mysqldump和Mysql Source导入命令进行SQL数据导入导出工作,通过Mysql命令行导出工具Mysqldump命令能够将Mysql数据导出为文本格式( ...
- Android应用性能优化之使用SQLiteStatement优化SQLite操作
平常在做Android数据库操作时,都是用的execSQL之个方法. 今天偶然发现了SQLiteStatement这个类.让我想起了在做Java Web开发写JDBC的代码时Prestatement这 ...
- Eclipse c++环境搭建 并加载OpenCV库 2015最新
C++: 搜索 Eclipse c++ 即可 注意新版的mingw安装器,要安装: 1.mingw-developer-toolkit 2.mingw32-base 3.mingw32-gcc-g++ ...
- Spring3.0将全面支持REST
Rod Johnson上个月底说,Spring 3.0全面支持REST风格的Web服务. "We're really seeing extensive interest and growth ...