hdu 6318 Swaps and Inversions (线段树求逆序对数)
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
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.
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.
1 2 3
3 1 666
3 2 1
3
#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 (线段树求逆序对数)的更多相关文章
- 4163 hzwer与逆序对 (codevs + 权值线段树 + 求逆序对)
题目链接:http://codevs.cn/problem/4163/ 题目:
- BNU 2418 Ultra-QuickSort (线段树求逆序对)
题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=2418 解题报告:就是给你n个数,然后让你求这个数列的逆序对是多少?题目中n的范围是n & ...
- HDU 4911 http://acm.hdu.edu.cn/showproblem.php?pid=4911(线段树求逆序对)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911 解题报告: 给出一个长度为n的序列,然后给出一个k,要你求最多做k次相邻的数字交换后,逆序数最少 ...
- SGU 180 Inversions(离散化 + 线段树求逆序对)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=180 解题报告:一个裸的求逆序对的题,离散化+线段树,也可以用离散化+树状数组.因为 ...
- hdu1394(线段树求逆序对)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 线段树功能:update:单点增减 query:区间求和 分析:如果是0到n-1的排列,那么如果 ...
- poj2299 Ultra-QuickSort(线段树求逆序对)
Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...
- HDU 6318 - Swaps and Inversions - [离散化+树状数组求逆序数][杭电2018多校赛2]
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=6318 Problem Description Long long ago, there was an ...
- HDU 1394 线段树求逆序对
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- FZU2018级算法第五次作业 m_sort(归并排序或线段树求逆序对)
首先对某人在未经冰少允许情况下登录冰少账号原模原样复制其代码并且直接提交的赤裸裸剽窃行为,并且最终被评为优秀作业提出抗议! 题目大意: 给一个数组含n个数(1<=n<=5e5),求使用冒泡 ...
- POJ 2188线段树求逆序对
题目给的输入是大坑,算法倒是很简单-- 输入的是绳子的编号wire ID,而不是上(或下)挂钩对应下(或上)挂钩的编号. 所以要转换编号,转换成挂钩的顺序,然后再求逆序数. 知道了这个以后直接乱搞就可 ...
随机推荐
- 概率的基本概念&离散型随机变量
使用excel可以直接计算二项分布和超几何分布:
- react-router重定向
① ②通过this.props重定向
- pycharm设置python脚本模板
PyCharm PyCharm是一个有名的Python IDE,带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,比如调试.语法高亮.Project管理.代码跳转.智能提示.自动完成 ...
- ASP.NET Aries 高级开发教程:如何写WebAPI接口
前提: 最近,有不少同学又问到,Aries里如何提供WebAPI接口? 针对这个问题,今天给顺路写个教程,其实呢,很简单的. 方式一:直接用WebService提供接口. 用这种方式,直接添加接口就可 ...
- PostGIS 爆管分析之找出上游阀门
环境: Win10 ArcMap10.4(用于数据处理) postgresql9.4 postgis2.2.3 pgRouting2.3(postgresql插件) 说明: 继上一篇文章做了爆管分析找 ...
- Head First设计模式——适配器和外观模式
前言:为什么要一次讲解这两个模式,说点骚话:因为比较简单(*^_^*),其实是他们两个有相似和有时候我们容易搞混概念. 讲到这两个设计模式与另外一个“装饰者模式”也有相似,他们三个按照结构模式分类都属 ...
- kali linux 修改更新源和更新命令
1.修改sources.list源文件: vim /etc/apt/sources.list #aliyun 阿里云 deb http://mirrors.aliyun.com/kali kali-r ...
- Java包package之间调用问题-cmd运行窗口编译运行
问题:在使用了java包机制(package)后,编译出现错误:找不到或无法加载主类 xxx 的错误提示信息(各种编译不通过) 先给演示结果: 编译:javac -d classes src/a/He ...
- VS Code 中文社区正式成立啦!VS Code Day 圆满落幕!
背景简介 Visual Studio Code 是一款现代化轻量级代码编辑器,它免费.开源.跨平台.功能强大.本次 VS Code Day 是广大 VS Code 爱好者一起学习与交流的盛会,让我们对 ...
- 利用堆来处理Top K问题
目录 一.什么是Top K问题 二.Top K的实际应用场景 三.Top K问题的代码实现及其效率对比 1.用堆来实现Top K 2.用快排来实现Top K 3.用堆或用快排来实现 TopK 的效率对 ...