<Sicily>Inversion Number(线段树求逆序数)
一、题目描述
There is a permutation P with n integers from 1 to n. You have to calculate its inversion number, which is the number of pairs of Pi and Pj satisfying the following conditions: iPj.
二、输入
The input may contain several test cases.
In each test case, the first line contains a positive integer n (n<=100000) indicating the number of elements in P. The following n lines contain n integers making the permutation P.
Input is terminated by EOF.
三、输出
For each test case, output a line containing the inversion number.
四、解题思路
题意:求逆序
思路:
1.对整个输入序列,从前往后扫,统计比a[i]小的,在a[i]后面的有多少个,进行累加。这种方法是暴力做法。复制度为n^2。
2)统计a[i]前面的比它大的数
这样利用输入时,已经知道前面的序列,每输入一个数,就把这个数的num[i]值加1,然后统计比这个数大的数的num和,因为这里的和一定是在这个数列中比a[i]大,且在它前面出现的数之和,再把这个和加到总逆序数sum里。
3)在统计比这个数大的数的num和这一步,可以进行优化处理,使用线段树求区间域值,时间复杂度是logn,所以总体复杂度就降到了nlogn。
4)线段树的图如下:
附:这道题也可以使用归并排序的方法。
五、代码
#include<iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAX_NUM = 110000;
struct Node
{
int lChild, rChild;
int num;
}segTree[MAX_NUM*3];
void Build(int lChild, int rChild, int i)
{
segTree[i].lChild = lChild;
segTree[i].rChild = rChild;
segTree[i].num = 0;
if(lChild == rChild)return;
int mid = (lChild + rChild) >> 1;
Build(lChild, mid, i << 1);
Build(mid + 1, rChild, i + i + 1);
}
void update(int t, int o)
{
if(segTree[o].lChild == segTree[o].rChild && segTree[o].lChild == t)
{
segTree[o].num++;
return;
}
int mid = (segTree[o].lChild + segTree[o].rChild) >> 1;
if(t > mid) update(t, o + o +1);
else update(t, o + o);
segTree[o].num = segTree[o+o].num + segTree[o + o + 1].num;
}
int query(int lChild, int rChild, int i)
{
if(segTree[i].lChild == lChild && segTree[i].rChild == rChild)
return segTree[i].num;
int mid = (segTree[i].lChild + segTree[i].rChild) >> 1;
if(rChild <= mid) return query(lChild, rChild, i + i);
else if(lChild > mid) return query(lChild, rChild, i + i + 1);
else return query(lChild, mid, i * 2) + query(mid+1, rChild, i * 2 + 1);
}
int main()
{
int num;
while(cin >> num)
{
int dataArray[MAX_NUM];
int inputData;
for(int i = 0; i < num; i++)
{
cin >> inputData;
dataArray[i] = inputData;
}
Build(1, num, 1);
long long result = 0;
for(int i = 0; i < num; i++)
{
result += query(dataArray[i], num, 1);
update(dataArray[i], 1);
}
cout << result << endl;
}
return 0;
}
<Sicily>Inversion Number(线段树求逆序数)的更多相关文章
- [HDU] 1394 Minimum Inversion Number [线段树求逆序数]
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- HDU_1394_Minimum Inversion Number_线段树求逆序数
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- hdu1394--Minimum Inversion Number(线段树求逆序数,纯为练习)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- HDU-1394 Minimum Inversion Number(线段树求逆序数)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- 线段树求逆序数方法 HDU1394&&POJ2299
为什么线段树能够求逆序数? 给一个简单的序列 9 5 3 他的逆序数是3 首先要求一个逆序数有两种方式:能够从头開始往后找比当前元素小的值,也能够从后往前找比当前元素大的值,有几个逆序数就是几. 线段 ...
- hdu 1394 (线段树求逆序数)
<题目链接> 题意描述: 给你一个有0--n-1数字组成的序列,然后进行这样的操作,每次将最前面一个元素放到最后面去会得到一个序列,那么这样就形成了n个序列,那么每个序列都有一个逆序数,找 ...
- HDU - 1394 Minimum Inversion Number (线段树求逆序数)
Description The inversion number of a given number sequence a1, a2, ..., an is the number of pairs ( ...
- hdu1394 Minimum Inversion Number (线段树求逆序数&&思维)
题目传送门 Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- 【线段树求逆序数】【HDU1394】Minimum Inversion Number
题目大意: 随机给你全排列中的一个,但不断的把第一个数丢到最后去,重复N次,形成了N个排列,问你这N个排列中逆序数最小为多少 做法: 逆序数裸的是N^2 利用线段树可以降到NlogN 具体方法是插入一 ...
随机推荐
- VBA 第一天
公司实习第一天,excel搞不定啊,学点VBA留着用: 录制宏: 点击录制宏按钮以后,在这段期间你做的每一个操作都会被记录下来,直到你点击停止录制按钮才能够停下,停下来后在此期间每一个操作都会以宏代码 ...
- MHA+ProxySQL 读写分离高可用
文档结构如下: 1.ProxySQL说明 ProxySQL是mysql的一款中间件的产品,是灵活的mysql代理层,可以实现读写分离,支持query路由器的功能,支持动态指定sql进行缓存,支持动态加 ...
- 移动端1px细线问题
1可以用伪类实现 .con{position: relative;.con:before { content: " "; position: absolute; left: 0; ...
- Batch脚本获取日期SET YEAR=%date:~10,4%
在batch脚本中我们可以通过下面的语句来对日期进行操作: SET YEAR=%date:~10,4% SET MONTH=%date:~4,2% SET DAY=%date:~7,2% SET HO ...
- 【转】IIS初始化(预加载),解决第一次访问慢,程序池被回收问题
原地址:http://www.debugrun.com/a/mpyWXwg.html 读在最前面: 1.本文以IIS8,Windows Server 2012R2做为案例 2.IIS8 运行在 Win ...
- Django shortcut functions
django.shortcuts package提供提供帮助类和函数可以更便捷的操作MVC中的每一部分,包含: render(request, template_name,[dictionary],[ ...
- [转]Adobe CC 2018 下载链接 Creative Cloud 2018 - Creative Cloud 2018 – Adobe CC 2018 Download Links
Creative Cloud 2018 – Adobe CC 2018 Download Links – ALL Languages Adobe CC 2018Direct Downloads Win ...
- Django——Model
一. ORM 在 MVC 或者说 MTV 设计模式中,模型(M)代表对数据库的操作.那么如何操作数据库呢? 我们可以在 Python 代码中嵌入 SQL 语句. 但是问题又来了,Python 怎么连接 ...
- React中的事件处理为什么要bind this?
个人总结: 问: 请给我讲一下React中的事件处理为什么要bind this? 答: 好的,比如说我写了一个类组件,有个onClick属性 ,onClick={ this.fun },如果不bind ...
- Node_进阶_6
Node进阶第六天 一.复习 cookie是在res中设置,req中读取的.第一次的访问没有cookie. cookie的存储大小有限,kv对儿.对用户可见,用户可以禁用.清除cookie.可以被篡改 ...