hdu 1394 Minimum Inversion Number (树状数组 逆序对)
2015-10-28 22:00
543 查看
Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 15247 Accepted Submission(s): 9311
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each case, output the minimum inversion number on a single line.
[align=left]Sample Input[/align]
10
1 3 6 9 0 8 5 7 4 2
[align=left]Sample Output[/align]
16
[align=left]Author[/align]
CHEN, Gaoli
[align=left]Source[/align]
ZOJ Monthly, January 2003
[align=left]Recommend[/align]
Ignatius.L | We have carefully selected several similar problems for you: 1698 1540 1542 1255 2795
虽然在学线段树。。。但是感觉美必要为了用线段树而用线段树。。。
我知道树状数组的东西都可以用线段树来解决。。。。
不过还是觉得树状数组。。。实在是优美。。。所以这题还是用树状数组搞得。。
这题是问一个长度为n的循环数组中,逆序对最少的个数。。。
我们可以先用树状数组求出初始的数列的逆序对。。。
然后其他的可以通过递推得到。。。。
当a[i]从处于位置1而被放到最后的时候。。。
cnt = cnt -a[i]+n-a[i]+1;
然后取所有cnt的最大值就行。
/************************************************************************* > File Name: code/hud/1394.cpp > Author: 111qqz > Email: rkz2013@126.com > Created Time: 2015年10月28日 星期三 21时16分47秒 ************************************************************************/ #include<iostream> #include<iomanip> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<string> #include<map> #include<set> #include<queue> #include<vector> #include<stack> #include<cctype> #define yn hez111qqz #define j1 cute111qqz #define ms(a,x) memset(a,x,sizeof(a)) using namespace std; const int dx4[4]={1,0,0,-1}; const int dy4[4]={0,-1,1,0}; typedef long long LL; typedef double DB; const int inf = 0x3f3f3f3f; const int N=5E3+7; int c ; int n,a ; int lowbit( int x) { return x&(-x); } void update ( int x,int delta) { // cout<<"a?"<<endl; for ( int i = x ; i <= n ; i = i + lowbit(i)) c[i] = c[i] + delta; } int Sum( int x) { // cout<<"b?"<<endl; int res = 0 ; for ( int i = x ;i >= 1; i = i - lowbit(i)) { res = res + c[i]; } return res; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif while (scanf("%d",&n)!=EOF) { ms(c,0); for ( int i = 1 ; i <= n ; i++) { scanf("%d",&a[i]); a[i]++; } int cnt = 0 ; int ans = inf; // puts("mmmmmmmiao?"); for ( int i = 1 ; i <= n ; i++) { update(a[i],1); cnt = cnt + i - Sum(a[i]); // puts("whats wrong?"); } // printf("cnt::%d\n",cnt); if (cnt<ans) ans = cnt; for ( int i = 1 ; i <= n ; i++) { cnt =cnt -a[i]+n-a[i]+1; if (cnt<ans&&cnt>0) ans = cnt; // printf("cnt:%d\n",cnt); } printf("%d\n",ans); } #ifndef ONLINE_JUDGE fclose(stdin); #endif return 0; }
View Code
相关文章推荐
- LeetCode Missing Number (简单题)
- android studio 学习遗留问题和学习总结
- sdut 3258 Square Number(山东声第六届ACM程序设计竞赛)
- 06-图1 列出连通集
- 如何运行ruby代码
- 自己写一个java.lang.reflect.Proxy代理的实现
- C#编程--输入和输出
- bzoj2067: [Poi2004]SZN
- Java基础(10):泛型
- HDU 3065 病毒侵袭持续中(AC自动机)
- 模板_二叉树先序中序遍历求后序遍历
- 我的iOS学习历程 - OC第三天
- 我的iOS学习历程 - OC第三天
- HDU 3065 病毒侵袭持续中(AC自动机)
- hdu3879 最大权闭合图
- 优化斐波那契
- Android之React Native平台与Android本地模块之间的调用
- 谈业务流程全生命周期管理支撑业务流程再造(3)
- 关于Android Studio 中快捷键 相当于 Eclipse中 Alt+/ 怎么设置
- 决策树算法 生成 剪枝 in Python