博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Minimum Inversion Number
阅读量:4967 次
发布时间:2019-06-12

本文共 2275 字,大约阅读时间需要 7 分钟。

Minimum Inversion Number

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14585 Accepted Submission(s): 8901

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.

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

Author

CHEN, Gaoli

Source

ZOJ Monthly, January 2003
线段树求逆序数,每当你将开头的a[i],移到尾部的时候,逆序数会减少a[i],也会增加n-a[i]-1,所以变化为n-2*a[i]-1,求最小值

#include 
#include
#include
#include
#include
#include
#define LL long longusing namespace std;const int MAX = 5500;int Tree[MAX*4];int a[MAX];int MM;int Query(int L,int R,int site,int l,int r){ if(l==L&&R==r) { return Tree[site]; } int mid=(L+R)>>1; if(r<=mid) { return Query(L,mid,site<<1,l,r); } else if(l>mid) { return Query(mid+1,R,site<<1|1,l,r); } else { return Query(L,mid,site<<1,l,mid)+Query(mid+1,R,site<<1|1,mid+1,r); }}void update(int L,int R,int site,int s){ if(L==R) { Tree[site]++; return ; } Tree[site]++; int mid = (L+R)>>1; if(mid>=s) { update(L,mid,site<<1,s); } else { update(mid+1,R,site<<1|1,s); }}int main(){ int n; while(~scanf("%d",&n)) { memset(Tree,0,sizeof(Tree)); int ans=0; for(int i=1;i<=n;i++) { scanf("%d",&a[i]);//求逆序数 ans+=Query(0,n-1,1,a[i],n-1); update(0,n-1,1,a[i]); } MM = ans; for(int i=1;i<=n;i++)//进行移位 { ans=ans+n-2*a[i]-1; if(ans

转载于:https://www.cnblogs.com/juechen/p/5255991.html

你可能感兴趣的文章
2016-2017-2点集拓扑作业[本科生上课时]讲解视频
查看>>
appium(13)- server config
查看>>
IIS负载均衡-Application Request Route详解第六篇:使用失败请求跟踪规则来诊断ARR...
查看>>
管理信息系统 第三部分 作业
查看>>
[Leetcode Week13]Search a 2D Matrix
查看>>
查看端口占用cmd命令
查看>>
2019.01.17王苛震作业
查看>>
清除浮动
查看>>
PayPal(贝宝)支付接口、文档、IPN
查看>>
ORACLE 10G R2_执行计划中cost cardinality bytes cpu_cost io_cost解释
查看>>
本地存储
查看>>
MP3的播放与停止
查看>>
牛客(59)按之字形顺序打印二叉树
查看>>
JavaScript 图表库 xCharts
查看>>
Android项目的目录结构
查看>>
C++中“引用”的底层实现
查看>>
Spring Cloud与微服务构建:微服务简介
查看>>
Babel 是干什么的
查看>>
20180418小测
查看>>
数字三角形
查看>>