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
 
Source
 
 
 #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的更多相关文章

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

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

  2. [HDU 6318] Swaps and Inversions

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6318 [算法] 线段树 / 树状数组 [代码] #include<bits/stdc++.h ...

  3. HDU 6318.Swaps and Inversions-求逆序对-线段树 or 归并排序 or 离散化+树状数组 (2018 Multi-University Training Contest 2 1010)

    6318.Swaps and Inversions 这个题就是找逆序对,然后逆序对数*min(x,y)就可以了. 官方题解:注意到逆序对=交换相邻需要交换的次数,那么输出 逆序对个数 即可. 求逆序对 ...

  4. HDU 6318 Swaps and Inversions(归并排序 || 树状数组)题解

    题意:一个逆序对罚钱x元,现在给你交换的机会,每交换任意相邻两个数花钱y,问你最少付多少钱 思路:最近在补之前还没过的题,发现了这道多校的题.显然,交换相邻两个数逆序对必然会变化+1或者-1,那我们肯 ...

  5. HDU 6318 Swaps and Inversions 思路很巧妙!!!(转换为树状数组或者归并求解逆序数)

    Swaps and Inversions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  6. hdu 6318 Swaps and Inversions (线段树求逆序对数)

    Swaps and Inversions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  7. Swaps and Inversions HDU - 6318 树状数组+离散化

    #include<iostream> #include<algorithm> #include<cstring> #include<cstdio> us ...

  8. ( 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 ...

  9. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

随机推荐

  1. docker部署mysql远程连接 解决1251 client does not support ..

    现象:用虚拟机上Docker启动mysql之后无法在本地安装的navicat上远程连接已启动的mysql,错误截图: 原因:mysql 8.0 默认使用 caching_sha2_password 身 ...

  2. 帝国empirecms去除后台登陆认证码

    打开文件:\e\config\config.php 找到代码 $ecms_config['esafe']['loginauth']='abc'; 把值设为空即可,即改为 $ecms_config['e ...

  3. C++ error:Debug Assertion Failed.Expression:_BLOCK_TYPE_IS_VALID(phead->nBlock)

    Debug Assertion Failed.Expression:_BLOCK_TYPE_IS_VALID(phead->nBlockUse) 关于上面这个错误,我在上一篇文章中的程序遇到过了 ...

  4. ios 利用runtime任性跳转

    在开发项目中,会有这样变态的需求: 推送:根据服务端推送过来的数据规则,跳转到对应的控制器 feeds列表:不同类似的cell,可能跳转不同的控制器(嘘!产品经理是这样要求:我也不确定会跳转哪个界面哦 ...

  5. git如何强制用远程分支更新本地

    git本地即使有修改如何强制更新: 本地有修改和提交,如何强制用远程的库更新本地.我尝试过用git pull -f,总是提示 You have not concluded your merge. (M ...

  6. 新建博客第一天,随意来一发Win8运行命令大全

    1.calc:启动计算器 2.appwiz.cpl:程序和功能   3.certmgr.msc:证书管理实用程序 4.charmap:启动字符映射表 5.chkdsk.exe:Chkdsk磁盘检查(管 ...

  7. js使用my97插件显示当前时间,且select控制计算时间差

    做页面需要两个时间输入框一个显示当前时间,一个显示之前的时间,并且需要一个select下拉框控制两个时间输入框之间的差,效果如下图: 这里使用的是My97DatePicer,简单方便,引入my97插件 ...

  8. 让您的 VS 2012/2013 升级开发 .NET 4.6 -- Targeting the .NET Framework 4.6 (多目标包)

    原文出处:让您的 VS 2012/2013 升级开发 .NET 4.6 -- Targeting the .NET Framework 4.6 (多目标包) http://www.dotblogs.c ...

  9. 从照片网站pexels批量爬取照片

    调试中,未成功. from bs4 import BeautifulSoup import requests headers={ #'User-Agent':'Nokia6600/1.0 (3.42. ...

  10. python基础教程总结15——5 虚拟茶话会

    聊天服务器: 服务器能接受来自不同用户的多个连接: 允许用户同时(并行)操作: 能解释命令,例如,say或者logout: 容易拓展 套接字和端口: 套接字是一种使用标准UNIX文件描述符(file ...