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,很明显不会超时. 递推式 ...
随机推荐
- AJPFX关于抽象方法和接口
class Demo_Animal1{ public static void main(String[] args) { Cat a = new Cat("加菲 ...
- AJPFX总结关于JVM的基础知识
写在前面 之前老大让做一些外包面试,我的问题很简单: 介绍一下工作中解决过比较 有意思的问题. HashMap使用中需要注意的点. 第一个问题主要是想了解一下对方项目经验的含金量,第二个问题则是测试下 ...
- Caused by: javax.el.PropertyNotFoundException: Property 'product' not found on type java.lang.String
今天在JSP利用EL表达式取值报了 "javax.el.PropertyNotFoundException”,经过debug和打印将问题定位到这段代码: HTML应该是没啥问题,看提示在ja ...
- Java常用的排序查找算法
public static void main(String[] args) { // bubbleSort(); // int[] a = {20,2,10,8,12,17,4,25,11 ...
- C++ 异常处理(try catch throw)、命名空间
一.c++工具 模板(函数模板.类模板).异常处理.命名空间等功能是c++编译器的功能,语言本身不自带,这些功能已经成为ANSI C++标准了,建议所有的编译器都带这些功能,早期的c++是没有这些功能 ...
- GIT 获取指定历史版本代码
cd 到该项目的一个本地仓库下 log 查看提交记录,找到想要的提交记录,粘贴对应的希哈值 执行 git checkout 希哈值 这本地的这个仓库的代码就会变成你想要的那个版本的代码
- 从mysql全库备份中恢复指定库和指定表
需求:开发要求导入某天某个表的数据,而我们的数据是全库备份 例如: 从newbei_2017-08-31_402793782.tar.bz2中恢复表:bei_table 的数据 一.备份策略 备份全 ...
- caffe数据读取
caffe的数据读取分为lmdb和 待清理,包括fast 这个一系列是怎么转换成lmdb数据的
- MFC如何设置窗口最前
首先,放到最前 this->SetWindowPos(&wndTopMost,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);//使窗口总是在最前面 this->Se ...
- mysql 优化策略
from:https://dbaplus.cn/news-155-1531-1.html MySQL逻辑架构 如果能在头脑中构建一幅MySQL各组件之间如何协同工作的架构图,有助于深入理解MySQL服 ...