C - Minimum Inversion Number

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
 //线段树专题解题。
//其实一开始没想通,后来手动模拟了一下整个树建立的过程就全部清楚了。
//详细AC代码在下面:
 #include"iostream"
#include"algorithm"
#include"cstdio"
#include"cstring"
#include"cmath"
#define max(a,b) a>b?a:b
#define min(a,b) a<b?a:b
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int MX = +;
int sum[MX<<];
void PushUp(int rt) {
sum[rt]=sum[rt<<]+sum[rt<<|]; //更新节点 父节点为子节点之和
} void Build(int l,int r,int rt) {
sum[rt]=; //建立一棵空树 【这里之前放在了判断里面,叶节点确实清空了,枝节点漏掉了】
if(r==l) return ;
int m=(r+l)>>;
Build(lson);//建立左节点
Build(rson);//建立右节点
} void UpData(int p,int l,int r,int rt) {
if(r==l) { //找到并更新目标点
sum[rt]++;
return ;
}
int m=(r+l)>>;
if(p<=m) UpData(p,lson); //如果不是目标点向左右寻找
if(p >m) UpData(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=(r+l)>>;
int ret=;
if(L<= m) ret += Query(L,R,lson); //比x[i]大的树的左值和
if(R > m) ret += Query(L,R,rson); //比x[i]大的树的右值和
return ret;
}
int x[MX];
int main() {
int n;
int sums;
char s[];
while(~scanf("%d",&n)) {
sums=;
Build(,n-,); //【这里应该从0~n-1比较好,从1~n的话0的位置不好放在哪里了。后面也就一样了。】
for(int i=; i<n; i++) {
scanf("%d",&x[i]);
sums+=Query(x[i],n-,,n-,);
UpData(x[i],,n-,);
}
int ret=sums;
for(int i=; i<n; i++) {
sums=sums+n-*x[i]-;
ret=min(ret,sums);
}
printf("%d\n",ret);
}
return ;
}
 
 

ACM Minimum Inversion Number 解题报告 -线段树的更多相关文章

  1. ACM: Just a Hook 解题报告 -线段树

    E - Just a Hook Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   D ...

  2. ACM: 敌兵布阵 解题报告 -线段树

    敌兵布阵 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Li ...

  3. Hdu P1394 Minimum Inversion Number | 权值线段树

    题目链接 题目翻译: 约定数字序列a1,a2,...,an的反转数是满足i<j和ai>aj的数对(ai,aj)的数量. 对于给定的数字序列a1,a2,...,an,如果我们将第1到m个数字 ...

  4. ACM: I Hate It 解题报告 - 线段树

    I Hate It Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Des ...

  5. HDU 1394 Minimum Inversion Number (数据结构-段树)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  6. ACM: Billboard 解题报告-线段树

     Billboard Time Limit:8000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descript ...

  7. ACM: Hotel 解题报告 - 线段树-区间合并

    Hotel Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description The ...

  8. [P3097] [USACO13DEC] [BZOJ4094] 最优挤奶Optimal Milking 解题报告(线段树+DP)

    题目链接:https://www.luogu.org/problemnew/show/P3097#sub 题目描述 Farmer John has recently purchased a new b ...

  9. [jzoj 5662] 尺树寸泓 解题报告 (线段树+中序遍历)

    interlinkage: https://jzoj.net/senior/#contest/show/2703/1 description: solution: 发现$dfs$序不好维护 注意到这是 ...

随机推荐

  1. MVC公开课 – 2.查询,删除 (2013-3-15广州传智MVC公开课)

    查询 /Controller/HomeController.cs /// <summary> /// 查询 文章 列表 /// </summary> /// <retur ...

  2. Dubbo超时和重连机制

    dubbo启动时默认有重试机制和超时机制.超时机制的规则是如果在一定的时间内,provider没有返回,则认为本次调用失败,重试机制在出现调用失败时,会再次调用.如果在配置的调用次数内都失败,则认为此 ...

  3. Android中Thread和Service的区别zz

    1). Thread:Thread 是程序执行的最小单元,它是分配CPU的基本单位.可以用 Thread 来执行一些异步的操作. 2). Service:Service 是android的一种机制,当 ...

  4. 如何观察JS的事件队列的执行划分

    也就是说,不同的操作函数,操作符,JS将其放入事件队列是不一样的... 比如: 外部函数和内部函数,就是分两次放入事件循环的尾端的. 比如,多个操作符组成的链式操作,也有可能是放不同的操作批次进入事件 ...

  5. mysql5.7.11安装配置

    1.下载安装包. mysql-5.7.11版本: http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.11-winx64.zip 2.拷贝到任意盘: ...

  6. POJ3685 Matrix(嵌套二分)

    同行元素递减,同列元素递增,采用嵌套二分的方法 #include<cstdio> #include<iostream> #include<cstdlib> #inc ...

  7. 使用.NET Framework的配置文件app.config

    在一般的项目中,为了使你的代码更加灵活,更方便调整,减少不必要的hard code,我们都在config中添加许多配置信息,一般可以选择.NET自带的配置文件形式app.config或者web项目中的 ...

  8. MS SQL 合并结果集并求和 分类: SQL Server 数据库 2015-02-13 10:59 92人阅读 评论(0) 收藏

    业务情景:有这样一张表:其中Id列为表主键,Name为用户名,State为记录的状态值,Note为状态的说明,方便阅读. 需求描述:需要查询出这样的结果:某个人某种状态的记录数,如:张三,待审核记录数 ...

  9. Python 入门简介(一)

    Why Python? 我个人认为去学习一门新的语言其实是不需要理由的,当然如果你硬要我编个理由的话还是很容易的. 容易学 容易用 有人真的在用Python么? 这个问题谁用谁知道. 选择什么开发工具 ...

  10. win8.1/win10 UEFI + GPT 安装(测试机型:华硕S56CM)

    本教程简要介绍在UEFI 启动模式下在GPT分区表中,最简单的方法安装 Windows 10 x64 位系统.(并非傻瓜教程,安装者总要有一定的经验基础)下面先简单介绍一下UEFI和GTP. UEFI ...