题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=6318

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 [−1e9,1e9]. 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个元素的序列,现在你有机会,花费y元交换一次任意相邻两个元素,交换完毕后,若存在逆序对,每一个逆序对要花费x元,求最少的花费。

题解:

考虑冒泡排序:

  1. 比较相邻的元素。如果第一个比第二个大,就交换他们两个。

  2. 对每一对相邻元素做同样的工作,从开始第一对到结尾的最后一对。做完一遍,最后的元素是最大的数。

  3. 针对所有的元素重复以上的步骤,除了最后一个(因为确定是最大的)。

  4. 持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。

显然,我们每交换一次两个相邻元素,就必然能使逆序数减1,而排序完毕后逆序数等于0,所以冒泡排序交换相邻元素次数等于逆序数。

而且同样易知,我们不可能用更少的交换次数使得逆序数等于0。

所以我们就得到了如下结论:

  逆序数 = 在只能交换相邻元素条件下,使得序列有序的最少交换次数

而本题中,交换一次花费x,一个逆序对花费y,也就是说,要么全部交换直到没有逆序对,要么一次也不交换。

所以我们只要求出逆序数k,答案ans = k * min(x,y)。

由于本题数值范围[-1e9,1e9],而n最多1e5,树状数组不可能开2e9,所以只能进行离散化。

时间复杂度:sort函数O(nlogn),unique函数O(n),暴力枚举n个元素并获取ID以及树状数组修改+查询操作O(nlogn),总的O(nlogn),满足要求。

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=+; struct _BIT //单点修改、区间查询
{
int n,C[maxn];
int lowbit(int x){return x&(-x);}
void init(int n)
{
this->n=n;
memset(C,,sizeof(C));
}
void add(int pos,int val) //在pos点加上val
{
while(pos<=n)
{
C[pos]+=val;
pos+=lowbit(pos);
}
}
int ask(int pos) //查询1~pos点的和
{
int ret=;
while(pos>)
{
ret+=C[pos];
pos-=lowbit(pos);
}
return ret;
}
}BIT; int n;
ll x,y; int a[maxn]; vector<int> v;
inline int getID(int x){return lower_bound(v.begin(),v.end(),x)-v.begin()+;}
int main()
{
while(scanf("%d%d%d",&n,&x,&y)!=EOF)
{
v.clear();
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
v.push_back(a[i]);
}
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end()); BIT.init(v.size());
ll cnt=;
for(int i=;i<=n;i++)
{
int id=getID(a[i]);
BIT.add(id,);
cnt+=BIT.ask(v.size())-BIT.ask(id);
} cout<<cnt*min(x,y)<<endl;
}
}

HDU 6318 - Swaps and Inversions - [离散化+树状数组求逆序数][杭电2018多校赛2]的更多相关文章

  1. SGU180 Inversions(树状数组求逆序数)

    题目: 思路:先离散化数据然后树状数组搞一下求逆序数. 离散化的方法:https://blog.csdn.net/gokou_ruri/article/details/7723378 自己对用树状数组 ...

  2. hdu 1394 Minimum Inversion Number (裸树状数组 求逆序数 && 归并排序求逆序数)

    题目链接 题意: 给一个n个数的序列a1, a2, ..., an ,这些数的范围是0-n-1, 可以把前面m个数移动到后面去,形成新序列:a1, a2, ..., an-1, an (where m ...

  3. 牛客练习赛38 D 题 出题人的手环 (离散化+树状数组求逆序对+前缀和)

    链接:https://ac.nowcoder.com/acm/contest/358/D来源:牛客网 出题人的手环 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他 ...

  4. hdu 5147 Sequence II (树状数组 求逆序数)

    题目链接 Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. poj 2299 Ultra-QuickSort(树状数组求逆序数)

    链接:http://poj.org/problem?id=2299 题意:给出n个数,求将这n个数从小到大排序,求使用快排的需要交换的次数. 分析:由快排的性质很容易发现,只需要求每个数的逆序数累加起 ...

  6. HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number                         ...

  7. poj 2299 Ultra-QuickSort(树状数组求逆序数+离散化)

    题目链接:http://poj.org/problem?id=2299 Description In this problem, you have to analyze a particular so ...

  8. Codeforces645B【树状数组求逆序数】

    题意: 给你1-n的序列,然后有k次机会的操作,每一次你可以选择两个数交换. 求一个最大的逆序数. 思路: 感觉就是最后一个和第一个交换,然后往中间逼近,到最终的序列,用树状数组求一下逆序数. #in ...

  9. HDU 1394 Minimum Inversion Number(线段树/树状数组求逆序数)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

随机推荐

  1. create a cocos2d-x-3.0 project in Xcode

    STEP1: Open Terminal SETP2: Run setup.py SETP3: Run source /Users/your_user/.bash_profile( so that e ...

  2. 取值为[1,n-1]含n个元素的整数数组,至少存在一个重复数,即可能存在多个重复数,O(n)时间内找出其中任意一个重复数,不使用额外存储空间。

    有一种非常诡异的算法,就是采用类似于单链表是否存在环的问题.“判断单链表是否存在环”是一个非常经典的问题,同时单链表可以采用数组实现,此时每个元素值作为next指针指向下一个元素.本题可以转换化为“已 ...

  3. 5 -- Hibernate的基本用法 --2 1 Hibernate 下载和安装

    1. 下载Hibernate压缩包 2. 解压:文件结构 ⊙ documentation : 该路径下存放了Hibernate的相关文档,包括Hibernate的参考文档和API文档等. ⊙ lib ...

  4. ios开发之--tableview单选/多选实现(非tableview的editing状态)及默认选中

    实现思路比较简单,这里仅做记录: 直接上代码: 1,实现didSelectRowAtIndexPath方法 -(void)tableView:(UITableView *)tableView didS ...

  5. CentOS下安装高版本GCC

    CentOS下安装高版本GCC 微信分享:   有时编译需要用到4.8以上版本的GCC,由于CentOS源没有提供高版本的GCC安装包,这时就不能通过安装包安装.通常的解决方案就是通过编译安装高版本的 ...

  6. /etc/issue

    /etc/issue 与 /etc/motd 作用一致,都是用于显示欢迎信息,区别在于 /etc/issue 是在 login 提示符之前显示,而 /etc/motd 则在在用户成功登录系统之后显示 ...

  7. React Native(十二)——嵌套WebView中的返回处理

    情景描述: 从一个名为"My"的组件点击进去,进入一个列表(该列表内容为webView中内容),其中一个webView也可以点击进入详情页(也为webView),但是如果对导航栏不 ...

  8. 在wepy里面使用redux

    wepy 框架本身是支持 Redux 的,我们在构建项目的时候,将 是否安装 Redux 选择 y 就好了,会自动安装依赖,运行项目后看官方给的 demo 确实是可以做到的,但是官方文档里却对这一块只 ...

  9. make: Warning: File `Makefile' has modification time 17 s in the future

    linux下,make makefile文件的时候报警告: make: Warning: File `Makefile' has modification time 17 s in the futur ...

  10. Windows驱动中通过MDL实现用户态与核心态共享内存

    Windows驱动跑在核心态(Kernel mode),驱动的调用者跑在用户态.如何使用户态进程与核心态驱动共享内存呢 ? 我们知道32位Windows中,默认状态下虚拟空间有4G,前2G是每个进程私 ...