您的位置:首页 > 其它

【hdu1394】Minimum Inversion Number——逆序对

2016-06-04 12:07 363 查看

题目:

C - Minimum Inversion Number Time Limit:1000MS Memory

Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status

Practice HDU 1394 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. 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

描述:

给定一个0-n-1长度为n的数列(n <= 5000),求这个数列的所有排列中逆序对数最小的,所有排列是指每次将数列的第一个数移动到最后一个,重复n-1次。

题解:

首先,逆序对有三种解法,树状数组和线段树是常规解法,但是由于这次的数据是连续的而且从0开始,说明我们不需要对数据进行离散化,这样的话除了常规做法外,由于数据范围不大,可以根据逆序对的定义直接暴力求解。

接下来对于每次移动,将第一个移动到最后一个,意味着原逆序对数要加上所有比这个数大的数,减去所有比这个数小的数,枚举每一次的变化即可。

代码:

树状数组版

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 5005;
int c[maxn],a[maxn],n;
int lowbit(int x)
{
return x & (-x);
}

void add(int x,int val)
{
for(;x <= n;x += lowbit(x))c[x] += val;
}

int cal(int x)
{
int ans = 0;
for(;x > 0;x -= lowbit(x))ans += c[x];
return ans;
}

int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d",&n))
{
memset(c,0,sizeof(c));
for(int i = 1;i <= n;i++)
scanf("%d",&a[i]);
int sum = 0,ans;
for(int i = n;i > 0;i--)
{
sum += cal(a[i]);
add(a[i]+1, 1);
}
ans = sum;
for(int i = 1;i <= n;i++)
{
sum += n - 1 - a[i] * 2;
ans = min(ans, sum);
}
printf("%d\n",ans);
}
return 0;
}


线段树版

#include <cstdio>
#include <cstring>
#include <algorithm>
#define ls t<<1
#define rs (t<<1)|1
using namespace std;
const int maxn = 5005;
struct segmentTree
{
int l,r,val;
}tree[maxn*3];
int fa[maxn],a[maxn],n;

void update(int t)
{
if(t == 1)return;
t >>= 1;
tree[t].val = tree[ls].val + tree[rs].val;
update(t);
}

void build(int l,int r,int t)
{
tree[t].l = l;
tree[t].r = r;
tree[t].val = 0;
if(l == r)
{
fa[l] = t;
return ;
}
build(l, (l+r)/2, ls);
build((l+r)/2+1, r, rs);
}

long long query(int l,int r,int t)
{
long long sum = 0;
if(tree[t].l >= l && tree[t].r <= r) return tree[t].val;
int mid = (tree[t].l + tree[t].r) / 2;
if(mid >= l) sum += query(l,r,ls);
if(mid < r) sum += query(l,r,rs);
return sum;
}

int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d",&n))
{
long long sum = 0,ans;
build(1,n,1);
for(int i = 0;i < n;i++)
{
scanf("%d",&a[i]);
sum += query(a[i]+1, n, 1);
tree[fa[a[i]+1]].val = 1;
update(fa[a[i]+1]);
}
ans = sum;
for(int i = 0;i < n;i++)
{
sum += n - 1 - a[i] * 2;
ans = min(ans,sum);
}
printf("%lld\n",ans);
}
return 0;
}


暴力直接求版

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 5005;
int a[maxn];
int main()
{
//freopen("in.txt","r",stdin);
int n;
while(~scanf("%d",&n))
{
long long ans = 0,res = 0;
for(int i = 0;i < n;i++)scanf("%d",&a[i]);
for(int i = 0;i < n;i++)
for(int j = i+1;j < n;j++)
if(a[i] > a[j]) ans++;
res = ans;
for(int i = 0;i < n;i++)
{
ans -= a[i]-(n-1-a[i]);
res = min(res,ans);
}
printf("%lld\n",res);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息