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. Android 使用系统的Activity播放音频文件 intent

    Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(Inten ...

  2. jackson基于注解的简单使用

    Jackson提供了一系列注解,方便对JSON序列化和反序列化进行控制,下面介绍一些常用的注解. 1.@JsonIgnore 此注解用于属性上,作用是进行JSON操作时忽略该属性. 2.@JsonFo ...

  3. 解决Eclipse10配置Pydev不成功的问题

    本人在线配置还在本地配置后 重启Eclipse,Windows-Preferences中并无Python选项,新建项目也无Python可选 这个尝试了好多种方法,重新安装Eclipse10,重新安装j ...

  4. map的详细用法

     map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时 ...

  5. 在PowerDesigner中设计物理模型3——视图、存储过程和函数

    原文:在PowerDesigner中设计物理模型3--视图.存储过程和函数 视图 在SQL Server中视图定义了一个SQL查询,一个查询中可以查询一个表也可以查询多个表,在PD中定义视图与在SQL ...

  6. 自己封装的poi操作Excel工具类

    自己封装的poi操作Excel工具类 在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完 ...

  7. svn 版本升级的问题

    原创文章,转载请注明 svn本地版本由1.6升级到1.7后,再使用时遇到一些问题,这里记录一下以备忘. 升级后,使用任何命令 不能用了,提示的意思大致是本地的workcopy版本太低了(之前用1.6版 ...

  8. IOS代码

    //// MJViewController.m// UITableView-编辑模式//// Created by mj on 13-4-11.// Copyright (c) 2013年 itcas ...

  9. jQuery EasyUI - Add link to datagrid cell

    Extracted from: http://stackoverflow.com/questions/16061894/jquery-easyui-add-link-to-cell HTML: < ...

  10. [HDOJ1698]Just a Hook(线段树,区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 陈题,更新后查询所有叶节点的和.撸一遍模版,形成自己的风格. #include <algo ...