hdu 4911 求逆序对数+树状数组
http://acm.hdu.edu.cn/showproblem.php?pid=4911
给定一个序列,有k次机会交换相邻两个位置的数,问说最后序列的逆序对数最少为多少。
实际上每交换一次能且只能减少一个逆序对,所以问题转换成如何求逆序对数。
归并排序或者树状数组都可搞
树状数组:
先按大小排序后分别标号,然后就变成了求1~n的序列的逆序数,每个分别查询出比他小的用i减,在把他的值插入即可
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define clr0(x) memset(x,0,sizeof(x))
typedef long long LL;
typedef pair<int,int> p; const int maxn=100005;
LL f[maxn];
int n;
void add(int x,LL y)
{
for(;x<=n;x += x&(-x)) f[x]+=y;
}
LL sum(int x){
LL s=0;
for (;x;x -= x&(-x)) s+=f[x];
return s;
} p a[maxn];
int k; bool cmp(p i,p j){
return i.second < j.second;
} int main(){
int i;
LL s;
while (~RD2(n,k)){
for (i=0;i<n;++i){
RD(a[i].first);
a[i].second=i;
}
sort(a,a+n);
for (i=0;i<n;++i)
a[i].first = i+1;
sort(a,a+n,cmp);
clr0(f);
for (s=i=0;i<n;++i){
s += i - sum(a[i].first);
add(a[i].first,1);
}
printf("%I64d\n",max(0LL,s-k));
}
return 0;
}
归并排序:
每次归并发现需要前后交换时都给总的ret加上mid - mvl + 1,因为mvl到mid直接的数都比mvr下标上的数大
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define clr0(x) memset(x,0,sizeof(x))
typedef long long LL;
const int maxn = 1e5+5;
LL k;
int n, a[maxn], b[maxn]; LL merge_sort(int l, int r)
{
if (l == r)
return 0; int mid = (l + r) / 2;
LL ret = merge_sort(l, mid) + merge_sort(mid+1, r);
int mvl = l, mvr = mid+1, mv = l;
while (mvl <= mid || mvr <= r) {
if (mvr > r || (mvl <= mid && a[mvl] <= a[mvr])) {
b[mv++] = a[mvl++];
} else {
ret += mid - mvl + 1;
b[mv++] = a[mvr++];
}
} for (int i = l; i <= r; i++)
a[i] = b[i];
return ret;
} int main () {
while (scanf("%d%I64d", &n, &k) == 2) {
for (int i = 1; i <= n; i++)
RD(a[i]);
printf("%I64d\n", max(merge_sort(1, n) - k, 0LL));
}
return 0;
}
hdu 4911 求逆序对数+树状数组的更多相关文章
- Day2:T4求逆序对(树状数组+归并排序)
T4: 求逆序对 A[I]为前缀和 推导 (A[J]-A[I])/(J-I)>=M A[j]-A[I]>=M(J-I) A[J]-M*J>=A[I]-M*I 设B[]=A[]-M*( ...
- AcWing 107. 超快速排序(归并排序 + 逆序对 or 树状数组)
在这个问题中,您必须分析特定的排序算法----超快速排序. 该算法通过交换两个相邻的序列元素来处理n个不同整数的序列,直到序列按升序排序. 对于输入序列9 1 0 5 4,超快速排序生成输出0 1 4 ...
- P1908 逆序对-(树状数组)
https://www.luogu.org/problem/P1908 比较喜欢线段树,懒得用树状数组(只会套模板,位运算的精髓没有领悟到),一直没有记录树状数组代码,又得捡回来,趁这道题记录一下模板 ...
- 【BZOJ 3295】动态逆序对 - 分块+树状数组
题目描述 给定一个1~n的序列,然后m次删除元素,每次删除之前询问逆序对的个数. 分析:分块+树状数组 (PS:本题的CDQ分治解法见下一篇) 首先将序列分成T块,每一块开一个树状数组,并且先把最初的 ...
- Bzoj 3295: [Cqoi2011]动态逆序对 分块,树状数组,逆序对
3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2886 Solved: 924[Submit][Stat ...
- bzoj1831 逆序对 (dp+树状数组)
注意到,所有的-1应该是一个不降的序列,否则不会更优那就先求出来不是-1的的逆序对个数,然后设f[i][j]表示第i个-1放成j的前i个-1带来的最小逆序对数量这个可以树状数组来求 #include& ...
- BZOJ3295 [Cqoi2011]动态逆序对 分治 树状数组
原文链接http://www.cnblogs.com/zhouzhendong/p/8678185.html 题目传送门 - BZOJ3295 题意 对于序列$A$,它的逆序对数定义为满足$i< ...
- P3157 [CQOI2011]动态逆序对(树状数组套线段树)
P3157 [CQOI2011]动态逆序对 树状数组套线段树 静态逆序对咋做?树状数组(别管归并QWQ) 然鹅动态的咋做? 我们考虑每次删除一个元素. 减去的就是与这个元素有关的逆序对数,介个可以预处 ...
- BZOJ3295: [Cqoi2011]动态逆序对(树状数组套主席树)
3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 7465 Solved: 2662[Submit][Sta ...
随机推荐
- myeclipse的安装与配置和JUnit的简单使用
安装配置 首先根据自己电脑系统选择合适的JDK版本 http://www.oracle.com/technetwork/java/javase/downloads/index.html 这是JDK下载 ...
- Andriod——手机尺寸相关的概念 +尺寸单位+关于颜色
手机的尺寸: 屏幕对角线的长度,单位为英寸(2.54cm) 手机的分辨率: 屏幕能显示的像素的数量, 一般用在长方向上数量*宽方向上数量来表达 手机的像素密度: pixels per inch,也称P ...
- RoCE vs iWARP
两种以太网 RDMA 协议: iWARP 和 RoCE 转载 2017年03月08日 16:10:09 1510 http://weibo.com/p/1001603936363903889917?m ...
- C&Cpp.CallGraph
1. CodeViz http://www.skynet.ie/~mel/projects/codeviz/ 2. http://my.oschina.net/zmlblog/blog/186308
- SQL注入漏洞的原理
在平常生活中,我们登陆某网页,常常需要输入用户名和密码,点击登陆,即可登陆成功. 对于黑客来说,不需要用户名和密码,只输入 admin '— 也可以登陆成功. 黑客利用的这种漏洞就是SQL Injec ...
- ES6 中 let and const
let 和 const 命令 let 命令 基本用法 ES6 新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. { let a = 10; v ...
- [翻译]Javaslang 介绍
原文地址:Introduction to Javaslang 1. 概述 在这篇文章中,我们将会探讨: Javaslang 是什么? 为什么需要它? 以及怎样在项目中使用它? Javaslang 是J ...
- 枚举之后define
经常会看到类似下边的code写法,觉得这么写没什么意义. enum { AA, BB, CC, }; #define AA AA #define BB BB #define CC CC 尝试下边代码, ...
- struts2升级
http://www.blogjava.net/ldwblog/archive/2013/10/14/404944.html
- is not allowed to connect to this MySQL server解决办法
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION; myuser:代表你 ...