HDU 1394 (树状数组 & 线段树 两种做法)
来源:互联网 发布:mysql修改连接密码 编辑:程序博客网 时间:2024/05/11 00:27
Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16165 Accepted Submission(s): 9836
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>#include <cstring>#include <cstdio>#define Max 5005#define ls l,m,rt<<1#define rs m+1,r,rt<<1|1using namespace std;int segTree[Max<<2];//存的是树的下面的最小的值int ary[Max];//存的是输入的数字int vis[Max<<2];//inline max(int a,int b){return a>b?a:b;}inline min(int a,int b){return a>b?b:a;}void push_up(int rt){segTree[rt] = segTree[rt<<1] + segTree[rt<<1|1];}void build(int l,int r,int rt)//构建线段树,并求出每个节点的下面分支的最小值{segTree[rt] = 0;if(l == r)return ;int m = (l+r)>>1;build(ls);build(rs);push_up(rt);}void update(int site,int l,int r,int rt)//单点更新{if(l == r){segTree[rt]++;return ;}int m = (l + r) >> 1;if(site <= m)update(site,ls);elseupdate(site,rs);push_up(rt);}int query(int ll,int rr,int l,int r,int rt){if(l >= ll && r <= rr)return segTree[rt];int m = (l + r) >> 1;int ans =0 ;if(ll <= m)ans += query(ll,rr,ls);if(rr > m)ans += query(ll,rr,rs);return ans;}int main(){ int n,m;int i,j,k;int x[Max]; while(cin>>n){build(0,n-1,1);int sum = 0;for(i=0;i<n;i++){scanf("%d",&x[i]);sum += query(x[i],n-1,0,n-1,1);update(x[i],0,n-1,1);}int res = sum;for(i=0;i<n;i++){sum += n-x[i] - x[i] - 1;res = min(sum,res);}cout<<res<<endl; } return 0; }
然后试着用树状数组也写了一下,原理和线段树是相同的,只不过树状数组找比他前面的大的数,是先找比他小的数,然后再拿总的数减去比他小的数就可以求出比他大的数了
#include <iostream>#include <cstring>#include <cstdio>#define Max 5005using namespace std;int c[Max],nn;inline max(int a,int b){return a>b?a:b;}inline min(int a,int b){return a>b?b:a;}int lowbit(int k){return k&(-k);}void add(int k){while(k <= nn){c[k]++;k += lowbit(k);}}int sum(int k){int s = 0;while(k){s += c[k];k -= lowbit(k);}return s;}int main(){ int n,m;int i,j,k;int x[Max]; while(cin>>nn){m=0;memset(c,0,sizeof(c));for(i=0;i<nn;i++){scanf("%d",&x[i]);m += (sum(nn) - sum(x[i]+1));add(x[i]+1);}int res = m;for(i=0;i<nn;i++) {m += nn - x[i] - x[i] -1;res = min(res,m);}cout<<res<<endl; } return 0; }
最终发现树状数组还是效率更高一点点
0 0
- HDU 1394 (树状数组 & 线段树 两种做法)
- hdu 4031 attack 树状数组 线段树 两种做法
- hdu 1166 敌兵布阵 单点更新 树状数组 线段树 两种做法
- poj1195 Mobile phones 二维线段树和二维树状数组两种做法 树套树
- hdu1556树状数组&&线段树2种做法
- HDU 1394 树状数组||线段树
- 洛谷 P3372 线段树 1(树状数组做法)
- POJ--线段树成段更新--树状数组做法
- upc1976 树状数组和线段树两种做法
- HDU1556 树状数组,线段树区间更新两种方法(主要树状数组)
- HDU 1394 Minimum Inversion Number 树状数组/线段树
- HDU-1394 Minimum Inversion Number 线段树|树状数组
- HDU 1394 Minimum Inversion Number 树状数组&&线段树
- hdu 1394 Minimum Inversion Number(树状数组,线段树)
- HDU - 1394 Minimum Inversion Number(线段树 | 树状数组)
- Hdu 1394 Minimum Inversion Number(线段树或树状数组)
- HDU 1394 Minimum Inversion Number(树状数组||线段树)
- hdu-1394-Minimum Inversion Number(线段树 || 树状数组)
- leetcode118 & 119. [Array]Pascal's Triangle
- 第三周 项目4 (2)换分币
- 关于Java和C#的内部类的小记
- HD_1241Oil Deposits(DFS)
- https原理及tomcat配置https方法
- HDU 1394 (树状数组 & 线段树 两种做法)
- autoconf AC_ARG_WITH, AC_CACHE_CHECK, AC_TRY_LINK宏学习
- Bloom Filter概念和原理
- javascript学习2
- hdu1828线段树扫描线求周长
- jQuery transform类型实例方法探究
- hdu 5636 Shortest Path
- 这种员工,将来都是做高管的料!
- epoll示范