hdu 6318
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.
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 <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int N=1e5+;
typedef long long ll;
/*
例如:
4 2 3 1 有5个逆序对
可以先交换两次相邻位置 2 4 3 1 2 3 4 1 逆序对变成了5-2==3
可以先交换一次相邻位置 2 4 3 1 逆序对变成了5-1==4
分析:4 2 3 1 可以任意选两个相邻的数交换,如4 2 ,那么在交换4 2
时,1,3所对应的逆序对不会变(1,3,左边比他们大的数的数目不变)
但4 2 变成了2 4 因此这个逆序对没了。肯定把逆序对变成非逆序对。
y 对应一个逆序对 ,x 对应一个逆序对
因此 最小花费=逆序对*min(x,y) .
*/
// 注意到逆序对=交换相邻需要交换的次数
int a[N],b[N];
ll ans;
int n,x,y;
void gsort(int l,int r)
{ if(l==r) return ;
int mid=(r+l)>>;
gsort(l,mid);gsort(mid+,r);
int k=l;
int i=l,j=mid+;
while(i<=mid&&j<=r){
if(a[i]<=a[j]){
b[k++]=a[i++];
}
else{
b[k++]=a[j++];
ans+=mid-i+;
}
}
while(i<=mid) b[k++]=a[i++];
while(j<=r) b[k++]=a[j++];
for(int i=l;i<=r;i++) a[i]=b[i];
}
void solve()
{
ans=;
gsort(,n);
printf("%lld\n",ans*min(x,y));
}
int main()
{
while(~scanf("%d%d%d",&n,&x,&y)){
for(int i=;i<=n;i++) scanf("%d",&a[i]);
solve();
}
return ;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
#define ll long long
#define N 100009
#define gep(i,a,b) for(ll i=a;i<=b;i++)
#define mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&(-x)
ll c[N],a[N],n;
ll x,y;
struct Node{
ll id,val;
}nod[N];
void update(ll i,ll num)
{
while(i<=n){
c[i]+=num;
i+=lowbit(i);
}
}
ll getsum(ll n)
{
ll sum=;
while(n>){
sum+=c[n];
n-=lowbit(n);
}
return sum;
}
bool cmp(Node a,Node b)
{
return a.val<b.val;
}
int main()
{
while(~scanf("%lld%lld%lld",&n,&x,&y)){
mem(a,);
mem(c,);
gep(i,,n){
scanf("%lld",&nod[i].val);
nod[i].id=i;
}
sort(nod+,nod++n,cmp);//要先排序
a[nod[].id]=;//先让最小的为1
gep(i,,n)
{
if(nod[i].val!=nod[i-].val){//离散化
a[nod[i].id]=i;
}
else{
a[nod[i].id]=a[nod[i-].id];
}
}
ll ans=;
gep(i,,n){
update(a[i],);
ans+=getsum(n)-getsum(a[i]);//左边比我大的数的数目
}
printf("%lld\n",ans*min(x,y));
}
return ;
}
hdu 6318的更多相关文章
- 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 6318] Swaps and Inversions
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6318 [算法] 线段树 / 树状数组 [代码] #include<bits/stdc++.h ...
- HDU 6318.Swaps and Inversions-求逆序对-线段树 or 归并排序 or 离散化+树状数组 (2018 Multi-University Training Contest 2 1010)
6318.Swaps and Inversions 这个题就是找逆序对,然后逆序对数*min(x,y)就可以了. 官方题解:注意到逆序对=交换相邻需要交换的次数,那么输出 逆序对个数 即可. 求逆序对 ...
- HDU 6318 Swaps and Inversions(归并排序 || 树状数组)题解
题意:一个逆序对罚钱x元,现在给你交换的机会,每交换任意相邻两个数花钱y,问你最少付多少钱 思路:最近在补之前还没过的题,发现了这道多校的题.显然,交换相邻两个数逆序对必然会变化+1或者-1,那我们肯 ...
- HDU 6318 Swaps and Inversions 思路很巧妙!!!(转换为树状数组或者归并求解逆序数)
Swaps and Inversions Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- hdu 6318 Swaps and Inversions (线段树求逆序对数)
Swaps and Inversions Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- Swaps and Inversions HDU - 6318 树状数组+离散化
#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> us ...
- ( 2018 Multi-University Training Contest 2)
2018 Multi-University Training Contest 2) HDU 6311 Cover HDU 6312 Game HDU 6313 Hack It HDU 6314 Mat ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
随机推荐
- Ubuntu常用指令集
Ubuntu Linux 操作系统常用命令详细介绍 ( 1)Udo apt-get install 软件名 安装软件命令 sudo nautilus 打开文件(有 root 权限)su root 切换 ...
- zuul prefix
经过测试,书上应该是写错了,如果要全部的路由加前缀,需要将zuul.stripPrefix=true进行设置 而不是书上所说的false
- Kendo MVVM (一) 概述
Kendo MVVM (一) 概述 Model View ViewModel (MVVM) 是开发人员经常使用的一种设计模式,以实现数据模型(Model)和视图(View)的分离.MVVM 中的 V ...
- (转)VC得到可用的串口列表
//枚举串口 //参数:bEnablePort,哪个串口有效,bEnablePort[0]表示COM1,bEnablePort[n-1]表示COMn //返回值:有效的串口个数 int EnumAll ...
- LibreOJ #6208. 树上询问
内存限制:512 MiB 时间限制:500 ms 标准输入输出 题目类型:传统 评测方式:文本比较 上传者: 匿名 树链剖分+线段树 屠龙宝刀点击就送 #include <vector> ...
- python基础教程总结11——图形用户界面GUI
1. 丰富的平台 工具包 描述 Tkinter 使用Tk平台.很容易得到.半标准. wxpython 基于wxWindows.跨平台越来越流行. PythonWin 只能在Windows上使用.使用了 ...
- LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
在创建MFC项目时,如果没有设置好项目参数, 就会在编译时产生很多连接错误, 如我今天遇到的: LIBCD.lib(crt0.obj) : error LNK2001: unresolved exte ...
- cdoj 414 八数码 (双向bfs+康拓展开,A*)
一道关乎人生完整的问题. DBFS的优越:避免了结点膨胀太多. 假设一个状态结点可以扩展m个子结点,为了简单起见,假设每个结点的扩展都是相互独立的. 分析:起始状态结点数为1,每加深一层,结点数An ...
- Hybrid App开发之Html基本标签使用
前言: 前面简单学习了html简单标签的使用,今天学习一下其他的标签的使用. HTML的超链接 1.)创建一个超链接 <div> <p> <a href="ht ...
- oracle 数据导到 sql server
方法一: navicate:用法比较简单,选择工具-数据传输就可以了.目前测试了下暂时没遇到什么问题. 方法二: Microsoft SQL Server Migration Assistant 8. ...