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 ...
随机推荐
- 054 Spiral Matrix 旋转打印矩阵
给出一个 m x n 的矩阵(m 行, n 列),请按照顺时针螺旋顺序返回元素.例如,给出以下矩阵:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]应该返回 [1,2, ...
- openstack安装newton版本Glance部署(二)
一.部署Glance 1.Glance 安装 [root@linux-node1 ~]#yum install openstack-glance python-glance python-glance ...
- 字节码技术---------动态代理,lombok插件底层原理。类加载器
字节码技术应用场景 AOP技术.Lombok去除重复代码插件.动态修改class文件等 字节技术优势 Java字节码增强指的是在Java字节码生成之后,对其进行修改,增强其功能,这种方式相当于对应用 ...
- [WPF自定义控件库]了解如何自定义ItemsControl
1. 前言 对WPF来说ContentControl和ItemsControl是最重要的两个控件. 顾名思义,ItemsControl表示可用于呈现一组Item的控件.大部分时候我们并不需要自定义It ...
- 关于Identityserver4和IdentityServer3 授权不兼容的问题
使用IdentityServer3 作为授权服务器,如果没有设置证书,而且client又没有设置AccessTokenType = AccessTokenType.Reference,则获取token ...
- c# 基础字符串
ToLower():得到字符串的小写形式.注意字符串是不可变的,所以这些函数都不会直接改变字符串的内容,而是把修改后的字符串的值通过函数返回值的形式返回.s.ToLower()与s=s.ToLower ...
- Hibernate 事物隔离级别
Hibernate事务和并发控制 ++YONG原创,转载请注明 1. 事务介绍: 1.1. ...
- 1043 方格取数 2000年NOIP全国联赛提高组
1043 方格取数 2000年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 设有N* ...
- Bootstrap设置按钮禁用
在Bootstrap中,按钮可以使用button标签或者a标签.设置按钮禁用可以通过两种方式,一种是通用CSS样式,一种是用过JS脚本动态设置,下面举例说明! <!DOCTYPE html> ...
- Vuforia切换回识别场景后黑屏解决
使用Vuforia SDK开发时,如果从其他非识别场景切换回识别场景,可能会出现黑屏问题. 解决方法是在切换到其他场景时,先将当前场景的Tracker信息全部Stop.代码如下: IEnumerato ...