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 ...
随机推荐
- 在东京生活的中国IT程序员
应之前文章的博友邀请,我来开一篇在日本东京生活的中国IT程序员自谈,文中的讨论对象多为我自己或者是我的中国人(前)同事,有以偏概全之处还请包涵. 首先,我之前说日本的IT并不发达,不发达到什么程度呢? ...
- UIcollectionView 实现 轮番图
UICollectionView 用作轮番图的实现,demo 地址:https://github.com/SummerHH/YJCYCleCollectionVIew #import <UIKi ...
- spring data jpa自定义baseRepository
在一些特殊时候,我们会设计到对Spring Data JPA中的方法进行重新实现,这将会面临一个问题,如果我们新创建一个实现类.如果这个实现类实现了JpaRepository接口,这样我们不得不实现该 ...
- swift 基础-4
函数:完成特定任务的代码块,通过名字来表示函数做什么 func 函数名(形参:形参类型)->返回类型 command +option+0 隐藏右边的框 //定义函数 func sayHello( ...
- CF1157D N Problems During K Days
思路: 在1, 2, 3, ... , k的基础上贪心构造. 实现: #include <bits/stdc++.h> using namespace std; typedef long ...
- 洛谷P3959 宝藏(模拟退火乱搞)
题意 题目链接 题面好长啊...自己看吧.. Sol 自己想了一个退火的思路,没想到第一次交85,多退了几次就A了哈哈哈 首先把没用的边去掉,然后剩下的边从小到大排序 这样我们就得到了一个选边的序列, ...
- JavaScript中函数对象和对象的区别
function Test (word) { console.log (word); } Test('哈哈,我是函数'); new Test('哈哈,我是对象'); //将以上的调用方式换种通俗易懂的 ...
- python中的构造函数和构造函数和析构函数的作用
构造函数和构造函数和析构函数都属于python中的特殊方法 其中的“__del__”就是一个析构函数了,当使用del 删除对象时,会调用他本身的析构函数,另外当对象在某个作用域中调用完毕,在跳出其作用 ...
- linux+apache+mod_python+wechat_sdk搭建微信公共账号服务器
linux+apache+mod_python+wechat_sdk搭建微信公共账号服务器 转载请注明本文原作者:FignerLiu PRE 最近尝试了下使用python搭建微信公共账号服务器,实现了 ...
- sum特殊用法
在python中,list可以存储False和True a = [False] python的sum除了可以加数字,还可以计算列表中False,True的个数,默认是计算False个数 >> ...