HDU 1394 线段树求逆序对
时间:2019-03-20
本文章向大家介绍HDU 1394 线段树求逆序对,主要包括HDU 1394 线段树求逆序对使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25905 Accepted Submission(s): 15250
Problem Description
The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj.
For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of the seqence, we will obtain another sequence. There are totally n such sequences as the following:
a1, a2, ..., an-1, an (where m = 0 - the initial seqence)
a2, a3, ..., an, a1 (where m = 1)
a3, a4, ..., an, a1, a2 (where m = 2)
...
an, a1, a2, ..., an-1 (where m = n-1)
You are asked to write a program to find the minimum inversion number out of the above sequences.
For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of the seqence, we will obtain another sequence. There are totally n such sequences as the following:
a1, a2, ..., an-1, an (where m = 0 - the initial seqence)
a2, a3, ..., an, a1 (where m = 1)
a3, a4, ..., an, a1, a2 (where m = 2)
...
an, a1, a2, ..., an-1 (where m = n-1)
You are asked to write a program to find the minimum inversion number out of the above sequences.
Input
The input consists of a number of test cases. Each case consists of two lines: the first line contains a positive integer n (n <= 5000); the next line contains a permutation of the n integers from 0 to n-1.
Output
For each case, output the minimum inversion number on a single line.
Sample Input
10
1 3 6 9 0 8 5 7 4 2
Sample Output
16
题意:一个长度为n 的 0-n-1 的全排列 你可以进行若干次操作 把第一个数放到最后面 形成一个新的序列(最多n个)问所有序列中逆序对最少的数量。
解析:逆序对很好求 对于每一个数看它前面有多少个比它大的记为sum[ a[i] ] sigmasum[] 就是逆序的数量 每个位置都如此,线段树就是每次查询[a[i],n-1] 的个数;
假如当前序列的逆序对是ans 那么把当前第一个数a[i] 放到最后面 后面对于每个数[0,a[i]) sum[ ] 就会少一次 同样 sum[ a[i] ] = n-1-a[i]
所以 操作之后就是 ans+=n-a[i]+n-1-a[i]。取最大值就好了
#include <bits/stdc++.h> #define pb push_back #define mp make_pair #define fi first #define se second #define all(a) (a).begin(), (a).end() #define fillchar(a, x) memset(a, x, sizeof(a)) #define huan printf("\n") #define debug(a,b) cout<<a<<" "<<b<<" "<<endl #define ffread(a) fastIO::read(a) using namespace std; typedef long long ll; const int maxn = 1e4+10; const int inf = 0x3f3f3f3f; const int mod = 100000007; const double epx = 1e-6; const double pi = acos(-1.0); //head----------------------------------------------------------------- int sum[maxn*4]; void PushUp(int rt) { sum[rt]=sum[rt<<1]+sum[rt<<1|1]; } void build(int l,int r,int rt) { if(l==r) { sum[rt]=0; return; } int mid=(l+r)>>1; build(l,mid,rt<<1); build(mid+1,r,rt<<1|1); PushUp(rt); } void update(int x,int val,int l,int r,int rt) { if(l==x&&r==x) { sum[rt]+=val; return; } int mid=(l+r)>>1; if(x<=mid) update(x,val,l,mid,rt<<1); else update(x,val,mid+1,r,rt<<1|1); PushUp(rt); } int query(int L,int R,int l,int r,int rt) { if(L<=l&&R>=r) { return sum[rt]; } int mid=(l+r)>>1; int ans=0; if(L<=mid) ans+=query(L,R,l,mid,rt<<1); if(R>mid) ans+=query(L,R,mid+1,r,rt<<1|1); return ans; } int a[maxn]; int main() { int n; while(scanf("%d",&n)!=EOF) { fillchar(sum,0); int ans=0; for(int i=1;i<=n;i++) { scanf("%d",&a[i]); update(a[i]+1,1,1,n,1); //出于方便从1开始 ans+=query(a[i]+2,n,1,n,1); } int ret=inf; for(int i=1;i<=n;i++) { ans+=n-2*a[i]-1; ret=min(ans,ret); } printf("%d\n",ret); } }
复习一下线段树基础。。
- JS魔法堂:彻底理解0.1 + 0.2 === 0.30000000000000004的背后
- mybatis: 利用多数据源实现分库存储
- 树莓派:文本编辑器与文件
- Java常用类(三)之StringBuffer与StringBuidler
- 使用监听器:定时清除map缓存
- 树莓派:光阴的故事
- lombok在IntelliJ IDEA下的使用
- 基础野:细说浮点数
- 微信开发如何保证access_token/jsapi_ticket长期有效
- struts2(五)之struts2拦截器与自定义拦截器
- 自定义的html radio button的样式
- 树莓派:漂洋过海来看你
- 树莓派:开机使用
- JavScript中的循环
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 【从0到1学算法】大O表示法
- 锦囊篇|一文摸懂AsyncTask
- 在Substrate链上跑Solidity ERC20智能合约
- 【Nginx】实现负载均衡、限流、缓存、黑白名单和灰度发布,这是最全的一篇了!
- 对称二叉树
- 你可能会踩到的一个 Eloquent 小坑
- 谨慎使用 Laravel 的 Model 复制(replicate)功能
- 0791-5.13.1-Hive视图执行show create table被截断异常分析
- SpringBoot系列之IDEA项目中设置热部署教程
- [教程] 创建第一条 Substrate 链
- SpringBoot系列之actuator监控管理极速入门与实践
- 如何查看JVM运行的堆内存情况
- axios不支持finally的解决办法
- 【机器学习基础】关于Scikit-Learn,你不一定知道的10件事
- Java高级技术梳理