bobo has a sequence a 1,a 2,…,a n. He is allowed to swap twoadjacent 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.

InputThe 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).OutputFor each tests:

A single integer denotes the minimum number of inversions.

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<memory>
#include<bitset>
#include<string>
#include<functional>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL; const int MAXN = 1e5 + ;
#define INF 0x3f3f3f3f
//
LL n, k, cnt = ;
LL a[MAXN], L[MAXN/], R[MAXN];
void merge(LL l, LL r)
{
LL mid = (l + r) / ;
LL t1 = , t2 = ;
for (LL i = l; i <= mid; i++)
L[t1++] = a[i];
for (LL i = mid + ; i <= r; i++)
R[t2++] = a[i];
LL i = , j = , pos = l;
while (i < t1&&j < t2)
{
if (L[i] > R[j])
{
cnt += (t1 - i);
a[pos++] = R[j++];
}
else
a[pos++] = L[i++];
}
while (i < t1)
a[pos++] = L[i++];
while (j < t2)
a[pos++] = R[j++];
}
void merge_sort(LL l, LL r)
{
if (l < r)
{
LL mid = (l + r) / ;
merge_sort(l, mid);
merge_sort(mid + , r);
merge(l, r);
}
}
int main()
{
while (scanf("%lld%lld", &n, &k) != EOF)
{
cnt = ;
for (LL i = ; i < n; i++)
{
scanf("%lld", &a[i]);
}
merge_sort(, n - );
if (cnt >= k)
printf("%lld\n", cnt - k);
else
printf("0\n");
}
}

最新文章

  1. python基础篇----字符串unicode
  2. (企业面试部分)超详细思路讲解SQL语句的查询实现,及数据的创建。
  3. 六个前端开发工程师必备的Web设计模式/模块资源(转)
  4. Nmap 源代码学习四 软件简单使用
  5. 外语学习强烈推荐Rosetta Stone
  6. linshi_temp_erweima_html
  7. C++primer第三章标准库类型
  8. SVN分支/合并原理及最佳实践
  9. Win7 32位系统下Sublime text 3的安装以及配置C/C++、java、python的开发环境方法
  10. 当 ReactJS 遇到 TypeScript
  11. 安卓和IOS兼容问题
  12. centos下 telnet访问百度
  13. postgreSQL使用杂谈
  14. vue 项目使用 webpack 构建自动获取电脑ip地址
  15. java ArrayList去重
  16. Nginx、Tomcat配置https
  17. MySql(十一):MySQL性能调优——常用存储引擎优化
  18. 关于Ubuntu的默认python版本
  19. jquery validation表单验证插件。
  20. Installing Oracle Database 12c Release 2(12.2) RAC on RHEL7.3 in Silent Mode

热门文章

  1. iOS捷径(Workflow 2.0)拓展
  2. css3 transform + deviceorientation实现图片旋转效果
  3. 简洁大方的wordpress主题,不容错过的主题,附带主题源码下载
  4. 21全志r58m平台的framework在使用过程中会莫名的崩溃掉
  5. Android(java)学习笔记189:ContentProvider使用(银行数据库创建和增删改查的案例)
  6. CAD参数绘制角度标注(网页版)
  7. ActiveX控件获取不到对象属性或者方法的原因分析
  8. jQuery中Ajax事件beforesend及各参数含义1
  9. socket编程(Java实现)
  10. Web项目ConcurrentModificationException异常