杭电 HDU ACM 1394 Minimum Inversion Number (线段树 逆序数)
2015-07-19 19:09
381 查看
Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 13558 Accepted Submission(s): 8277
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
线段树求逆序数 !!!!!
以前只是听说过 ,现在完美解决
#include<iostream> #include<sstream> #include<algorithm> #include<cstdio> #include<string.h> #include<cctype> #include<string> #include<cmath> #include<vector> #include<stack> #include<queue> #include<map> #include<set> using namespace std; int sum=0; const int INF=5005; struct Tree { int left,right,count; } tree[5005<<2]; int create(int root,int left,int right) { tree[root].left=left; tree[root].right=right; if(left==right) return tree[root].count=0; int a,b,mid=(left+right)>>1; a=create(root<<1,left,mid); b=create(root<<1|1,mid+1,right); return tree[root].count=a+b; } int cal (int root,int left,int right) { if(tree[root].left>right||tree[root].right<left) return 0; if(tree[root].left>=left&&tree[root].right<=right) return tree[root].count; int a,b,mid=(tree[root].left+tree[root].right)>>1; a=cal(root<<1,left,right); b=cal(root<<1|1,left,right); return a+b; } void update(int root,int pos,int val) { if(tree[root].left==tree[root].right) { tree[root].count=1; return ; } int mid=(tree[root].left+tree[root].right)>>1; if(pos<=mid) update(root<<1,pos,val); else update(root<<1|1,pos ,val); tree[root].count=tree[root<<1].count+tree[root<<1|1].count; } int main() { int n,result=0 ,cnt[5004]; while(scanf("%d",&n)!=EOF) { result=0; create(1,0,n-1); for(int i=0; i<n; i++) { scanf("%d",&cnt[i]); result+=cal(1,cnt[i],n-1); update(1,cnt[i],1); } int RR=result; for(int i=0; i<n; i++) { result=result-cnt[i]+(n-cnt[i]-1); RR=min(RR,result); } printf("%d\n",RR); } return 0; }
相关文章推荐
- 自己用java写的一个swing版的万年历
- 黑马程序员—IOS基础之OC—static、self、NSString和NSLog讲解
- wp page template
- 【cocos2d-js公文】十八、Cocos2d-JS v3.0物业风格API
- java设计模式-Builder
- C++如何连续输入多个可能含空格的字符串
- Java单元测试技术1
- 使用HttpClient访问网络
- jquery组件开发
- 架构师十项技能
- 模板匹配(Match Template)
- android RelativeLayout 内容居中解决办法:
- 密码学的进化史——手工加密阶段
- c# 鼠标中的各种形状
- Java的反射机制
- 如何升级openssl到最新版本 ubuntu
- PHP数组的使用方法小结
- Backbone 学习笔记
- 19-linux中一些命令
- hdu 3068 最长回文 (Manacher模版)