Inversion 归并求逆元
2024-08-30 13:14:27
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");
}
}
最新文章
- python基础篇----字符串unicode
- (企业面试部分)超详细思路讲解SQL语句的查询实现,及数据的创建。
- 六个前端开发工程师必备的Web设计模式/模块资源(转)
- Nmap 源代码学习四 软件简单使用
- 外语学习强烈推荐Rosetta Stone
- linshi_temp_erweima_html
- C++primer第三章标准库类型
- SVN分支/合并原理及最佳实践
- Win7 32位系统下Sublime text 3的安装以及配置C/C++、java、python的开发环境方法
- 当 ReactJS 遇到 TypeScript
- 安卓和IOS兼容问题
- centos下 telnet访问百度
- postgreSQL使用杂谈
- vue 项目使用 webpack 构建自动获取电脑ip地址
- java ArrayList去重
- Nginx、Tomcat配置https
- MySql(十一):MySQL性能调优——常用存储引擎优化
- 关于Ubuntu的默认python版本
- jquery validation表单验证插件。
- Installing Oracle Database 12c Release 2(12.2) RAC on RHEL7.3 in Silent Mode
热门文章
- iOS捷径(Workflow 2.0)拓展
- css3 transform + deviceorientation实现图片旋转效果
- 简洁大方的wordpress主题,不容错过的主题,附带主题源码下载
- 21全志r58m平台的framework在使用过程中会莫名的崩溃掉
- Android(java)学习笔记189:ContentProvider使用(银行数据库创建和增删改查的案例)
- CAD参数绘制角度标注(网页版)
- ActiveX控件获取不到对象属性或者方法的原因分析
- jQuery中Ajax事件beforesend及各参数含义1
- socket编程(Java实现)
- Web项目ConcurrentModificationException异常