Swaps and Inversions

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

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

#include<cstdio>
#include<cstring>
#include<algorithm>
#define lowbit(x) x&(-x)
using namespace std; typedef long long ll;
const int maxn=100005;
struct node
{
int index;
int data;
bool operator < (const node &b) const
{
if(data==b.data) return index<b.index; //很重要,否则就会wa
return data<b.data;
}
}a[maxn];
int n,x,y;
ll ans;
int c[maxn];
void update(int x)
{
for(;x<=n;x+=lowbit(x))
{
c[x]++;
}
}
ll sum(int x)
{
ll tmp=0;
for(;x;x-=lowbit(x))
{
tmp+=c[x];
}
return tmp;
}
int main()
{
while(~scanf("%d%d%d",&n,&x,&y))
{
memset(a,0,sizeof(a));
memset(c,0,sizeof(c));
ans=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i].data);
a[i].index=i;
}
sort(a+1,a+1+n);
for(int i=n;i>0;i--)
{
ans+=sum(a[i].index-1);
update(a[i].index);
}
printf("%lld\n",ans*min(x,y));
}
return 0;
}

树状数组-逆序对-HDU6318的更多相关文章

  1. [树状数组+逆序对][NOIP2013]火柴排队

    火柴排队 题目描述 涵涵有两盒火柴,每盒装有n根火柴,每根火柴都有一个高度.现在将每盒中的火柴各自排成一列,同一列火柴的高度互不相同,两列火柴之间的距离定义为:∑ (ai-bi)2,i=1,2,3,. ...

  2. hdu 5497 Inversion 树状数组 逆序对,单点修改

    Inversion Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5497 ...

  3. Codevs 3286 火柴排队 2013年NOIP全国联赛提高组 树状数组,逆序对

    题目:http://codevs.cn/problem/3286/ 3286 火柴排队  2013年NOIP全国联赛提高组  时间限制: 1 s   空间限制: 128000 KB   题目等级 : ...

  4. Bzoj 2789: [Poi2012]Letters 树状数组,逆序对

    2789: [Poi2012]Letters Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 278  Solved: 185[Submit][Stat ...

  5. Bzoj 3295: [Cqoi2011]动态逆序对 分块,树状数组,逆序对

    3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2886  Solved: 924[Submit][Stat ...

  6. Bzoj 3289: Mato的文件管理 莫队,树状数组,逆序对,离散化,分块

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 1539  Solved: 665[Submit][Status][Di ...

  7. Poj 2299 - Ultra-QuickSort 离散化,树状数组,逆序对

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 52306   Accepted: 19194 ...

  8. hdu 2838 Cow Sorting (树状数组+逆序对)

    题目 题意:给你N个排列不规则的数,任务是把它从小到大排好,每次只能交换相邻两个数,交换一次的代价为两数之和,求最小代价 拿到这道题,我根本看不出这道题和树状数组有半毛钱关系,博客之,全说用树状数组做 ...

  9. 【树状数组逆序对】USACO.2011JAN-Above the median

    [题意] 给出一串数字,问中位数大于等于X的连续子串有几个.(这里如果有偶数个数,定义为偏大的那一个而非中间取平均) [思路] 下面的数据规模也小于原题,所以要改成__int64才行.没找到测试数据, ...

随机推荐

  1. vue-创建新项目

    1.安装node,下载地址:https://nodejs.org/en/download/ 2.安装完成后打开cmd,输入node-v,出现版本信息说明安装成功 3.输入npm install -g ...

  2. Java集合实现

    set: public class BSTSet<E extends Comparable<E>> implements Set<E> { private BST& ...

  3. css固定div头部 滚动条滚动内容

    页面布局,固定头部,滚动下方内容 实际场景 在制作页面的时候,经常会遇到要这样的情况:整个页面,整体分三大模块,头部固定,内容区域,左边固定,右边可以滚动. 最终想要的效果 案例源码 <!DOC ...

  4. sklearn交叉验证3-【老鱼学sklearn】

    在上一个博文中,我们用learning_curve函数来确定应该拥有多少的训练集能够达到效果,就像一个人进行学习时需要做多少题目就能拥有较好的考试成绩了. 本次我们来看下如何调整学习中的参数,类似一个 ...

  5. python接口测试-认识POST请求

    上午和一个大神交流了一下,给我了一点建议:多做笔记,勤复盘:及时记录自己,最好的提升不是来自于别人,而是来自于自身.我觉得挺有道理的,分享出来.共勉 说正事. 今天把post请求的大概内容看了一下.虽 ...

  6. LeetCode 929.Unique Email Addresses

    Description Every email consists of a local name and a domain name, separated by the @ sign. For exa ...

  7. IO操作、PIL使用、Django的admin操作

    操作内存的流-StringIO | BytesIO from io import StringIO, BytesIO #创建操作字符串的内存流 sf = StringsIO() #往内存中写 sf.w ...

  8. Redis新接触

    一.redis简介 redis即Remote Dictionary Server,是一个key—value存储系统. 二.优点 1.redis支持的存储类型较多,如String.List.Hash.s ...

  9. Spring Cloud微服务笔记(一)微服务概念

    微服务概念 一.什么是微服务架构 微服务,是一个小的.松耦合的分布式服务. 为什么需要微服务: 1)单体系统部署在一个进程中,修改了一个小功能,为了部署上线就会影响其他功能. 2)单体应用各个功能模块 ...

  10. 13树莓派手动安装Home Assistant

    2017-09-05 00:53:02 https://home-assistant.io/docs/installation/raspberry-pi/ 已经安装步骤安装了带桌面的树莓派系统,在SD ...