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. css未知高度垂直居中

    <!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  2. 【NHOI2018】扑克游戏

    [问题描述] 有一种别样“小猫钓鱼”扑克游戏.有 N 张牌,每张牌都有一个花色和点数.游戏的规则:扑克接龙时,若前面有同样花色的牌,你可以将这两张牌连同之间的牌都取走,得到的分值为取走牌点数之和.这里 ...

  3. day 17 re模块 正则表达式

    import re    引用re模块 查找 finall:匹配所有,每一项都是列表中的一个元素 search:只匹配从左到右的第一个,得到的不是直接的结果而是一个变量,通过group方法获取结果,没 ...

  4. 攻略前端面试官(一):JS的数据类型和内存机制浅析

    原文地址:http://rainykane.cn/2019/09/29/与K_K君一起攻略前端面试官(一):JS的数据类型和内存机制浅析/ 背就完事了 介绍:一些知识点相关的面试题和答案 使用姿势:看 ...

  5. mac 终端高亮显示~

    针对terminal采用bash模式: 编辑 ~/.bash_profile, 加入以下代码: export CLICOLOR=1 export LSCOLORS=gxfxaxdxcxegedabag ...

  6. c++关于multiset的头文件包含问题

    最近在Bilibili上看到不少侯捷老师C++的视频教程,侯捷老师翻译了很多C++的经典书籍,比如<Essential C++中文版>.<STL源码剖析>,也写了<深入浅 ...

  7. Spring Boot2 系列教程(二十九)Spring Boot 整合 Redis

    经过 Spring Boot 的整合封装与自动化配置,在 Spring Boot 中整合Redis 已经变得非常容易了,开发者只需要引入 Spring Data Redis 依赖,然后简单配下 red ...

  8. 一条数据的HBase之旅,简明HBase入门教程3:适用场景

    [摘要] 这篇文章继HBase数据模型之后,介绍HBase的适用场景,以及与一些关键场景有关的周边技术生态,最后给出了本文的示例数据 华为云上的NoSQL数据库服务CloudTable,基于Apach ...

  9. request获取路径

    1.request.getRequestURL() 返回的是完整的url,包括Http协议,端口号,servlet名字和映射路径,但它不包含请求参数. 2.request.getRequestURI( ...

  10. 使用正则表达式实现(加减乘除)计算器(C#实现)

    起因:公司领导要求做一款基于行业规范的计算器, 然后需要用户输入一些数据,然后根据用户输入的数据满足某些条件后,再根据用户输入的条件二进行加减乘除运算.;-) 期间因为查找规范等形成数据表的某一列是带 ...