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. React动画库

    npm i react-transition --save import {CSSTransition} from 'react-transition-group'

  2. Mysql 添加字段 修改字段 删除字段

    1.添加字段(alter.add) mysql> alter table users add name varchar(30) not null after id; 2.修改字段(alter.m ...

  3. 20191107-7 beta week 2/2 Scrum立会报告+燃尽图 06

    此作业要求参见https://edu.cnblogs.com/campus/nenu/2019fall/homework/9959 一.小组情况 队名:扛把子 组长:孙晓宇 组员:宋晓丽 梁梦瑶 韩昊 ...

  4. 概率分布的python实现

    接上篇概率分布,这篇文章讲概率分布在python的实现. 文中的公式使用LaTex语法,即在\begin{equation}至\end{equation}的内容可以在https://www.codec ...

  5. Linux -- 进程管理之 fork() 函数

    一个进程调用fork()函数后,系统先给新的进程分配资源,例如存储数据和代码的空间.然后把原来的进程的所有值都复制到新的新进程中,只有少数值与原来的进程的值不同.相当于克隆了一个自己. Test1 f ...

  6. Xamarin.Forms学习系列之Android集成极光推送

    一般App都会有消息推送的功能,如果是原生安卓或者IOS集成消息推送很容易,各大推送平台都有相关的Sample,但是关于Xamarin.Forms的消息推送集成的资料非常少,下面就说下Xamarin. ...

  7. Spring(Bean)2

    <!-- util:list封装的心 --> <bean id="personList2" class="spring.beans.di.collect ...

  8. mysql 插入string类型变量时候,需要注意的问题,妈的,害我想了好几个小时!!

    很多人在用php+MySQL做网站往数据库插入数据时发现如下错误: 注册失败!Unknown column '1a' in 'field list' 结果发现用数字提交是没有问题的,其他如char型就 ...

  9. C语言l博客作业01

    2.1 你对软件工程专业或者计算机科学与技术专业了解是怎样?(2分) 计算机科学与技术是国家一级学科,下设信息安全,软件工程,计算机软件与理论等专业,主要培养符合教育部门实际需要的计算机教学及应用管理 ...

  10. Mysql密码忘记怎么修改?

    做开发的过程中多少会用到MySQL数据库,所以忘记密码也就成为一些马虎的同学的家常便饭了,今天发布一个忘记MySQL密码如何修改的文章作为记录. 1>首先将MySQL的服务关闭,两种方法:1,打 ...