博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ACM Minimum Inversion Number 解题报告 -线段树
阅读量:4668 次
发布时间:2019-06-09

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

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
1 //线段树专题解题。2 //其实一开始没想通,后来手动模拟了一下整个树建立的过程就全部清楚了。3 //详细AC代码在下面:
1 #include"iostream" 2 #include"algorithm" 3 #include"cstdio" 4 #include"cstring" 5 #include"cmath" 6 #define max(a,b) a>b?a:b 7 #define min(a,b) a
>1;21 Build(lson);//建立左节点22 Build(rson);//建立右节点23 }24 25 void UpData(int p,int l,int r,int rt) {26 if(r==l) { //找到并更新目标点27 sum[rt]++;28 return ;29 }30 int m=(r+l)>>1;31 if(p<=m) UpData(p,lson); //如果不是目标点向左右寻找32 if(p >m) UpData(p,rson);33 PushUp(rt);//将更新过的每个点的子节点的和更新。34 }35 36 int Query(int L,int R,int l,int r,int rt) {37 if(L<=l&&R>=r) //大小超过整个范围38 return sum[rt]; //返回总数39 int m=(r+l)>>1;40 int ret=0;41 if(L<= m) ret += Query(L,R,lson); //比x[i]大的树的左值和42 if(R > m) ret += Query(L,R,rson); //比x[i]大的树的右值和43 return ret;44 }45 int x[MX];46 int main() {47 int n;48 int sums;49 char s[2];50 while(~scanf("%d",&n)) {51 sums=0; 52 Build(0,n-1,1); //【这里应该从0~n-1比较好,从1~n的话0的位置不好放在哪里了。后面也就一样了。】 53 for(int i=0; i
View Code

 

 
 

转载于:https://www.cnblogs.com/HDMaxfun/p/5693100.html

你可能感兴趣的文章
利用jQuery点击DIV变颜色的小例子
查看>>
OpenStack Cinder发展动态系列--Austin峰会
查看>>
MySQL -- 行转列 -- GROUP_CONCAT -- MAX(CASE WHEN THEN)
查看>>
CSS魔法堂:hasLayout原来是这样!
查看>>
JS获取JSON对象数组某个属性最大值
查看>>
教程链接
查看>>
Spring MVC 文件上传 & 文件下载
查看>>
C++中print和printf的区别
查看>>
service程序改为windows窗体展示
查看>>
查询集 QuerySet
查看>>
ios 键盘的一些问题
查看>>
mac上使用终端生成RSA公钥和密钥
查看>>
jQuery-点击按钮页面滚动到顶部,底部,指定位置
查看>>
[原创]group by和compute 的使用
查看>>
9.13列表的用法
查看>>
secureCRT 如何上传下载文件
查看>>
Spring Cloud Config
查看>>
phoneGap实现离线缓存
查看>>
第六周学习进度
查看>>
java学习之—链表(3)
查看>>