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");
}
}

Inversion 归并求逆元的更多相关文章

  1. CF451E Devu and Flowers (隔板法 容斥原理 Lucas定理 求逆元)

    Codeforces Round #258 (Div. 2) Devu and Flowers E. Devu and Flowers time limit per test 4 seconds me ...

  2. 【板子】gcd、exgcd、乘法逆元、快速幂、快速乘、筛素数、快速求逆元、组合数

    1.gcd int gcd(int a,int b){ return b?gcd(b,a%b):a; } 2.扩展gcd )extend great common divisor ll exgcd(l ...

  3. 【BZOJ-4522】密钥破解 数论 + 模拟 ( Pollard_Rho分解 + Exgcd求逆元 + 快速幂 + 快速乘)

    4522: [Cqoi2016]密钥破解 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 290  Solved: 148[Submit][Status ...

  4. hdu 1576 求逆元

    题意:给出n=A mod 9973和B,求(A/B) mod 9973 昨天用扩展欧几里得做过这题,其实用逆元也可以做. 逆元的定义:例如a*b≡1 (mod m),则b就是a关于m的逆元. 求逆元方 ...

  5. HDU4869:Turn the pokers(快速幂求逆元+组合数)

    题意: 给出n次翻转和m张牌,牌相同且一开始背面向上,输入n个数xi,表示xi张牌翻转,问最后得到的牌的情况的总数. 思路: 首先我们可以假设一开始牌背面状态为0,正面则为1,最后即是求ΣC(m,k) ...

  6. ZOJ 3609 求逆元

    Modular Inverse Time Limit: 2 Seconds      Memory Limit: 65536 KB The modular modular multiplicative ...

  7. codeforces 492E. Vanya and Field(exgcd求逆元)

    题目链接:codeforces 492e vanya and field 留个扩展gcd求逆元的板子. 设i,j为每颗苹果树的位置,因为gcd(n,dx) = 1,gcd(n,dy) = 1,所以当走 ...

  8. 51NOD 1258 序列求和 V4 [任意模数fft 多项式求逆元 伯努利数]

    1258 序列求和 V4 题意:求\(S_m(n) = \sum_{i=1}^n i^m \mod 10^9+7\),多组数据,\(T \le 500, n \le 10^{18}, k \le 50 ...

  9. 51nod 1118 机器人走方格 解题思路:动态规划 & 1119 机器人走方格 V2 解题思路:根据杨辉三角转化问题为组合数和求逆元问题

    51nod 1118 机器人走方格: 思路:这是一道简单题,很容易就看出用动态规划扫一遍就可以得到结果, 时间复杂度O(m*n).运算量1000*1000 = 1000000,很明显不会超时. 递推式 ...

随机推荐

  1. 使用Xamarin.Forms跨平台开发入门 Hello,Xamarin.Forms 第一部分 快速入门

    本文介绍了如何使用VisualStudio开发Xamarin.Forms 应用程序和使用Xamarin.Forms开发应用的基础知识,包括了构建和发布Xamarin.Forms应用的工具,概念和步骤. ...

  2. Apache Tomcat 之路(三 部署多个应用)

    想要在一台服务器上部署多个web应用的时候有两种部署方式:1.拷贝多个tomcat 服务器,每个服务器启动不同的web应用;2.一个tomcat容器部署多个web应用 两种方式的优缺点:多个tomca ...

  3. iOS--多线程之线程间通讯

    线程间通讯 一.NSThread 1.简单说明 ①线程间通信:在1个进程中,线程往往不是孤立存在的,多个线程之间需要经常进行通信 ②线程间通信的体现 1个线程传递数据给另1个线程 在1个线程中执行完特 ...

  4. iOS-UI控件之UITableView(一)

    UITableView 介绍 UITableView 是用来用列表的形式显示数据的UI控件 举例 QQ好友列表 通讯录 iPhone设置列表 tableView 常见属性 // 设置每一行cell的高 ...

  5. 常用css属性拓展

    text-overflow:clip | ellipsis(默认值:clip)clip:当内联内容溢出块容器时,将溢出部分裁切掉.ellipsis:当内联内容溢出块容器时,将溢出部分替换为(...). ...

  6. Vue.js语法糖整理

    el:element 需要获取的元素,一定是HTML中的根容器元素 data:用于数据的存储 methods:用于存储各种方法 数据绑定字面量只加载一次{{* msg}} data里面可以进行简单的运 ...

  7. Windows bat 设置代理

    转自tt-0411 @echo off cls color 0A Echo The program is running... Echo Setting the ip and dns... netsh ...

  8. 最后一个非零数字(POJ 1604、POJ 1150、POJ 3406)

    POJ中有些问题给出了一个长数字序列(即序列中的数字非常多),这个长数字序列的生成有一定的规律,要求求出这个长数字序列中某个位上的数字是多少.这种问题通过分析,找出规律就容易解决. 例如,N!是一个非 ...

  9. web pack 生成本地dist后 本地可以访问 路径由/ 改 ./

    config / index.js 里面将 / 改成 ./ 有两个 都改了 反正管用 然后npm run build 如果涉及到字体 css里面不会改 需要手工改成 ../../ 反正一般用到字体也不 ...

  10. 05EXtensible Stylesheet Language

    1. EXtensible Stylesheet Language 1. EXtensible Stylesheet Language XSL 指扩展样式表语言(EXtensible Styleshe ...