我是靠谱客的博主 有魅力皮带,最近开发中收集的这篇文章主要介绍HDU 1394 (树状数组 & 线段树 两种做法) Minimum Inversion Number,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
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
10 1 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|1
using 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);
else
update(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 5005
using 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;
}
最终发现树状数组还是效率更高一点点
最后
以上就是有魅力皮带为你收集整理的HDU 1394 (树状数组 & 线段树 两种做法) Minimum Inversion Number的全部内容,希望文章能够帮你解决HDU 1394 (树状数组 & 线段树 两种做法) Minimum Inversion Number所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复