Swaps and Inversions

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

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
 
题目大意:
求给定序列的逆序数。
 
维护一个和给定序列一样大的初始全为0的数组。从大到小依次取数列中的数,将其对应的位置置为1,并查询前面有多少个1,则逆序数加上这个数。
用线段树可以方便地查询区间1的个数。
注意从大到小取数的时候,如果两个数一样大,则先取位置靠后的。这样逆序数就不会多算了。在排序中实现这个构思。
 
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<stack> using namespace std; const int maxn=; struct tnode
{
int num;//值
int seq;//序号
bool operator<(const tnode& y) const
{
if(num==y.num)
return seq<y.seq;
return num<y.num;
}
};
tnode node[maxn+]; struct ttree
{
int l,r;
int sum;
};
ttree tree[maxn*+]; void pushup(int x)
{
if(tree[x].l==tree[x].r)
return;
tree[x].sum=tree[x*].sum+tree[x*+].sum;
} void build(int x,int l,int r)
{
tree[x].l=l;
tree[x].r=r;
tree[x].sum=;
if(l<r)
{
int mid=(l+r)/;
build(x*,l,mid);
build(x*+,mid+,r);
}
} void modify(int x,int pos)
{
if(tree[x].l==tree[x].r)
tree[x].sum=;
else
{
int mid=(tree[x].l+tree[x].r)/;
if(pos<=mid)
modify(x*,pos);
else
modify(x*+,pos);
pushup(x);
}
} int query(int x,int l,int r)
{
if(l<=tree[x].l&&tree[x].r<=r)
return tree[x].sum;
int ret=;
int mid=(tree[x].l+tree[x].r)/;
if(l<=mid)
ret+=query(x*,l,r);
if(r>mid)
ret+=query(x*+,l,r);
return ret;
} int main()
{
int n,x,y;
while(scanf("%d%d%d",&n,&x,&y)!=EOF)
{
for(int i=;i<=n;i++)
{
scanf("%d",&node[i].num);
node[i].seq=i;
}
sort(node+,node+n+); long long ans=;
build(,,n);
for(int i=n;i>=;i--)
{
ans+=1LL*query(,,node[i].seq);
modify(,node[i].seq);
} printf("%lld\n",ans*min(x,y));
}
return ;
}

hdu 6318 Swaps and Inversions (线段树求逆序对数)的更多相关文章

  1. 4163 hzwer与逆序对 (codevs + 权值线段树 + 求逆序对)

    题目链接:http://codevs.cn/problem/4163/ 题目:

  2. BNU 2418 Ultra-QuickSort (线段树求逆序对)

    题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=2418 解题报告:就是给你n个数,然后让你求这个数列的逆序对是多少?题目中n的范围是n & ...

  3. HDU 4911 http://acm.hdu.edu.cn/showproblem.php?pid=4911(线段树求逆序对)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911 解题报告: 给出一个长度为n的序列,然后给出一个k,要你求最多做k次相邻的数字交换后,逆序数最少 ...

  4. SGU 180 Inversions(离散化 + 线段树求逆序对)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=180 解题报告:一个裸的求逆序对的题,离散化+线段树,也可以用离散化+树状数组.因为 ...

  5. hdu1394(线段树求逆序对)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 线段树功能:update:单点增减 query:区间求和 分析:如果是0到n-1的排列,那么如果 ...

  6. poj2299 Ultra-QuickSort(线段树求逆序对)

    Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...

  7. HDU 6318 - Swaps and Inversions - [离散化+树状数组求逆序数][杭电2018多校赛2]

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=6318 Problem Description Long long ago, there was an ...

  8. HDU 1394 线段树求逆序对

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

  9. FZU2018级算法第五次作业 m_sort(归并排序或线段树求逆序对)

    首先对某人在未经冰少允许情况下登录冰少账号原模原样复制其代码并且直接提交的赤裸裸剽窃行为,并且最终被评为优秀作业提出抗议! 题目大意: 给一个数组含n个数(1<=n<=5e5),求使用冒泡 ...

  10. POJ 2188线段树求逆序对

    题目给的输入是大坑,算法倒是很简单-- 输入的是绳子的编号wire ID,而不是上(或下)挂钩对应下(或上)挂钩的编号. 所以要转换编号,转换成挂钩的顺序,然后再求逆序数. 知道了这个以后直接乱搞就可 ...

随机推荐

  1. go中的关键字-go(上)

    1. goroutine的使用 在Go语言中,表达式go f(x, y, z)会启动一个新的goroutine运行函数f(x, y, z),创建一个并发任务单元.即go关键字可以用来开启一个gorou ...

  2. RAM、ROM和fFLASH相关概念整理

    一:ROM ROM:Read Only Memory.只读存储器    是一种半导体内存,又叫做非挥发性内存.其特性是一旦数据被存储就无法再将之改变或删除.存储的数据不会因为电源关闭而消失.   二: ...

  3. 迈进java初中级程序员分水岭是否合格?十个题告诉你!

    前言 不论你是职场新人还是步入职场N年的职场新人大哥大~当然这个N<3~,我能担保你答不对这十个题~不要问我为什么这么自信~,这些个题还是"有水平"的javase的基础题,传 ...

  4. Makefile使用指南

    转载请标明出处:http://blog.csdn.net/shensky711/article/details/52231202 本文出自: [HansChen的博客] 什么是Makefile Mak ...

  5. 【Android - 进阶】之PopupWindow的使用

    创建一个类继承自PopupWindow,编写自定义的PopupWindow类.示例代码如下: import android.app.Activity; import android.graphics. ...

  6. exc_bad_instruction(code=EXC_I386_INVOP,subcode=0x0) 错误

    对象存储异常 对象存储要遵守NSCoding协议 #import "EmotionModel.h" @interface EmotionModel()<NSCoding> ...

  7. 【灵魂拷问】你真的懂得Mysql的管理和使用吗?

    作者 | Jeskson 来源 | 达达前端小酒馆 MySQL管理,数据库管理和数据表管理,用户管理. 初始化数据库,创建数据库,查看数据库,删除数据库. 创建数据表,查看数据表,修改数据表,删除数据 ...

  8. Ctrl + Shift + F7 ; F3、Shift + F3

    pycharm 查找并高亮参数 选中某一参数,Ctrl + Shift + F7  高亮所有该文件中所有该参数 接下来, 按 F3 在所有高亮选择中向下移动一个, Shift + F3 在所有高亮选择 ...

  9. Selenium 4 Java的最佳测试框架

    几十年来,Java一直是开发应用程序服务器端的首选编程语言.尽管JUnit一直在与开发人员一起帮助他们进行自动化的单元测试,但随着时间的推移和测试行业的发展,特别是伴随着自动化测试的兴起,已经开发了许 ...

  10. 浏览器主页锁定之战——IE:我太难了

    精彩回顾: 我是一个explorer的线程 我是一个杀毒软件线程 我是一个IE浏览器线程 比特宇宙-TCP/IP的诞生 产品vs程序员:你知道www是怎么来的吗? Hello, World! 我是一个 ...