原题:

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≤ai≤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

分析:
先用归并法求逆序数,再将逆序数减去k,结果为正   答案即这个
结果为负  答案就是0
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
__int64 a[100005], cnt, c[100005];
__int64 k;
void merg(int low, int mid, int high)
{int i = low, j = mid + 1;cnt = 0;while (i <= mid&&j <= high){if (a[i]>a[j]){c[cnt++] = a[j++];k += mid - i + 1;}else{c[cnt++] = a[i++];}}while (i <= mid){c[cnt++] = a[i++];}while (j <= high){c[cnt++] = a[j++];}cnt = 0; i = low;while (i <= high){a[i++] = c[cnt++];}
}
void merger(int low, int high)
{int mid;if (low<high){mid = (low + high) / 2;merger(low, mid);merger(mid + 1, high);merg(low, mid, high);}
}
int main()
{int i, n, m;while (~scanf("%d%d", &n, &m)){k = 0;for (i = 0; i<n; i++){scanf("%I64d", &a[i]);}merger(0, n - 1);if (k - m <= 0)printf("0\n");elseprintf("%I64d\n", k - m);}return 0;
}

转载于:https://www.cnblogs.com/shawn-ji/p/4711856.html

HDU 4911 Inversion - 疯狂的癫子 解题心得相关推荐

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

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

  2. hdu 5497 Inversion(树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5497 解题思路: 用树状数组维护一段区间L,区间长度为m,依次枚举该区间的终点ai,即将该点加入到区间 ...

  3. c语言五子棋游戏心得体会,下五子棋执白子之心得

    五子棋易学好懂,流传比较广泛,人们也将五子棋称为 连五子 . 连珠;下面是有下五子棋执白子之心得,欢迎参阅. 五子棋执白子之心得 1.防守执白棋最重要的就是防守,而防守的方法或技巧也就决定了是否能赢这 ...

  4. HDU 4911 http://acm.hdu.edu.cn/showproblem.php?pid=4911(线段树求逆序对)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911 解题报告: 给出一个长度为n的序列,然后给出一个k,要你求最多做k次相邻的数字交换后,逆序数最少 ...

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

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

  6. Hard Disk Drive HDU - 4788——24行代码AC,解题报告

    立志用更少的代码做更高效的表达 Yesterday your dear cousin Coach Pang gave you a new 100MB hard disk drive (HDD) as ...

  7. HDU 2144 (最长连续公共子列 + 并查集) Evolution

    我发现我一直理解错题意了,这里的子序列指的是连续子序列,怪不得我写的LCS一直WA 顺便复习一下并查集 1 //#define LOCAL 2 #include <iostream> 3 ...

  8. HDU 3071-Gcd Lcm game-线段树+素因子分解-[解题报告]HOJ

    Gcd & Lcm game 问题描述 : Tired of playing too much computer games, alpc23 is planning to play a gam ...

  9. wechall.net/stegano 解题心得

    /* 转载请注明出处:http://www.cnblogs.com/Martinium/p/wechall_stegano.html */ 最近迷上了 www.wechall.net 网站,里面都是些 ...

最新文章

  1. Android Studio 添加Assets目录
  2. Ug文档服务器,ug服务器
  3. Echarts开源可视化库学习(三)主题的使用
  4. Python丢弃返回值
  5. PhoneGap API帮助文档翻译—Camera (摄像头)
  6. ming window 交叉编译_如何在Linux for Windows上与MinGW交叉编译?
  7. 【Wonder整理】防止重复提交并弹出半透明对话框
  8. windows命令行压缩解压缩
  9. ThinkPHP5零食商城系统(前后台)
  10. 使用动软.net代码生成器生成数据库文档
  11. QWidget_层级关系
  12. 阿里天池课堂Python
  13. 小程序推送代码到远程库
  14. 社群怎么建立,怎么做好社群营销?
  15. 进程资源和进程状态 TASK_RUNNING TASK_INTERRUPTIBLE TASK_UNINTERRUPTIBLE
  16. DOSBox编译汇编程序(debug、MASM)
  17. 玩转Redis-干掉钉子户-没有设置过期时间的key
  18. 文章管理平台PC端(文章分类)
  19. php修改css文件后缀,css样式表文件的扩展名是什么
  20. 电大本科计算机上机考试题,电大计算机上机考试模拟题及答案

热门文章

  1. 在Java中构建响应式微服务系统——第三章 构建响应式微服务
  2. 合法的python赋值语句_关于Python赋值语句,以下选项中合法的是??()。
  3. 使用scp传输文件 Host key verification failed.解决
  4. Java工程师是做什么的 岗位职责都有哪些
  5. 春暖花开,微信公众号图文排版素材礼包已到!
  6. 项目敏捷管理模式有哪几种_敏捷团队中有效沟通的5种模式
  7. Mydrivers: 64国IT竞争力排名 中国仅第49
  8. imx6ul rtc 驱动修改
  9. halcon 将数据保存到excel_halcon保存数据到excel表格-怎样把图像里面的数据提取到excel表格里面去?...
  10. HDU 6143 Killer Names【容斥定理】【排列组合】