poj2299 Ultra-QuickSort(线段树求逆序对)
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<queue>
#include<string>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define lson root<<1
#define rson root<<1|1
#define hi puts("hi!");
using namespace std; struct node
{
int kd,val;
}a[]; int n,m,cnt[];
long long tr[]; bool cmp(node a,node b)
{
return a.val<b.val;
} void push_up(int root)
{
tr[root]=tr[lson]+tr[rson];
} void build(int root,int l,int r)
{
if(l==r)
{
tr[root]=;
return;
}
int mid=(l+r)>>;
build(lson,l,mid);
build(rson,mid+,r);
push_up(root);
} void add(int root,int l,int r,int x,int p)
{ if(l==r)
{
tr[root]=;
return;
}
int mid=(l+r)>>;
if(p<=mid)
{
add(lson,l,mid,x,p);
}
if(p>mid)
{
add(rson,mid+,r,x,p);
}
push_up(root);
} long long query(int root,int l,int r,int x,int y)
{
long long ans=;
if(x<=l&&y>=r)
{
return tr[root];
}
int mid=(l+r)>>;
if(x<=mid)
{
ans+=query(lson,l,mid,x,y);
}
if(y>mid)
{
ans+=query(rson,mid+,r,x,y);
}
return ans;
} int main()
{
while(scanf("%d",&n)==&&n)
{
long long ans1=;
memset(tr,,sizeof(tr));
build(,,n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i].val);
a[i].kd=i;
}
sort(a+,a+n+,cmp);
for(int i=;i<=n;i++)
{
cnt[a[i].kd]=i;
}
for(int i=n;i>=;i--)
{
ans1+=query(,,n,,cnt[i]);
add(,,n,,cnt[i]);
}
printf("%lld\n",ans1);
}
}
poj2299 Ultra-QuickSort(线段树求逆序对)的更多相关文章
- 4163 hzwer与逆序对 (codevs + 权值线段树 + 求逆序对)
题目链接:http://codevs.cn/problem/4163/ 题目:
- BNU 2418 Ultra-QuickSort (线段树求逆序对)
题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=2418 解题报告:就是给你n个数,然后让你求这个数列的逆序对是多少?题目中n的范围是n & ...
- HDU 4911 http://acm.hdu.edu.cn/showproblem.php?pid=4911(线段树求逆序对)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911 解题报告: 给出一个长度为n的序列,然后给出一个k,要你求最多做k次相邻的数字交换后,逆序数最少 ...
- SGU 180 Inversions(离散化 + 线段树求逆序对)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=180 解题报告:一个裸的求逆序对的题,离散化+线段树,也可以用离散化+树状数组.因为 ...
- hdu1394(线段树求逆序对)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 线段树功能:update:单点增减 query:区间求和 分析:如果是0到n-1的排列,那么如果 ...
- HDU 1394 线段树求逆序对
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- FZU2018级算法第五次作业 m_sort(归并排序或线段树求逆序对)
首先对某人在未经冰少允许情况下登录冰少账号原模原样复制其代码并且直接提交的赤裸裸剽窃行为,并且最终被评为优秀作业提出抗议! 题目大意: 给一个数组含n个数(1<=n<=5e5),求使用冒泡 ...
- POJ 2188线段树求逆序对
题目给的输入是大坑,算法倒是很简单-- 输入的是绳子的编号wire ID,而不是上(或下)挂钩对应下(或上)挂钩的编号. 所以要转换编号,转换成挂钩的顺序,然后再求逆序数. 知道了这个以后直接乱搞就可 ...
- HDU 1394 Minimum Inversion Number(线段树求逆序对)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1394 解题报告:给出一个序列,求出这个序列的逆序数,然后依次将第一个数移动到最后一位,求在这个过程中 ...
随机推荐
- form表单中name和id区别
HTML文本是由HTML命令组成的描述性文本,HTML命令可以说明文字.图形.动画.声音.表格.链接等.HTML的结构包括头部(Head).主体(Body)两大部分,其中头部描述浏览器所需的信息,而主 ...
- 三种实现Ajax的方式
本文主要是比较三种实现Ajax的方式 1. prototype.js 2. jquery1.3.2.min.js 3. json2.js Java代码 收藏代码 后台处理程序(Servlet),访问路 ...
- 关于Android Studio上得处女座福音功能——reformat code
在mac上,选中需要的代码,然后 Option+(shift) + Command + L 全部重新排列!!爽飞!
- strtotime出现时区问题不一致的解决方法
学习源头:https://blog.csdn.net/longjuanfengzc/article/details/80622842 https://segmentfault.com/q/101000 ...
- Linux环境下安装XAMPP的PHP的PDF扩展
安装pdf扩展1. wget http://pecl.php.net/get/pdflib-4.1.2.tgz2. tar zxvf pdflib-4.1.2.tgz3. cd pdflib-4.1. ...
- java中二维数组求最大值代码。。。。。。不懂呀
总结:二维数组理解不了,,,,求解析... package com.c2; public class Aaa { // 求数组元素中最大的 public static void main(String ...
- 内核中的内存申请:kmalloc、vmalloc、kzalloc、get_free_pages 之间的区别
kmalloc vmalloc kzalloc get_free_page()是内核空间申请内存空间函数 malloc是用户空间申请内存函数 一 ,kmalloc() 与 kfree() ...
- Python内置函数二 (递归函数,匿名函数,二分法)
匿名函数 lambda() 语法: lambad 参数 : 返回值 def func(a,b): return a * b print(func(2,5)) a = lambda a ,b : a* ...
- javascript中的类方法、构造方法、原型方法的对比
如果你已经接触js很久了,那么应该可以看看我总结的是否正确,如果你刚开始学习,那么通过我的总结,你可以更快的区别他们,记得我刚接触js时,这一块反正是模糊了很久! 1,长相的区别: function ...
- BurpSuite—-decoder模块(编码模块)
一.简介 Burp Decoder是Burp Suite中一款编码解码工具,将原始数据转换成各种编码和哈希表的简单工具,它能够智能地识别多种编码格式采用启发式技术. 二.模块说明 通过有请求的任意模块 ...