博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu-1394Minimum Inversion Number(暴力解法或者线段树 求最少逆序对)
阅读量:4048 次
发布时间:2019-05-25

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

Minimum Inversion Number

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


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
101 3 6 9 0 8 5 7 4 2
 

Sample Output
16
 

Author
CHEN, Gaoli
 

Source
 

Recommend
Ignatius.L   |   We have carefully selected several similar problems for you:            
暴力解法
#include
#include
#include
#include
using namespace std;int a[5500];int n;int main(){ while(~scanf("%d",&n)) //总共最多有n-1种逆数对 { for(int i=0;i
a[j]) ans++; } } int res=999999999; if(ans
线段树解法
#include
#include
#include
#include
using namespace std; #define N 5555#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1int sum[N<<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); pushup(rt);}void update(int p,int l,int r,int rt){ if(l==r) { sum[rt]++;//由于存入了p这个点 sum[rt]的数量要加一 在递归更新节点的sum值 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)//将L到R之间有多少点存在的数量 求出来 { if(L<=l&&R>=r) { return sum[rt]; } int m=(l+r)>>1; int res=0; if(L<=m)res+=query(L,R,lson); if(R>m)res+=query(L,R,rson); return res;}int x[N];int main(){ int n; while(~scanf("%d",&n)) { build(0,n-1,1); int sum=0; for(int i=0;i
 

转载地址:http://dafci.baihongyu.com/

你可能感兴趣的文章
flutter-解析json
查看>>
android中shader的使用
查看>>
java LinkedList与ArrayList迭代器遍历和for遍历对比
查看>>
drat中构造方法
查看>>
JavaScript的一些基础-数据类型
查看>>
JavaScript基础知识(2)
查看>>
转载一个webview开车指南以及实际项目中的使用
查看>>
android中对于非属性动画的整理
查看>>
一个简单的TabLayout的使用
查看>>
ReactNative使用Redux例子
查看>>
Promise的基本使用
查看>>
coursesa课程 Python 3 programming 统计文件有多少单词
查看>>
coursesa课程 Python 3 programming 输出每一行句子的第三个单词
查看>>
Returning a value from a function
查看>>
coursesa课程 Python 3 programming Functions can call other functions 函数调用另一个函数
查看>>
coursesa课程 Python 3 programming The while Statement
查看>>
course_2_assessment_6
查看>>
coursesa课程 Python 3 programming course_2_assessment_7 多参数函数练习题
查看>>
coursesa课程 Python 3 programming course_2_assessment_8 sorted练习题
查看>>
在unity中建立最小的shader(Minimal Shader)
查看>>