Inversion

题目链接:

http://acm.hust.edu.cn/vjudge/contest/121349#problem/A

Description

bobo has a sequence a 1,a 2,…,a n. 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 a i>a j.

Input

The input consists of several tests. For each tests:

The first line contains 2 integers n,k (1≤n≤10 5,0≤k≤10 9). The second line contains n integers a 1,a 2,…,a n (0≤a i≤10 9).

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

##题意:

对有n个元素的数组进行不超过k次操作,每次操作允许交换相邻元素;
求操作后能得到的整个数组的最小逆序数.


##题解:

容易得出每次交换相邻元素最多只能使得逆序数降低1.
而对于一个逆序数不为0的数组,一定存在相邻的两个数满足(i

##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 101000
#define mod 100000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

LL num[maxn];

LL _count=0;

void merge_array(int left,int mid,int right)

{

int temp[right-left+1];int k=0;

int i=left;int j=mid+1;

while(i<=mid&&j<=right)

{

if(num[i]<=num[j])

temp[k++]=num[i++];

else

{temp[k++]=num[j++];_count+=mid-i+1;}

}

while(i<=mid)

temp[k++]=num[i++];

while(j<=right)

temp[k++]=num[j++];

for(i=left;i<=right;i++)
num[i]=temp[i-left];

}

void merge_sort(int left,int right)

{

if(left<right)

{

int mid=left+(right-left)/2;

merge_sort(left,mid);

merge_sort(mid+1,right);

merge_array(left,mid,right);

}

}

int main(void)

{

//IN;

int n;
LL k;
while(scanf("%d %I64d",&n,&k) != EOF)
{
for(int i=0; i<n; i++)
scanf("%I64d",&num[i]);
_count=0;
merge_sort(0, n-1); printf("%I64d\n", max(_count-k,0LL));
}
return 0;

}

HDU 4911 Inversion (逆序数 归并排序)的更多相关文章

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

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

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

    Inversion                                                                             Time Limit: 20 ...

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

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

  4. hdu 1394 Minimum Inversion Number (裸树状数组 求逆序数 && 归并排序求逆序数)

    题目链接 题意: 给一个n个数的序列a1, a2, ..., an ,这些数的范围是0-n-1, 可以把前面m个数移动到后面去,形成新序列:a1, a2, ..., an-1, an (where m ...

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

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

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

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

  7. hdu 4911 求逆序对数+树状数组

    http://acm.hdu.edu.cn/showproblem.php?pid=4911 给定一个序列,有k次机会交换相邻两个位置的数,问说最后序列的逆序对数最少为多少. 实际上每交换一次能且只能 ...

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

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

  9. HDU 4911 Inversion

    http://acm.hdu.edu.cn/showproblem.php?pid=4911   归并排序求逆对数. Inversion Time Limit: 2000/1000 MS (Java/ ...

随机推荐

  1. MyBatis学习总结4--解决字段名与实体类属性名不相同的冲突

    在平时的开发中,我们表中的字段名和表对应实体类的属性名称不一定是完全相同的,如果直接在xml映射文件中使用sql进行映射,会造成返回值为空的情况,下面阐述解决方案: 测试所用表和数据 create t ...

  2. bzoj1499: [NOI2005]瑰丽华尔兹

    dp. 首先我们可以看到每个时间段只能往一个方向转移最多t步(t为时间段的长度),所以我们可以按时间段dp.因为这个前后值互不影响,也不用占用这一维空间就可以省去. 然后每个时间段内是一列一列(行) ...

  3. hdu 4671 Backup Plan(签到题)

    错成那样,还以为是卡时间卡精度的变态题,结果就那么ac了= = 悔死我了 题意就不概述了,只要处理前两列即可.其中第一列顺序直接扫一遍,第二列要先处理较少的那几种.我是接着第一列用 head[] 继续 ...

  4. Java [leetcode 7] Reverse Integer

    问题描述: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Ha ...

  5. LeetCode中有技巧的题需要面试前记得的

    https://leetcode.com/problems/insert-interval/ http://www.cnblogs.com/yxzfscg/p/4459173.html https:/ ...

  6. DirectShow建立一个视频捕捉程序

    DirectShow 提供了用应用程序从适当的硬件中捕捉和预览音/视频的能力.数据源包括:VCR,camera,TV tuner,microphone,或其他的数据源.一个应用程序可以立刻显示捕捉的数 ...

  7. .Net中的各种序列化

    我们知道将对象的状态保持在存储媒体中,以便可以在以后重新创建精确的副本这正是数据持久化所要做的.而且,不同应用程序之间的通讯需要相互传输数据.那么序列化和反序列化正是为此而生. 序列化和反序列化 所谓 ...

  8. mysql explain中key_len的计算

    ken_len表示索引使用的字节数,根据这个值,就可以判断索引使用情况,特别是在组合索引的时候,判断是否所有的索引字段都被查询用到. key_len显示了条件检索子句需要的索引长度,但 ORDER B ...

  9. delphi7在win7系统如何安装spcomm控件

    1.先准备好串口控件SPCOMM,例如把它放在F盘的工具安装文件夹下,等一下加载时需要用到. 2.打开delphi7软件. 3.按下上面的Component>Install  Component ...

  10. OnItemClickListener 的参数详解(转)

    转载地址:http://blog.iamzsx.me/show.html?id=147001 我们在使用ListView的时候,一般都会为ListView添加一个响应事件android.widget. ...