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

Problem Description
bobo has a sequence
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.

 
Input
The input consists of several tests. For each
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).

 
Output
For each tests:

A single integer denotes the
minimum number of inversions.

 
Sample Input
3 1
2 2 1
3 0
2 2 1
 
Sample Output
1
2
题意:相邻的最多调换k次,使得逆对数最小,
思路,先求出整个序列的逆对数-k次就可,如果出现负数就输出为0.用归并排序求逆对数。
#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的更多相关文章

  1. hdu 4911 Inversion(找到的倒数)

    主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911 Inversion Time Limit: 2000/1000 MS (Java/Others) ...

  2. HDU 4911 Inversion (逆序数 归并排序)

    Inversion 题目链接: http://acm.hust.edu.cn/vjudge/contest/121349#problem/A Description bobo has a sequen ...

  3. hdu 4911 Inversion(归并排序求逆序对数)2014多校训练第5场

    Inversion                                                                             Time Limit: 20 ...

  4. 2014多校第五场1001 || HDU 4911 Inversion (归并求逆序数)

    题目链接 题意 : 给你一个数列,可以随意交换两相邻元素,交换次数不超过k次,让你找出i < j 且ai > aj的(i,j)的对数最小是多少对. 思路 : 一开始想的很多,各种都想了,后 ...

  5. hdu 4911 Inversion (分治 归并排序 求逆序数)

    题目链接 题意:给n个数,求交换k次相邻的数之后的最小的逆序数对. 用分治的方法,以前在poj上做过这种题,昨天比赛的时候忘了.... 下面的归并排序还是以前的模板. #include <ios ...

  6. HDU 4911 Inversion 树状数组求逆序数对

    显然每次交换都能降低1 所以求出逆序数对数,然后-=k就好了.. . _(:зゝ∠)_ #include<stdio.h> #include<string.h> #includ ...

  7. hdu 4911 Inversion and poj2299 [树状数组+离散化]

    题目 题意:  给你一串数字,然后给你最多进行k次交换(只能交换相邻的)问交换后的最小逆序对个数是多少. 给你一个序列,每次只能交换相邻的位置,把他交换成一个递增序列所需要的最少步数 等于 整个序列的 ...

  8. HDU 6098 - Inversion | 2017 Multi-University Training Contest 6

    /* HDU 6098 - Inversion [ 贪心,数论 ] | 2017 Multi-University Training Contest 6 题意: 求出所有B[i] = max(A[j] ...

  9. HDU 4911 (树状数组+逆序数)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4911 题目大意:最多可以交换K次,就最小逆序对数 解题思路: 逆序数定理,当逆序对数大于0时,若ak ...

随机推荐

  1. spring @qualifier注解

    1.spring @qualifier注解用来在spring按类型装配可能存在多个bean的情况下,@qualifier注解可以用来缩小范围或者指定唯一. 也可以用来指定方法参数 2.@qualifi ...

  2. java:类集框架

    类集框架:jdk提供的一系列类和接口,位于java.util包当中,主要用于存储和管理对象,主要分为三大类:集合.列表和映射. 集合Set:用于存储一系列对象的集合.无序.不允许重复元素. 列表Lis ...

  3. JavaScript基础精华03(String对象,Array对象,循环遍历数组,JS中的Dictionary,Array的简化声明)

    String对象(*) length属性:获取字符串的字符个数.(无论中文字符还是英文字符都算1个字符.) charAt(index)方法:获取指定索引位置的字符.(索引从0开始) indexOf(‘ ...

  4. Hadoop HDFS文件常用操作及注意事项(更新)

    1.Copy a file from the local file system to HDFS The srcFile variable needs to contain the full name ...

  5. Mmap的实现原理和应用

    http://blog.csdn.net/edwardlulinux/article/details/8604400 很多文章分析了mmap的实现原理.从代码的逻辑来分析,总是觉没有把mmap后读写映 ...

  6. DoG 、Laplacian、图像金字塔详解

    DoG(Difference of Gaussian) DoG (Difference of Gaussian)是灰度图像增强和角点检测的方法,其做法较简单,证明较复杂,具体讲解如下: Differe ...

  7. struts2中利用POI导出Excel文档并下载

    1.项目组负责人让我实现这个接口,因为以前做过类似的,中间并没有遇到什么太困难的事情.其他不说,先上代码: package com.tydic.eshop.action.feedback; impor ...

  8. UIColor的用法

    UIColor,CGColor,CIColor的区别和联系 layer.shadowColor = [UIColor redColor].CGColor; 这个是今天用到的.顺便总结一下. 1.UIC ...

  9. 字符串模式匹配sunday算法

    文字部分转自:http://www.cnblogs.com/mr-ghostaqi/p/4285868.html 代码是我自己写的 今天在做LeetCode的时候,碰到一个写字符串匹配的题目: htt ...

  10. Qt之QuaZIP(zip压缩/解压缩)

    简述 QuaZIP是使用Qt/C++对ZLIB进行简单封装的用于压缩及解压缩ZIP的开源库.适用于多种平台,利用它可以很方便的将单个或多个文件打包为zip文件,且打包后的zip文件可以通过其它工具打开 ...