Algorithm学习笔记 --- Minimum Inversion Number
来源:互联网 发布:python日期格式 编辑:程序博客网 时间:2024/06/15 22:22
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
Author
CHEN, Gaoli
Source
ZOJ Monthly, January 2003
题意:此题的意思是让你输入n个数求出经过n-1次变换的最小的逆叙述。
解题分析:
此题可以多种解法:
1.暴力求解:
思路是这样的,先输入一长串数字,存入数组,然后用双重
for(int i=0;i<n;i++)
{
for(int j=i-1;j>=0;j--)
{
{
if(a[j]>a[i])
count++;//几下一共有多少个。
}
}
求出每输入一个a[i]之后a[i]前面有多少个数比a[i]大并且在a[i]之前,每存在一个就用计数器count存起来。
这是求出的原数列的你叙述。时间复杂度为O(n*n)。然后继续进行比较经过n-1次变换的每个数列的最小逆序数。
求经过变化的每次逆序数的公式是:
for(int i=0;i<n-1;i++)
{
count=count-a[i]+(n-a[i]-1);
if(count<MIN) //每次变化后进行比较,如果比当前的逆序数小的话就输出较小的逆序数。
MIN=count;
{
count=count-a[i]+(n-a[i]-1);
if(count<MIN) //每次变化后进行比较,如果比当前的逆序数小的话就输出较小的逆序数。
MIN=count;
}
代码如下:
#include <iostream>
#include <stdio.h>
#define MAXN 5010
using namespace std;
int main()
{
int n,a[MAXN];
while(scanf("%d",&n)!=EOF)
{
int count=0;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
for(int j=i-1;j>=0;j--)
{
if(a[j]>a[i])
{
count++;
}
}
}
int MIN=count;
for(int i=0;i<n-1;i++)
{
count=count-a[i]+(n-a[i]-1);
if(count<MIN)
MIN=count;
}
cout<<MIN<<endl;
}
return 0;
}
#include <stdio.h>
#define MAXN 5010
using namespace std;
int main()
{
int n,a[MAXN];
while(scanf("%d",&n)!=EOF)
{
int count=0;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
for(int j=i-1;j>=0;j--)
{
if(a[j]>a[i])
{
count++;
}
}
}
int MIN=count;
for(int i=0;i<n-1;i++)
{
count=count-a[i]+(n-a[i]-1);
if(count<MIN)
MIN=count;
}
cout<<MIN<<endl;
}
return 0;
}
2.线段树优化:
解题思路:
当每次数列变化的时候更新节点,然后区间求和:
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define MAXN 5010
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int cut[MAXN<<2];
void pushup(int rt)
{
cut[rt]=cut[rt<<1]+cut[rt<<1|1];
}
void build(int l,int r,int rt)
{
cut[rt]=0;
if(l==r)
return;
int m=(l+r)>>1;
build(lson);
build(rson);
}
void update(int p,int l,int r,int rt)
{
if(l==r)
{
cut[rt]++;
return ;
}
int m=(l+r)>>1;
if(p<=m)
update(p,lson);
else
update(p,rson);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
if(L<=l && r<=R)
{
return cut[rt];
}
int m=(l+r)>>1;
int ret=0;
if(L<=m)
ret+=query(L,R,lson);
if(R>=m)
ret+=query(L,R,rson);
return ret;
}
int a[MAXN];
int main()
{
int n;
while(~scanf("%d",&n))
{
build(0,n-1,1);
int sum=0;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum+=query(a[i],n-1,0,n-1,1);
update(a[i],0,n-1,1);
}
int ret=sum;
for(int i=0;i<n-1;i++)
{
sum+=n-a[i]-a[i]-1;
ret=min(ret,sum);
}
cout<<ret<<endl;
}
return 0;
}
#include <cstdio>
#include <algorithm>
using namespace std;
#define MAXN 5010
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int cut[MAXN<<2];
void pushup(int rt)
{
cut[rt]=cut[rt<<1]+cut[rt<<1|1];
}
void build(int l,int r,int rt)
{
cut[rt]=0;
if(l==r)
return;
int m=(l+r)>>1;
build(lson);
build(rson);
}
void update(int p,int l,int r,int rt)
{
if(l==r)
{
cut[rt]++;
return ;
}
int m=(l+r)>>1;
if(p<=m)
update(p,lson);
else
update(p,rson);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
if(L<=l && r<=R)
{
return cut[rt];
}
int m=(l+r)>>1;
int ret=0;
if(L<=m)
ret+=query(L,R,lson);
if(R>=m)
ret+=query(L,R,rson);
return ret;
}
int a[MAXN];
int main()
{
int n;
while(~scanf("%d",&n))
{
build(0,n-1,1);
int sum=0;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum+=query(a[i],n-1,0,n-1,1);
update(a[i],0,n-1,1);
}
int ret=sum;
for(int i=0;i<n-1;i++)
{
sum+=n-a[i]-a[i]-1;
ret=min(ret,sum);
}
cout<<ret<<endl;
}
return 0;
}
0 0
- Algorithm学习笔记 --- Minimum Inversion Number
- HDUOJ Minimum Inversion Number
- hdu1394 Minimum Inversion Number
- hdu1394 Minimum Inversion Number
- hdu1394 Minimum Inversion Number
- Minimum Inversion Number
- HDU1394:Minimum Inversion Number
- HDU1394:Minimum Inversion Number
- HDU1394--Minimum Inversion Number
- Minimum Inversion Number HDU
- HDU Minimum Inversion Number
- Minimum Inversion Number
- Minimum Inversion Number
- Minimum Inversion Number
- hdu1394 Minimum Inversion Number
- hdu1394 Minimum Inversion Number
- hdu1394 Minimum Inversion Number
- hdu1394 Minimum Inversion Number
- 通过strerror获取详细的错误信息
- 【Yii】表单模型和验证
- Java采用JNI调用VC++生成的dll(Java与C++交互)
- vi编辑器使用技巧
- 如何下载RFC文档?
- Algorithm学习笔记 --- Minimum Inversion Number
- 【Yii】AR模型和db
- 《C和指针》——C语言补漏(指针篇)
- Linux经典实用技巧 -
- java自学之路-----Map jdk1.5加的功能 System、Math、Math、Date
- 【原创】Ubuntu终端窗口大小设置和终端字体大小设置
- Web-Service-调用服务的方式
- vs中控制台程序输出时一闪而过的解决办法
- Linux编程工具C编译器和C编程工具介绍(1) -