HDU 1394 线段树or 树状数组~

                              Minimum Inversion Number  

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.

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的整数,允许你每次将序列的头调到序列尾,让你求出最小的逆序和,
思路:先用线段树or树状数组预处理出原先的逆序和,然后再一个一个推出,每次操作后所产生的的新的逆序和,就拿样例来说,一开始的逆序和是22,如果将1调到末尾那里的话,原先在1后面比1小的数有1个,这个数是0,而当1调到后面的时候,在1前面的比一大的数有8个,为3,6,9,8,5,7,4,2,那么新序列的逆序和为22 - 1 + 8 = 29。以此类推
AC代码:线段树的方法,借鉴了notonlysuccess大神的姿势:
Memory: 320 KB Time: 78 MS
#include <cstdio>
#include <iostream>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn = 5555;
int sum[maxn<<2];
void PushUp(int rt){sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void build(int l,int r,int rt){sum[rt] = 0;if(l == r) return ;int m = (l + r) >> 1;build(lson);build(rson);return ;
}
void update(int p,int l,int r,int rt){if(l == r){sum[rt]++;return ;}int m = (l + r)>>1;if(p <= m) update(p,lson);else update(p,rson);PushUp(rt);
}
int query(int L,int R,int l,int r,int rt){if(L <= l&&r <= R){return sum[rt];}int m = (l + r)>>1;int ret = 0;if(L <= m) ret += query(L,R,lson);if(R > m)  ret += query(L,R,rson);return ret;
}
int x[maxn];
int main(){int n;while(~scanf("%d",&n)){build(0,n - 1,1);int sum = 0;for(int i = 0;i < n;i++){scanf("%d",&x[i]);sum += query(x[i],n - 1,0,n - 1,1);update(x[i],0,n - 1,1);}int res = sum;for(int i = 0;i < n;i++){sum += n - x[i] - x[i] - 1;res = min(res,sum);}printf("%d\n",res);}return 0;
}
View Code

后来我有用树状数组做了一下,发觉树状数组一般都要比线段树要快~

Memory: 332 KB Time: 31 MS
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int maxn = 5555;
int c[maxn];
int a[maxn];
int n;
inline int lowbit(int x){return x&(-x);
}
int sum(int x){int ret = 0;while(x > 0){ret += c[x];x -= lowbit(x);}return ret;
}
void add(int x,int d){while(x <= n){c[x] += d;x += lowbit(x);}
}
int main(){while(~scanf("%d",&n)){memset(c,0,sizeof(c));memset(a,0,sizeof(a));int ans = 0;for(int i = 1;i <= n;i++){scanf("%d",&a[i]);ans += i - 1 - sum(a[i]);add(a[i] + 1,1);}//cout<<ans<<endl;int MIN = ans;for(int i = 1;i <= n;i++){MIN += n - (a[i] + 1) - (a[i]);ans = min(MIN,ans);}printf("%d\n",ans);}return 0;
}
View Code

 

 

 

转载于:https://www.cnblogs.com/jusonalien/p/4004973.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/420575.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

servlet,springmvc,springboot转发时页面静态资源404问题

目录不讨论静态资源过滤的问题。。。这个问题重定向不会404&#xff0c;因为重定向是找到了对应的页面&#xff0c;是浏览器决定的。 而转发在相同目录下转发会找到资源。但是从controller&#xff08;根目录&#xff09;里面转发到根目录的下面的目录&#xff0c;转发能过去&…

消除过期的对象引用

http://www.oak.hk/blog/2014/09/28/eliminate-obsolete-object-refrences/ 转载于:https://www.cnblogs.com/reader2012/p/4006299.html

mysql数据库面试总结

mysql数据库相关1. 数据库事务的四个特性及含义2. 视图的作用&#xff0c;视图可以更改么2.1 什么是视图&#xff0c;作用3. drop,delete与truncate的区别4. 索引的工作原理及其种类5. 连接查询的种类6. 数据库范式7. 数据库优化的思路7.1 sql语句的优化7.2 数据库结构优化7.3 …

SQL Server 中索引的禁用与删除

主题 1、 禁用索引 alter index index_name on table_name disable; 主题 2、 删除索引 drop index table_name.index_name; 转载于:https://www.cnblogs.com/JiangLe/p/4007095.html

关于split与StringTokenizer的理解

关于split与StringTokenizer的理解 一.split 依据匹配给定的正則表達式来拆分此字符串。此方法返回的数组包括此字符串的子字符串&#xff0c;每一个子字符串都由还有一个匹配给定表达式的子字符串终止&#xff0c;或者由此字符串末尾终止。数组中的子字符串按它们在此字符串中…

springboot, thymeleaf 教你快速搭建网站

目录项目结构国际化curd操作404页面拦截器地址&#xff1a; https://github.com/sevenyoungairye/spring-boot-study员工管理员系统&#xff0c;页面用html thymeleaf模板数据库用的是map集合&#xff0c;没用真实的数据库项目结构 国际化 默认中文 中文&#xff0c;英文切换…

springboot 整合druid

目录1. maven依赖2. yml配置3. druid配置类编写4. 后台性能监控https://github.com/sevenyoungairye/spring-boot-studydruid优点&#xff1a;提供性能监控&#xff0c;配置灵活丰富 1. maven依赖 <!-- mysql驱动 springboot内置 --><dependency><groupId>…

Ubuntu 查看默认软件安装位置

tags: Linux 方法 1&#xff1a;在命令行输入&#xff1a;dpkg -L 软件包名&#xff1b;方法 2&#xff1a;在/var/cache/apt/archives找的你安装程序的包&#xff0c;然后用gdebi-gtk软件包名可以查看具体安装在什么位置。转载于:https://www.cnblogs.com/svitter/p/4011433.h…

maven 项目管理和构建工具

mvn1. maven 是什么2. maven能解决什么问题3. maven 需要配置和下载4. 使用eclipse创建maven项目5. xml依赖配置 作用范围6. maven的常用命令1. maven 是什么 Maven 在美国是一个口语化的词语&#xff0c;代表专家、内行的意思&#xff0c; Maven是一个项目管理工具&#xff0…

大数据----基于sogou.500w.utf8数据的MapReduce编程

目录 一、前言二、准备数据三、编程实现3.1、统计出搜索过包含有“仙剑奇侠传”内容的UID及搜索关键字记录3.2、统计rank<3并且order>2的所有UID及数量3.3、上午7-9点之间&#xff0c;搜索过“赶集网”的用户UID3.4、通过Rank&#xff1a;点击排名 对数据进行排序 四、参…

Java源代码分析与生成

源代码分析&#xff1a;可使用ANTLRANTLR是开源的语法分析器&#xff0c;可以用来构造自己的语言&#xff0c;或者对现有的语言进行语法分析。JavaParser 对Java代码进行分析CodeModel 用于生成Java代码(但对于已有代码的支持可能有问题) 转载于:https://www.cnblogs.com/laoni…

springboot 整合mybatis实现curd

springboot 整合mybatispom文件mvc 架构application.properties 扩展配置&#xff0c;druid配置类项目地址&#xff1a;https://github.com/sevenyoungairye/spring-boot-study/tree/main/springboot-mybatis-05pom文件 <!--整合mybatis--><dependency><groupId…

关于android 调用网页隐藏地址栏

首先创建项目&#xff0c;在main.xml里 添加好WebView控件R.id为webview1。 HelloWebView.java 代码 package liu.ming.com; import android.app.Activity;import android.os.Bundle;import android.view.KeyEvent;import android.webkit.WebView;import android.webkit.WebVie…

spring security 认证与权限控制

目录1. 使用授权和认证的必要性2. spring security 与 shiro 与 过滤器&#xff0c;拦截器3. 具体配置使用项目地址&#xff1a;https://github.com/sevenyoungairye/spring-boot-study/tree/main/springboot-security-061. 使用授权和认证的必要性 什么是安全框架&#xff0c…

CodeIgniter框架下载辅助函数的一个小bug

if (strpos($_SERVER[HTTP_USER_AGENT], "MSIE") ! FALSE){header(Content-Type: .$mime); // <---1)这里header(Content-Disposition: attachment; filename".$filename.");header(Expires: 0);header(Cache-Control: must-revalidate, post-check0, p…

win7 cmd命令行窗口 宽度无法变大 自由调整大小

偶然遇到了这个问题,百度查到了解决方案,执行一个bat批处理命令. mode con lines40 mode con cols160 color 250 cls cmd转载于:https://www.cnblogs.com/DreamDrive/p/4017970.html

springboot 与shiro整合

shiro~ shiro快速入门springboot 整合shiro核心目标清爽pom用户认证授权认证&#xff0c;与数据库交互shiro configuration核心controller 获取shiro 中的token页面控制功能的隐藏和显示https://github.com/sevenyoungairye/spring-boot-study/tree/main/springboot-shiro-07sh…