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. 056 Merge Intervals 合并区间

    给出一个区间的集合, 请合并所有重叠的区间.示例:给出 [1,3],[2,6],[8,10],[15,18],返回 [1,6],[8,10],[15,18].详见:https://leetcode.c ...

  2. 如何安装使用windows自带的telnet服务

    控制面板->程序和功能->打开或关闭Windows功能->Telnet 客户端 [ Telnet 服务器 ] 安装完成后重启cmd telnet ip port

  3. SqlDbx连接oracle

    解压SqlDbx.zip,将SqlDbx放到C:盘根目录 1.Path里面增加:C:\SqlDbx  Path是为了找tnsnames.ora 2.增加系统变量:ORACLE_HOME,路径:C:\S ...

  4. 洛谷P3928 SAC E#1 - 一道简单题 Sequence2

    提交地址 题目背景 小强和阿米巴是好朋友. 题目描述 小强喜欢数列.有一天,他心血来潮,写下了三个长度均为n的数列. 阿米巴也很喜欢数列.但是他只喜欢其中一种,波动数列. 阿米巴把他的喜好告诉了小强. ...

  5. 求逆欧拉函数(arc)

    已知欧拉函数计算公式 初始公式:φ(n)=n*(1-1/p1)*(1-1/p2).....*(1-1/pm)   又 n=p1^a1*p2^a2*...*ps^as  欧拉函数是积性函数 那么:φ(n ...

  6. 第十六章 提升用户体验 之 设计实现routes

    1. 概述 ASP.NET MVC route 用来把URL映射到方法中的action,是用户和程序之间的桥梁. 本章内容包括:定义route处理URL Pattern.应用route限制.忽略URL ...

  7. TCP的三次握手以及TCP状态转换图详解

    今天来讨论一下TCP的三次握手以及TCP的状态转换图.首先发一个三次握手的流程图如下: 圖 2.4-3.三向交握之封包连接模式A:封包发起当用戶端想要对服务器端发起连接时,就必須要送出一個要求连线的封 ...

  8. Kendo UI 特效概述

    Kendo UI 特效概述 Kendo UI Fx 提供了一个丰富,可扩展,性能经过优化的工具集合用来完成 HTML 元素的过渡显示.每种特效近可能的使用 CSS Transition ,对于一些老版 ...

  9. javascript结合nodejs实现多文件上传

    前端文件上传功能比较依赖后端,所以第一步用nodejs实现一个供文件上传的功能接口. 因为本人对nodejs也是一知半解,所以刚开始的想法是像原始的ajax交互那样,获取上传文件的内容,然后再通过no ...

  10. 实现memcopy函数

    实现memcopy函数: void * memcpy(void *dest, const void *src, unsigned int count); { if ((src == NULL) || ...