HDU ~ 1394 ~ Minimum Inversion Number(暴力||归并排序||线段树||树状数组)

本文介绍了一种算法问题——最小逆序数的求解方法,包括暴力解法、归并排序解法、线段树解法及树状数组解法,并详细解释了每种方法的实现原理。

题目网址:Minimum Inversion Number

Minimum Inversion Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

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.
 

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
 

Recommend
Ignatius.L   |   We have carefully selected several similar problems for you:   1166  1698  1540  1542  1255 
 

题意:给你一个n,然后给你n个数字(0-n-1不重不漏!!!)的一个序列,你会进行n-1次变换每次把第一个数字放在最后一位,然后问你这些所有的序列的逆序数中最小的值

思路:首先对于这四种方法共同点都是他们都只求一次原始序列的逆序数,如果每变换一次求一次肯定超时;为什么只需要求一次呢,这个就这n个数字不重不漏有很大关系。

假设当前序列的逆序数为ans,则下一个序列的逆序数为:ans=ans-a[i]+(n-1-a[i]);         a[i]就是原始序列的第几位数字

假如上一个序列逆序数为ans,n==10,第一个数字为3,那么我们把3放在最后边那么3的逆序数变为0,那么3的逆序数为多少,因为序列中数字不重不漏,3在第一位,后边比他小的数字也就有3个0,1,2正好是这个数字的大小,所以ans=ans-a[i],3放在后边之后,比3大的数字的逆序数都加了1,同样因为不重不漏,所以比三大的数字有n-1-a[i]个,

ans=ans-a[i]+(n-1-a[i]);这样我们在求出来原始序列的逆序数后,只需要花费O(n)的复杂度就能求出来答案

暴力解法:

直接挨个求每个数字的逆序数加起来得到整个序列的逆序数;

#include <bits/stdc++.h> 
using namespace std;
const int maxn=5005;
int main()  
{  
    int n,a[maxn],ans;  
    while(~scanf("%d",&n))  
    {  
        for(int i=0;i<n;i++) scanf("%d",&a[i]);
        ans=0;
        for(int i=0;i<n;i++)
		{
			for(int j=i+1;j<n;j++)  
            {  
                if(a[i]>a[j]) ans++;  
            }  
		}  
        int MIN=ans;  
        for(int i=0;i<n;i++)  
        {  
        	ans=ans-a[i]+(n-1-a[i]);  
           	MIN=min(MIN,ans);
        }  
        printf("%d\n",MIN);  
    }  
    return 0;  
} 
归并排序解法:

归并排序解法,我们需要多开两个数组,一个b数组存一下原始序列,一个原始序列a数组然后进行归并排序,另一个是归并排序中用的数组temp;然后我们在归并排序过程中进行统计逆序数,当a[i]>a[j],即a[i]<a[i+1]<...<a[mid-1]<a[mid],共mid-i+1个数

#include <bits/stdc++.h> 
using namespace std;
const int maxn=5005;
int n,a[maxn],b[maxn],temp[maxn],ans;
void merge(int l,int mid,int r)
{
	int pos=0,i=l,j=mid+1;
	while(i<=mid&&j<=r)
	{
		if(a[i]<=a[j]) temp[pos++]=a[i++];
		else
		{
			ans+=mid+1-i;temp[pos++]=a[j++];
		}
	}
	while(i<=mid) temp[pos++]=a[i++];
	while(j<=r) temp[pos++]=a[j++];
	for(i=0;i<pos;i++) a[i+l]=temp[i];
} 
void merge_sort(int l,int r)
{
	if(l==r) return ;
	int mid=(l+r)/2;
	merge_sort(l,mid);
	merge_sort(mid+1,r);
	merge(l,mid,r);
}
int main()
{
	while(~scanf("%d",&n))
	{
		for(int i=0;i<n;i++)
		{
			scanf("%d",&a[i]);b[i]=a[i];
		}
		ans=0;
		merge_sort(0,n-1);
		int MIN=ans;
		for(int i=0;i<n;i++)
		{
			ans=ans-b[i]+(n-1-b[i]);
			MIN=min(MIN,ans);
		}
		cout<<MIN<<endl;
	}
	return 0;
}
线段树解法:

线段树的那个数组里面维护的是这个区间内数字出现次数,所以我们在建立线段树的时候只需要将所有的值都赋0就行了,然后每次输入一个数字a[i]就查询一下a[i]到n-1的区间和,也就是在后面有多少个比a[i]大的数字也就是a[i]的逆序数,统计一下就是原始序列的逆序数。然后在对线段树进行一下单点更新;

#include <bits/stdc++.h> 
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn=5005;
int sum[maxn<<2],a[maxn];
void PushUp(int rt)
{
	sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt)
{
	sum[rt]=0;
	if(l==r) return ;
	int m=(l+r)>>1;
	build(lson);
	build(rson);
}
void update(int pos,int l,int r,int rt)
{
	if(l==r)
	{
		sum[rt]++;return ;
	}
	int m=(l+r)>>1;
	if(pos<=m) update(pos,lson);
	else update(pos,rson);
	PushUp(rt);
}
int query(int L,int R,int l,int r,int rt)
{
	if(L<=l&&r<=R)
	{
		return sum[rt];
	}
	int m=(l+r)>>1,ret=0;
	if(L<=m) ret+=query(L,R,lson);
	if(R>m) ret+=query(L,R,rson);
	return ret;
}
int main()
{
	int n;
	while(cin>>n)
	{
		build(0,n-1,1);
		int ans=0;
		for(int i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
			ans+=query(a[i],n-1,0,n-1,1);
			update(a[i],0,n-1,1);
		}
		int MIN=ans;
		for(int i=0;i<n;i++)
		{
			ans=ans+(n-a[i]-1)-a[i];
			MIN=min(MIN,ans);
		}
		cout<<MIN<<endl;
	}
	return 0;
}
树状数组解法:

我感觉树状数组解法跟线段树有点像,树状数组里面存的是前n个数字中数字出现的次数(因为会有0这个数字而且树状数组中一般不用0这个位置所以我们将每个a[i]都加1),对于i位置的数字a[i],比a[i]小的数字有Sum(a[i])个,所以前面有i-1-Sum(a[i])个数字比a[i]大;

#include <bits/stdc++.h> 
using namespace std;
const int maxn=5005;
int n,a[maxn],c[maxn];
int lowbit(int x)
{
	return x&(-x);
}
int Sum(int x)
{
	int s=0;
	while(x>0)
	{
		s+=c[x];
		x-=lowbit(x);
	}
	return s;
}
void add(int pos,int value)
{
	while(pos<=n)
	{
		c[pos]+=value;
		pos+=lowbit(pos);
	}
}
int main()
{
	while(cin>>n)  
    {  
        memset(c,0,sizeof(c));  
        int ans=0;  
        for(int i=1;i<=n;i++)  
        {  
            scanf("%d",&a[i]);
            ans+=i-1-Sum(a[i]);
			add(a[i]+1,1); 
        }
        int MIN=ans;  
        for(int i=1;i<=n;i++)  
        {  
            ans=ans-a[i]+(n-1-a[i]);  
            MIN=min(MIN,ans);  
        }  
        cout<<MIN<<endl;  
    }  
	return 0;
}




### HDU OJ Problem 2566 Coin Counting Solution Using Simple Enumeration and Generating Function Algorithm #### 使用简单枚举求解硬币计数问题 对于简单的枚举方法,可以通过遍历所有可能的组合方式来计算给定面额下的不同硬币组合数量。这种方法虽然直观但效率较低,在处理较大数值时性能不佳。 ```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int[] coins = {1, 2, 5}; // 定义可用的硬币种类 while (scanner.hasNext()) { int targetAmount = scanner.nextInt(); int countWays = findNumberOfCombinations(targetAmount, coins); System.out.println(countWays); } } private static int findNumberOfCombinations(int amount, int[] denominations) { if (amount == 0) return 1; if (amount < 0 || denominations.length == 0) return 0; // 不使用当前面值的情况 int excludeCurrentDenomination = findNumberOfCombinations(amount, subArray(denominations)); // 使用当前面值的情况 int includeCurrentDenomination = findNumberOfCombinations(amount - denominations[0], denominations); return excludeCurrentDenomination + includeCurrentDenomination; } private static int[] subArray(int[] array) { if (array.length <= 1) return new int[]{}; return java.util.Arrays.copyOfRange(array, 1, array.length); } } ``` 此代码实现了通过递归来穷尽每一种可能性并累加结果的方式找到满足条件的不同组合数目[^2]。 #### 利用母函数解决硬币计数问题 根据定义,可以将离散序列中的每一个元素映射到幂级数的一个项上,并利用这些多项式的乘积表示不同的组合情况。具体来说: 设 \( f(x)=\sum_{i=0}^{+\infty}{a_i*x^i}\),其中\( a_i \)代表当总金额为 i 时能够组成的方案总数,则有如下表达式: \[f_1(x)=(1+x+x^2+...)\] 这实际上是一个几何级数,其封闭形式可写作: \[f_1(x)=\frac{1}{(1-x)}\] 同理,对于其他类型的硬币也存在类似的生成函数。因此整个系统的生成函数就是各个单独部分之积: \[F(x)=f_1(x)*f_2(x)...*f_n(x)\] 最终目标是从 F(x) 中提取系数即得到所需的结果。下面给出基于上述理论的具体实现: ```cpp #include<iostream> using namespace std; const int MAXN = 1e4 + 5; int dp[MAXN]; void solve() { memset(dp, 0, sizeof(dp)); dp[0] = 1; // 初始化基础状态 int values[] = {1, 2, 5}, size = 3; for (int j = 0; j < size; ++j){ for (int k = values[j]; k <= 10000; ++k){ dp[k] += dp[k-values[j]]; } } } int main(){ solve(); int T; cin >> T; while(T--){ int n; cin>>n; cout<<dp[n]<<endl; } return 0; } ``` 这段 C++ 程序展示了如何应用动态规划技巧以及生成函数的概念高效地解决问题实例[^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值