HDU 6318 Swaps and Inversions (2018 Multi-University Training Contest 2)

本文介绍了一道关于逆序数的问题,通过归并排序计算逆序数,并根据给定条件选择最优策略来减少花费。重点在于理解逆序数的概念及其实现方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Swaps and Inversions

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1733    Accepted Submission(s): 637

 

Problem Description

Long long ago, there was an integer sequence a.
Tonyfang think this sequence is messy, so he will count the number of inversions in this sequence. Because he is angry, you will have to pay x yuan for every inversion in the sequence.
You don't want to pay too much, so you can try to play some tricks before he sees this sequence. You can pay y yuan to swap any two adjacent elements.
What is the minimum amount of money you need to spend?
The definition of inversion in this problem is pair (i,j) which 1≤i<j≤n and ai>aj.

Input

There are multiple test cases, please read till the end of input file.
For each test, in the first line, three integers, n,x,y, n represents the length of the sequence.
In the second line, n integers separated by spaces, representing the orginal sequence a.
1≤n,x,y≤100000, numbers in the sequence are in [−109,109]. There're 10 test cases.

Output

For every test case, a single integer representing minimum money to pay.

Sample Input

3 233 666

1 2 3

3 1 666

3 2 1

Sample Output

0

3

题目大意:

有一串长度为n的数字,现在给出x和y,这一串数字里每存在一个逆序数(例如样例2:3 2 1,3的逆序数是0,2的逆序数是1,1的逆序数是2),就要付出x元的代价,当然你也可以交换相邻的两个数字来减少逆序数,每交换一次需要支付y元。

解法:

科普:逆序数的意思是一串数字中,当前数字的前面有多少个比它大的数字,总的逆序数就是全部数字逆序数的总和。

其实写多几组数据就会发现每交换两个数字(前大后小),逆序数就会相应的减少1,所以我们可以推出,逆序数=交换次数,其实线性代数学好了,这题还是很容易理解的。

所以我们只需要求出逆序数,判断x和y的大小,求逆序数只需要套用归并排序模板就可以愉快的AC了,附上代码:

#include <bits/stdc++.h>
#define ll long long
#define MAXN 100005
using namespace std;

ll a[MAXN],tep[MAXN];// a为原数组,tep为临时数组

ll merge(ll low,ll mid,ll high){
	ll i=low,j=mid+1,k=low;
	ll count=0;
	while(i<=mid&&j<=high){
		if(a[i]<=a[j])
			tep[k++]=a[i++];
		else{
			tep[k++]=a[j++];
			count+=j-k;// 每当后段的数组元素提前时,记录提前的距离
		}
	}
	while(i<=mid)
		tep[k++]=a[i++];
	while(j<=high)
		tep[k++]=a[j++];
	for(i=low;i<=high;i++)// 写回原数组
		a[i]=tep[i];
	return count;
}

ll mergeSort(ll a,ll b){
	if(a<b){
		ll mid=(a+b)/2;
		ll count=0;
		count+=mergeSort(a,mid);
		count+=mergeSort(mid+1,b);
		count+=merge(a,mid,b);
		return count;
	}
	return 0;
}

int main(){
	long long x,y,n;
	while(scanf("%lld%lld%lld",&n,&x,&y)!=EOF){
		for(ll i=0;i<n;i++)
			scanf("%lld",&a[i]);
		printf("%lld\n",mergeSort(0,n-1)*min(x,y));
	}
}

 

HDU 2034 是一道经典的 A-B Problem 题目,通常涉及简单的数学运算或者字符串处理逻辑。以下是对此类问题的分析以及可能的解决方法。 ### HDU 2034 的题目概述 该题目要求计算两个数之间的差值 \(A - B\) 并输出结果。需要注意的是,输入数据可能存在多种情况,因此程序需要能够适应不同的边界条件和特殊情况[^1]。 #### 输入描述 - 多组测试数据。 - 每组测试数据包含两行,分别表示整数 \(A\) 和 \(B\)。 #### 输出描述 对于每组测试数据,输出一行表示 \(A - B\) 的结果。 --- ### 解决方案 此类问题的核心在于正确读取多组输入并执行减法操作。以下是实现此功能的一种常见方式: ```python while True: try: a = int(input()) b = int(input()) print(a - b) except EOFError: break ``` 上述代码片段通过循环不断接收输入直到遇到文件结束符 (EOF),适用于批量处理多组测试数据的情况。 --- ### 特殊考虑事项 尽管基本思路简单明了,在实际编码过程中仍需注意以下几点: 1. **大数值支持**:如果题目中的 \(A\) 或 \(B\) 可能非常大,则应选用可以容纳高精度的数据类型来存储这些变量。 2. **负数处理**:当 \(B>A\) 导致结果为负时,确保程序不会因符号错误而失效。 3. **异常捕获**:为了防止运行期间由于非法字符或其他意外状况引发崩溃,建议加入必要的错误检测机制。 --- ### 示例解释 假设给定如下样例输入: ``` 5 3 7 2 ``` 按照以上算法流程依次完成各步操作后得到的结果应当分别为 `2` 和 `5`。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

KnightHONG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值