Inversion 归并求逆元
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 归并求逆元的更多相关文章
- CF451E Devu and Flowers (隔板法 容斥原理 Lucas定理 求逆元)
Codeforces Round #258 (Div. 2) Devu and Flowers E. Devu and Flowers time limit per test 4 seconds me ...
- 【板子】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 ...
- 【BZOJ-4522】密钥破解 数论 + 模拟 ( Pollard_Rho分解 + Exgcd求逆元 + 快速幂 + 快速乘)
4522: [Cqoi2016]密钥破解 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 290 Solved: 148[Submit][Status ...
- hdu 1576 求逆元
题意:给出n=A mod 9973和B,求(A/B) mod 9973 昨天用扩展欧几里得做过这题,其实用逆元也可以做. 逆元的定义:例如a*b≡1 (mod m),则b就是a关于m的逆元. 求逆元方 ...
- HDU4869:Turn the pokers(快速幂求逆元+组合数)
题意: 给出n次翻转和m张牌,牌相同且一开始背面向上,输入n个数xi,表示xi张牌翻转,问最后得到的牌的情况的总数. 思路: 首先我们可以假设一开始牌背面状态为0,正面则为1,最后即是求ΣC(m,k) ...
- ZOJ 3609 求逆元
Modular Inverse Time Limit: 2 Seconds Memory Limit: 65536 KB The modular modular multiplicative ...
- codeforces 492E. Vanya and Field(exgcd求逆元)
题目链接:codeforces 492e vanya and field 留个扩展gcd求逆元的板子. 设i,j为每颗苹果树的位置,因为gcd(n,dx) = 1,gcd(n,dy) = 1,所以当走 ...
- 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 ...
- 51nod 1118 机器人走方格 解题思路:动态规划 & 1119 机器人走方格 V2 解题思路:根据杨辉三角转化问题为组合数和求逆元问题
51nod 1118 机器人走方格: 思路:这是一道简单题,很容易就看出用动态规划扫一遍就可以得到结果, 时间复杂度O(m*n).运算量1000*1000 = 1000000,很明显不会超时. 递推式 ...
随机推荐
- webform 基础一
WebForm是微软开发的一款产品,它将用户的请求和响应都封装为控件.让开发者认为自己是在操作一个windows界面.极大地提高了开发效率.区别于dreamweaver,可以用代码写,也可以把控件像w ...
- 从源码对比DefaultServeMux 与 gorilla/mux
从源码对比DefaultServeMux 与 gorilla/mux DefaultServeMux Golang自带的net/http库中包含了DefaultServeMux方法,以此可以搭建一个稳 ...
- window下phpstudy开启redis扩展
注:一定要注意自己PHP的版本结构是64还是32位的!其次查看PHP Extension Build是NTS or TS! 1.使用phpinfo()函数查看PHP的版本信息,这会决定扩展文件版本(特 ...
- qt creator转换到 COFF 期间失败: 文件无效或损坏
转载请注明出处http://www.cnblogs.com/dachen408/p/7226198.html 环境 Qt5.5+Vs2010,删除vs2010安装目录bin下的cvtres.exe解决 ...
- JavaScript——class与原型对象
原型对象的意义 通过new 一个构造函数,我们能够获得一个实例,在new 的过程中,程序会在内存中申请一块区域,同时我们可以加参数,所以每个对象都不一样. 原型对象则是同一个构造函数 new 出来的所 ...
- JPA createNativeQuery遇到的几个问题
1.count方法返回值类型为java.math.BigInteger Query query = null; String sql = null; sql = "select count( ...
- C#中练级orcle数据查询
直接贴代码哈哈哈, public DataTable getInfo(int flag) { OracleConnection conn = null; DataSet ds = new DataSe ...
- sqlalchemy ORM进阶- 批量插入数据
参考: https://www.jb51.net/article/49789.htm https://blog.csdn.net/littlely_ll/article/details/8270687 ...
- 引入msword
找到解决方法了:不是直接引入mswork.tlh文件的,该文件是#import "C:\\Program Files\\Microsoft Office\\Office12\\MSWORD. ...
- vue全选和取消全选
代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...