Minimum Inversion Number+线段树
来源:互联网 发布:介绍几个源码论坛 编辑:程序博客网 时间:2024/05/13 23:57
Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6529 Accepted Submission(s): 3979
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.
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#include<iostream>using namespace std;#define MAX 5000typedef struct infor{int left , right , value ;}infor ;infor tree[MAX*3];void create(int root, int left , int right ){tree[root].left = left ;tree[root].right = right ;tree[root].value = 0 ;if(right == left )return ;int mid = (left + right ) / 2 ;create(root*2 , left , mid);create(root*2 + 1 , mid + 1 , right );}void update(int root, int pos){tree[root].value += 1;if(tree[root].left == tree[root].right )return ;int mid = (tree[root].left + tree[root].right ) / 2 ;if(pos > mid )update(root*2 + 1, pos);else update(root*2 , pos);}int cal(int root, int left , int right ){if(tree[root].left == left && tree[root].right == right )return tree[root].value ;int mid = (tree[root].left + tree[root].right ) / 2;if(left > mid )return cal(root*2 + 1 , left , right );else if(right <= mid)return cal(root*2 , left , right );else return cal(root*2 + 1 , mid + 1, right ) + cal(root*2 , left , mid );}int main(){int n;while(cin >> n){int i ,a[MAX+1];create(1,0,n-1);int ans = 0 ;for( i = 0 ; i < n ; i ++){cin >> a[i] ;ans+=cal(1,a[i],n-1);update(1,a[i]);}int min = ans ;for(i = 0 ; i < n ; i ++){ans -=a[i];ans +=n-a[i] - 1;if(ans<min)min=ans;}cout<<min<<endl;}return 0;}#include<iostream>using namespace std;#define MAX 5000int a[3*MAX],c[3*MAX] , n ; int lowbit( int k ){return k&(-k) ;}void update(int sum , int k){while( k <= n ){c[k]+=sum ;k+=lowbit(k);}}int cal( int k){int sum = 0 ;while(k > 0 ){sum += c[k];k-=lowbit(k);}return sum ;}int main( void ){while( cin >> n ){memset(c,0,sizeof(c));int i;int ans=0;for( i = 1 ; i <= n ; i ++){cin >> a[i] ; a[i]++;ans+=cal(n)-cal(a[i]) ;update( 1 , a[i]);}int sum = ans ; for( i =1 ; i <= n ; i ++){ans+=n-a[i]-(a[i]-1) ;if(sum>ans)sum = ans ; }cout<< sum << endl ;}return 0 ;}
- Minimum Inversion Number+线段树
- Minimum Inversion Number(线段树)
- Minimum Inversion Number (线段树 + 递推)
- HDU 1394 Minimum Inversion Number 线段树
- HDU 1394 Minimum Inversion Number 线段树
- HDU-1394 Minimum Inversion Number(线段树)
- hdu1394-Minimum Inversion Number(线段树)
- 【线段树】hdu 1394 Minimum Inversion Number
- 1394 Minimum Inversion Number 线段树
- hdu 1394 Minimum Inversion Number 线段树
- hdu 1394 Minimum Inversion Number 线段树
- HDU1394 Minimum Inversion Number(线段树)
- hdu 1394 Minimum Inversion Number 线段树
- Minimum Inversion Number 线段树+单点修改
- hdu 1394 Minimum Inversion Number 线段树
- HDU1394 Minimum Inversion Number线段树
- Minimum Inversion Number(线段树)
- Minimum Inversion Number(线段树)
- 多年收集的VC++电子书,源代码现在免费全部奉献给大家
- Eclipse搭建J2EE+PHP+C/C++平台(使用CDT和PDT)
- 日复一日的坚持着,希望自己早日如愿,把一个个职业规划尽快实现
- Git 常用指令
- Ubuntu 解决出现make menuconfig错误
- Minimum Inversion Number+线段树
- UIPickView的使用
- android的Handler
- Rescue 1242 广搜
- 10g RAC: How to Clean Up After a Failed CRS Install
- C++高级编程资料汇总(参考手册+电子书+编程思想+算法分析) ...
- android sdk没有64位linux版本
- 一道笔试题:假设在n进制下,下面的等式成立,n的值是(),567*456=150216.
- 递归建立二叉树 二叉树分层遍历