我是靠谱客的博主 简单黑猫,最近开发中收集的这篇文章主要介绍Minimum Inversion Number 树状数组Minimum Inversion Number,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21360 Accepted Submission(s): 12802
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
Author
CHEN, Gaoli
Source
ZOJ Monthly, January 2003
Recommend
Ignatius.L
题目的意思是给你一个n,然后是一段包含0~n-1全部数字的序列,然后有一个操作把前面的数放到后面来,这样的操作可以形成n个序列,让你求出这样所有的逆序序列里最小的逆序数
树状数组实现求取逆序数:把树状数组当做标记数组使用,从后往前输入每一个数据计算出现了多少个比这个数小的数的个数因为树状数组可以快速求和,这样就可以在nlogn的时间复杂度内求得逆序数了
然后就是怎么根据第一个求得的下一个状态的逆序数了
对于开头的第一个数,如果这个数放在后面,首先逆序数会减少【后面有多少个比他小的数的个数】,加到后面会导致逆序数增加【前面有多少个比他大的】,由于这里包含了所有的0~n-1的数,所以很容易求得所有的逆序数。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int MAXN=5050;
int c[MAXN];
int n;
int lowbit(int x)
{
return x&(-x);
}
void add(int i,int val)
{
while(i<=n)
{
c[i]+=val;
i+=lowbit(i);
}
}
int sum(int i)
{
int s=0;
while(i>0)
{
s+=c[i];
i-=lowbit(i);
}
return s;
}
int a[MAXN];
int main()
{
while(scanf("%d",&n)!=EOF)
{
int ans=0;
memset(a,0,sizeof(a));
memset(c,0,sizeof(c));
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
a[i]++;
ans+=sum(n)-sum(a[i]);
add(a[i],1);
}
int Min=ans;
for(int i=1;i<=n;i++)
{
ans+=n-a[i]-(a[i]-1);
if(ans<Min)Min=ans;
}
printf("%dn",Min);
}
return 0;
}
#include<string.h>
#include<algorithm>
using namespace std;
const int MAXN=5050;
int c[MAXN];
int n;
int lowbit(int x)
{
return x&(-x);
}
void add(int i,int val)
{
while(i<=n)
{
c[i]+=val;
i+=lowbit(i);
}
}
int sum(int i)
{
int s=0;
while(i>0)
{
s+=c[i];
i-=lowbit(i);
}
return s;
}
int a[MAXN];
int main()
{
while(scanf("%d",&n)!=EOF)
{
int ans=0;
memset(a,0,sizeof(a));
memset(c,0,sizeof(c));
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
a[i]++;
ans+=sum(n)-sum(a[i]);
add(a[i],1);
}
int Min=ans;
for(int i=1;i<=n;i++)
{
ans+=n-a[i]-(a[i]-1);
if(ans<Min)Min=ans;
}
printf("%dn",Min);
}
return 0;
}
人一我百!人十我万!永不放弃~~~怀着自信的心,去追逐梦想。
最后
以上就是简单黑猫为你收集整理的Minimum Inversion Number 树状数组Minimum Inversion Number的全部内容,希望文章能够帮你解决Minimum Inversion Number 树状数组Minimum Inversion Number所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复