HDU 4911 Inversion
http://acm.hdu.edu.cn/showproblem.php?pid=4911 归并排序求逆对数。
Inversion
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 556 Accepted Submission(s):
239
a1,a2,…,an. He is allowed to swap two
adjacent numbers for no more than k times.
Find the minimum number
of inversions after his swaps.
Note: The number of inversions is the
number of pair (i,j) where 1≤i<j≤n and ai>aj.
tests:
The first line contains 2 integers n,k
(1≤n≤105,0≤k≤109). The second line contains n integers
a1,a2,…,an
(0≤ai≤109).
A single integer denotes the
minimum number of inversions.
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
__int64 a[],cnt,c[];
__int64 k;
void merg(int low, int mid, int high)
{
int i=low, j=mid+;
cnt=;
while(i<=mid&&j<=high)
{
if(a[i]>a[j])
{
c[cnt++]=a[j++];
k+=mid-i+;
}
else
{
c[cnt++]=a[i++];
}
}
while(i<=mid)
{
c[cnt++]=a[i++];
}
while(j<=high)
{
c[cnt++]=a[j++];
}
cnt=;i=low;
while(i<=high)
{
a[i++]=c[cnt++];
}
}
void merger(int low, int high)
{
int mid;
if(low<high)
{
mid=(low+high)/;
merger(low,mid);
merger(mid+,high);
merg(low,mid,high);
}
}
int main()
{
int i,n,m;
while(~scanf("%d%d",&n,&m))
{
k=;
for(i=; i<n; i++)
{
scanf("%I64d",&a[i]);
}
merger(,n-);
if(k-m<=)
printf("0\n");
else
printf("%I64d\n",k-m);
}
return ;
}
HDU 4911 Inversion的更多相关文章
- hdu 4911 Inversion(找到的倒数)
主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911 Inversion Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 4911 Inversion (逆序数 归并排序)
Inversion 题目链接: http://acm.hust.edu.cn/vjudge/contest/121349#problem/A Description bobo has a sequen ...
- hdu 4911 Inversion(归并排序求逆序对数)2014多校训练第5场
Inversion Time Limit: 20 ...
- 2014多校第五场1001 || HDU 4911 Inversion (归并求逆序数)
题目链接 题意 : 给你一个数列,可以随意交换两相邻元素,交换次数不超过k次,让你找出i < j 且ai > aj的(i,j)的对数最小是多少对. 思路 : 一开始想的很多,各种都想了,后 ...
- hdu 4911 Inversion (分治 归并排序 求逆序数)
题目链接 题意:给n个数,求交换k次相邻的数之后的最小的逆序数对. 用分治的方法,以前在poj上做过这种题,昨天比赛的时候忘了.... 下面的归并排序还是以前的模板. #include <ios ...
- HDU 4911 Inversion 树状数组求逆序数对
显然每次交换都能降低1 所以求出逆序数对数,然后-=k就好了.. . _(:зゝ∠)_ #include<stdio.h> #include<string.h> #includ ...
- hdu 4911 Inversion and poj2299 [树状数组+离散化]
题目 题意: 给你一串数字,然后给你最多进行k次交换(只能交换相邻的)问交换后的最小逆序对个数是多少. 给你一个序列,每次只能交换相邻的位置,把他交换成一个递增序列所需要的最少步数 等于 整个序列的 ...
- HDU 6098 - Inversion | 2017 Multi-University Training Contest 6
/* HDU 6098 - Inversion [ 贪心,数论 ] | 2017 Multi-University Training Contest 6 题意: 求出所有B[i] = max(A[j] ...
- HDU 4911 (树状数组+逆序数)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4911 题目大意:最多可以交换K次,就最小逆序对数 解题思路: 逆序数定理,当逆序对数大于0时,若ak ...
随机推荐
- http://blog.csdn.net/superhosts/article/details/15813247
http://blog.csdn.net/superhosts/article/details/15813247
- ubuntu 13.10 64bit装BeyondCompare
1. Beyond Compare官网下载amd-64位的,安装失败,依赖于ia32-libs,但是这个文件已经不在源里了: 2. 官网下载tar.gz源码包,解压安装失败: 3. 直接装32位的,可 ...
- lintcode 中等题:Min stack 最小栈
题目 带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 解题 可以定义 ...
- lintcode :Remove Duplicates from Sorted Array II 删除排序数组中的重复数字 II
题目: 删除排序数组中的重复数字 II 跟进“删除重复数字”: 如果可以允许出现两次重复将如何处理? 样例 给出数组A =[1,1,1,2,2,3],你的函数应该返回长度5,此时A=[1,1,2,2, ...
- [hackerrank]Closest Number
https://www.hackerrank.com/contests/w5/challenges/closest-number 简单题. #include <iostream> #inc ...
- 【mysql的编程专题④】存储过程
类似函数,但是没有返回值,把sql进行封装,便于多次使用或多种应用程序共享使用.不能用在SQL语句中,只能使用CALL调用; 创建存储过程 语法 CREATE PROCEDURE sp_name ([ ...
- stringUtils是apache下的Java jar补充包
org.apache.commons.lang.StringUtils StringUtils中一共有130多个方法,并且都是static的, 所以我们可以这样调用StringUtils.xxx().
- [Unity菜鸟] Character控制移动
1. 给角色加角色控制器组件,然后用以下代码可以控制角色移动和跳跃 float speed = 6.0f; float jumpSpeed = 8.0f; float gravity = 20.0f; ...
- IDEA建项目的正确姿势
今天建多模块的分布式项目的时候折腾死了,可能是建项目的方法不对,最后经过摸索,觉得这样是比较合适的: 首先建一个空的项目:Empty Project,就是项目文件夹 然后在里面建model
- ossec 常用命令及目录说明
1. /var/www/html/analogi -> ossec 第三方的web界面的安装目录 [root@ossec-server ~]# cd /var/www/html/analogi/ ...