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. hdu 2177 取(2堆)石子游戏 博弈论

    由于要输出方案,变得复杂了.数据不是很大,首先打表,所有whthoff 的奇异局势. 然后直接判断是否为必胜局面. 如果必胜,首先判断能否直接同时相减得到.这里不需要遍历或者二分查找.由于两者同时减去 ...

  2. [SharePoint 2013 入门教程 2 ] 创建WEB应用程序,网站集,网站

    SharePoint 2013 的 Hello World 由大到小  创建WEB应用程序(老母),网站集(儿子),网站(孙子) 直接确定,其余都默认 填入标题,选好模板.网站集 儿子就有了. 点击页 ...

  3. LR_问题_运行场景时提示scripts you are running in invalid

    问题描述 脚本在virtual user generator中运行正常. 在Controller中运行场景时报错: the target you defined cannot be reached. ...

  4. SDK 与MFC

    SDK 就是Software Development Kit 软件开发包MFC 就是Microsoft Foundation Classes 微软函数类库.是以C++类的形式封装了Windows的AP ...

  5. parent children

    class parent{ protected static int count=0; public parent() { count++; } } public class child extend ...

  6. SQLite数据库的体系结构(翻译自sqlite.org)

    $1 简介    本文档描述了SQLite库的体系结构,这些信息对那些想理解和修改SQLite的内部工作机制的人是有用的.    下图显示了SQLite的主要组成部件及其相互关系,下面的内容将描述每一 ...

  7. C#操作.csv文件Demo

    1.使用OleDB操作.csv文件,比较费时 public static DataTable GetDataTableFromCsv(string path,bool isFirstRowHeader ...

  8. Csharp日常笔记

    1. 1.退出程序                   this.Close();             //方法退关闭当前窗口. Application.Exit();       //方法退出整 ...

  9. Right Column - 右侧栏目

    function share(sType){ var sName = "Yorhom\'s Game Box"; var title='Yorhom\'s Game Box', _ ...

  10. jQuery选择器最佳实践--来自jQ官网

    1.基于ID选择器进行查询,并且使用find方法. //快速 $("#container div.footer"); //超快 $("#container"). ...