题意:依据题目要求交换相邻的两个元素k次,使得最后剩下的逆序对数最少

思路:假设逆序数大于0,存在0 <= i < n使得交换Ai,Ai+1后逆序数降低1,所求答案就为max(inversion - k, 0);

利用归并排序计算逆序对数。

#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; const int MAXN = 1000005; int arr[MAXN], b[MAXN];
int n, k;
long long cnt; void merge_sort(int * a, int x, int y, int *b) {
if (y - x > 1) {
int m = x + (y - x) / 2;
int p = x, q = m, i = x;
merge_sort(a, x, m, b);
merge_sort(a, m, y, b);
while (p < m || q < y) {
if (q >= y || (p < m && a[p] <= a[q]))
b[i++] = a[p++];
else {
b[i++] = a[q++];
cnt += m - p;
}
}
for (i = x; i < y; i++)
a[i] = b[i];
}
} int main() {
while (scanf("%d%d", &n, &k) != EOF) {
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
cnt = 0;
merge_sort(arr, 0, n, b);
if (cnt - k > 0)
cnt -= k;
else
cnt = 0;
cout << cnt << endl;
}
return 0;
}

HDU4911-Inversion的更多相关文章

  1. HDU4911:Inversion

    Problem Description bobo has a sequence a1,a2,-,an. He is allowed to swap two adjacent numbers for n ...

  2. HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number                         ...

  3. 控制反转Inversion of Control (IoC) 与 依赖注入Dependency Injection (DI)

    控制反转和依赖注入 控制反转和依赖注入是两个密不可分的方法用来分离你应用程序中的依赖性.控制反转Inversion of Control (IoC) 意味着一个对象不会新创建一个对象并依赖着它来完成工 ...

  4. HDU 1394 Minimum Inversion Number(最小逆序数 线段树)

    Minimum Inversion Number [题目链接]Minimum Inversion Number [题目类型]最小逆序数 线段树 &题意: 求一个数列经过n次变换得到的数列其中的 ...

  5. 依赖倒置原则(Dependency Inversion Principle)

    很多软件工程师都多少在处理 "Bad Design"时有一些痛苦的经历.如果发现这些 "Bad Design" 的始作俑者就是我们自己时,那感觉就更糟糕了.那么 ...

  6. HDU 1394 Minimum Inversion Number(最小逆序数/暴力 线段树 树状数组 归并排序)

    题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS     Memory Limit: 32768 K Description The inve ...

  7. Inversion Sequence(csu 1555)

    Description For sequence i1, i2, i3, … , iN, we set aj to be the number of members in the sequence w ...

  8. ACM: 强化训练-Inversion Sequence-线段树 or STL·vector

    Inversion Sequence Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%lld & %llu D ...

  9. ACM Minimum Inversion Number 解题报告 -线段树

    C - Minimum Inversion Number Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &a ...

  10. HDU-Minimum Inversion Number(最小逆序数)

    Problem Description The inversion number of a given number sequence a1, a2, ..., an is the number of ...

随机推荐

  1. 搭建 GIT 服务器教程

  2. 南阳oj 士兵杀敌(二) 题目116 NYOJ 数据结构

     /*士兵杀敌(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:5 描写叙述 南将军手下有N个士兵.分别编号1到N.这些士兵的杀敌数都是已知的. 小工是南将军手下的军师, ...

  3. 分享一段php获取随意时间的前一天代码

    <?php /** * 获取给定日期的前一天 * @param string $date * @return string $yesterday */ function getYesterday ...

  4. 65.十一级指针实现百万qq号的增删查改以及排序写入

    运行结果: 内存使用情况: 写入文件排序好的数据: 创建文件地址以及创建十一级指针 char *path = "QQ.txt"; char *sortpath = "QQ ...

  5. springMVC注解用法:@modelattribute的用法

    在Spring MVC里,@ModelAttribute通常使用在Controller方法的参数注解中,用于解释model entity,但同时,也可以放在方法注解里. 如果把@ModelAttrib ...

  6. 有关 Lambda && linq练习 有待整理

    1. 查询Student表中的所有记录的Sname.Ssex和Class列.(select sname,ssex,class from student) Students.Select(s=> ...

  7. vb.net structure 定义静态数组

    Const RAS95_MaxEntryName = 256 Const RAS95_MaxDeviceName = 128 Const RAS_MaxDeviceType = 16 Structur ...

  8. 调色板原理 & 编程

    调色板原理 & 编程 逻辑调色板结构LOGPALETTE,该结构定义如下: typedef struct tagLOGPALETTE { WORD palVersion; //调色板的板本号, ...

  9. 51Nod——N1082 与7无关的数

    https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1082 题目来源: 有道难题 基准时间限制:1 秒 空间限制:13107 ...

  10. c++ builder firemonkey 实现填充椭圆

    相信同类Delphi 类似文章非常多了,这里我用c++ builder firemonkey 实现填充椭圆 本例主要在FormPaint实现,当然你想在Image1->Bitmap->Ca ...