【HDOJ6318】Swaps and Inversions(树状数组)
题意:
给定一串数组,其中含有一个逆序对则需要花费x,交换相邻两个数需要花费y,输出最小花费。
n<=1e5,-1e9<=a[i]<=1e9
思路:

#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define fi first
#define se second
#define MP make_pair
const int N=; int n,m,x,y,a[N],b[N],c[N];
ll ans; int read()
{
int v=,f=;
char c=getchar();
while(c<||<c) {if(c=='-') f=-; c=getchar();}
while(<=c&&c<=) v=(v<<)+v+v+c-,c=getchar();
return v*f;
} int query(int x)
{
int ans=;
for(;x;x-=x&-x) ans+=c[x];
return ans;
} void update(int x)
{
for(;x<=m;x+=x&-x) c[x]++;
} int main()
{ while(scanf("%d%d%d",&n,&x,&y)!=EOF)
{
for(int i=;i<=n;i++) scanf("%d",&a[i]);
for(int i=;i<=n;i++) b[i]=a[i];
sort(b+,b+n+);
m=unique(b+,b+n+)-b-;
for(int i=;i<=n;i++) a[i]=lower_bound(b+,b+m+,a[i])-b;
ans=;
memset(c,,sizeof(c));
for(int i=n;i>=;i--)
{
ans+=query(a[i]-);
update(a[i]);
}
ans=ans*min(x,y);
printf("%lld\n",ans);
}
return ;
}
【HDOJ6318】Swaps and Inversions(树状数组)的更多相关文章
- HDU5196--DZY Loves Inversions 树状数组 逆序数
题意查询给定[L, R]区间内 逆序对数 ==k的子区间的个数. 我们只需要求出 子区间小于等于k的个数和小于等于k-1的个数,然后相减就得出答案了. 对于i(1≤i≤n),我们计算ri表示[i,ri ...
- Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数
E. Infinite Inversions ...
- Infinite Inversions(树状数组+离散化)
思路及代码参考:https://blog.csdn.net/u014800748/article/details/45420085 There is an infinite sequence cons ...
- SGU180:Inversions(树状数组)
There are N integers (1<=N<=65537) A1, A2,.. AN (0<=Ai<=10^9). You need to find amount o ...
- 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(归并排序 || 树状数组)题解
题意:一个逆序对罚钱x元,现在给你交换的机会,每交换任意相邻两个数花钱y,问你最少付多少钱 思路:最近在补之前还没过的题,发现了这道多校的题.显然,交换相邻两个数逆序对必然会变化+1或者-1,那我们肯 ...
- HDU 6318.Swaps and Inversions-求逆序对-线段树 or 归并排序 or 离散化+树状数组 (2018 Multi-University Training Contest 2 1010)
6318.Swaps and Inversions 这个题就是找逆序对,然后逆序对数*min(x,y)就可以了. 官方题解:注意到逆序对=交换相邻需要交换的次数,那么输出 逆序对个数 即可. 求逆序对 ...
- Dynamic Inversions 50个树状数组
Dynamic Inversions Time Limit: 30000/15000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others ...
- CF #301 E:Infinite Inversions(逆序数,树状数组)
A-Combination Lock B-School Marks C-Ice Cave D-Bad Luck Island E-Infinite Inversions E:Infini ...
- CodeForces 540E - Infinite Inversions(离散化+树状数组)
花了近5个小时,改的乱七八糟,终于A了. 一个无限数列,1,2,3,4,...,n....,给n个数对<i,j>把数列的i,j两个元素做交换.求交换后数列的逆序对数. 很容易想到离散化+树 ...
随机推荐
- Android RxJava小结
一.如何使用 在build.gradle中添加依赖 dependencies { api 'io.reactivex:rxandroid:1.2.1' api 'io.reactivex:rxjava ...
- Verilog设计中的锁存器
问题: 什么是锁存器? 什么时候出现锁存器? 锁存器对电路有什么影响? 如何在FPGA设计中避免锁存器? 在FPGA设计中应该避免锁存器.实际上,锁存器与D触发器实现的逻辑功能基本相同,都有暂存数据的 ...
- ASP.NET MVC IIS7 403.14-Forbidden
问题描述 IIS 7上发布ASP.NET MVC程序报错:403.14-Forbidden Web 服务器被配置为不列出此目录的内容 折腾了半天,提示里面的解决方法是: 如果不希望启用目录浏览,请确保 ...
- element ui select组件和table做分页完整功能和二级联动效果
<template> <div class="index_box"> <div class="search_box"> &l ...
- JS 中的事件绑定、事件监听、事件委托是什么?
在JavaScript的学习中,我们经常会遇到JavaScript的事件机制,例如,事件绑定.事件监听.事件委托(事件代理)等.这些名词是什么意思呢,有什么作用呢? 事件绑定 要想让 JavaScri ...
- activitymq 集群构建
测试zk是否正常 [root@node2 bin]# cd /zk/1/zookeeper-3.4.10/bin/ [root@node2 bin]# ./zkCli.sh -server 10.50 ...
- No-10.高级变量类型
高级变量类型 目标 列表 元组 字典 字符串 公共方法 变量高级 知识点回顾 Python 中数据类型可以分为 数字型 和 非数字型 数字型 整型 (int) 浮点型(float) 布尔型(bool) ...
- ASP.NetCore 错误 NU1605 检测到包降级: Microsoft.Data.Sqlite 从 2.2.1 降级到 2.1.0
找到使用的.csproj文件 将 <PackageReference Include="Microsoft.Data.Sqlite" Version="2.1.0& ...
- 122. Best Time to Buy and Sell Stock II@python
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Shell转大写为小写
#!/bin/bash # read a a=$(echo "$a" | awk '{print tolower($0)}') #转大写为小写 if [[ $a = 'y' || ...