2018 Multi-University Training Contest 2 ---- hdu6318 Swaps and Inversions【离散化+归并排序】

Swaps and Inversions

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

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

题意:

给出了逆序对的定义,这个英文是真的有毒, 队友一直跟我争执我是不是看错题目了,害我也怀疑了一下自己。

给出一个数组的大小,数组内的每个数大小为-10的九次方到10的九次方,如果数组内出现一个逆序对你就要支付x元,你可以进行一个操作,交换相邻的两个数字,支付y元 ,请问最小花费多少?

思路:

不难发现,因为是交换相邻的两个数字,每次交换都是为了能减少逆序对,且只能减少一个,所以只要求出有多少个逆序对,答案再去乘以min(x,y),求逆序队的方法有两种, 一种是归并排序,一种是树状数组,比赛时敲了树状数组wa了几发,怀疑了人生,最后迫于无奈,写了归并排序,赛后才知道树状数组要离散化,不然数组数字太大过不了QAQ。

AC代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
#define N 100010
int c[N];
int aaa[N], bbb[N];
int n;
int lowbit(int i) {
	return i&(-i);
}
int insert(int i,int x) {
	while(i<=n) {
		c[i]+=x;
		i+=lowbit(i);
	}
	return 0;
}

int getsum(int i) {
	int sum=0;
	while(i>0) {
		sum+=c[i];
		i-=lowbit(i);
	}
	return sum;
}
void discre() {//离散化 
	map<int,int>ma;
	sort(bbb + 1,bbb + 1 + n);
	int cnt = 1;
	for(int i = 1; i <= n; i++) {
		if(i == 1) {
			ma[bbb[i]] = cnt;
		} else {
			if(bbb[i] != bbb[i - 1]) {
				ma[bbb[i]] = ++cnt;
			} else {
				ma[bbb[i]] = cnt;
			}
		}
	}
	for(int i = 1; i <= n; i++) {
		aaa[i] = ma[aaa[i]];
	}
}
int main() {
	int x, y;
	while(~scanf("%d%d%d",&n,&x,&y)) {
		ll ans=0;
		memset(c,0,sizeof(c));
		for(int i=1; i<=n; i++) {
			scanf("%d", &aaa[i]);
			bbb[i] = aaa[i];
		}
		discre();
		for(int i=1; i<=n; i++) {
			insert(aaa[i],1);
			ans += i-getsum(aaa[i]);
		}
		int z = min(x,y);
		ll k = z * ans;
		printf("%lld\n",k);
	}
	return 0;
}

 

全部评论

相关推荐

03-26 13:04
已编辑
电子科技大学 算法工程师
xiaowl:你这个简历“条目上”都比较有深度性,但是实际上面试官又没法很好的评估你是怎么达到很多看上去很厉害的结果的。要避免一些看上去很厉害的包装,比如高效的内存复用策略的表达,如果仅是简单的一些内存共享机制,而且面试上也没有深挖的空间,就不要这样表达。比如,工程化模式本质上可能就是定义了一些abstract class,那也就没特别多值得讲的内容。建议简历上应该侧重那些你花了大量时间和精力解决、研究的问题,不要过分追求“丰富”,而是关注在技术深入度、问题解决能力的表现上。
没有实习经历,还有机会进...
点赞 评论 收藏
分享
找工作勤劳小蜜蜂:自我描述部分太差,完全看不出想从事什么行业什么岗位,也看不出想在哪个地区发展,这样 会让HR很犹豫,从而把你简历否决掉。现在企业都很注重员工稳定性和专注性,特别对于热爱本行业的员工。 你实习的工作又太传统的it开发(老旧),这部分公司已经趋于被淘汰,新兴的互联网服务业,比如物流,电商,新传媒,游戏开发和传统的It开发有天然区别。不是说传统It开发不行,而是就业岗位太少,基本趋于饱和,很多老骨头还能坚持,不需要新血液。 工作区域(比如长三角,珠三角,成渝)等也是HR考虑的因素之一,也是要你有个坚定的决心。否则去几天,人跑了,HR会被用人单位骂死。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务